Home Author
Author

nikoo28

  • MiscTheory

    What is a memory leak?

    by nikoo28
    1 minutes read

    Question:What is a memory leak in a program? We have been writing many programs where we have used the “free()” keyword. One such example can be found in the “Delete the complete list” function found in the post – Important traversals in a Linked List. The code snippet is:- This is done due to a reason. When we allocate a memory in a program, usually that memory is unallocated once the execution of the program finishes. However, there may be some cases where the program runs forever and these scenarios,…

    0 FacebookTwitterLinkedinWhatsappEmail
  • StringsTheory

    Swap strings in C

    by nikoo28
    1 minutes read

    Question:How will you swap two strings in C? Let us consider the below program. Output of the program is str1 is study, str2 is algorithms. So the above swap() function doesn’t swap strings. The function just changes local pointer variables and the changes are not reflected outside the function. Let us see the correct ways for swapping strings: Method 1(Swap Pointers):- If you are using character pointer for strings (not arrays) then change str1 and str2 to point each other’s data. i.e., swap pointers. In a function, if we want…

    0 FacebookTwitterLinkedinWhatsappEmail
  • Question: Given an array with both positive and negative numbers, find the two elements such that their sum is closest to zero. ? Input: arr [] = {1, 60, -10, 70, -80, 85} Output: -80, 85 (Their sum is -5 that is closest to 0(zero)) What we discussed was the most naive approach by using two for loops. However, this method is not optimized and will take a lot of time if the data is very large. We need to improve the complexity of the program. One such method is…

    0 FacebookTwitterLinkedinWhatsappEmail
  • Question: Given an array with both positive and negative numbers, find the two elements such that their sum is closest to zero. ? Input: arr [] = {1, 60, -10, 70, -80, 85} Output: -80, 85 (Their sum is -5 that is closest to 0(zero)) We can solve this problem by a brute force approach. That involves two loops, in the first loop we pick an element from the array and in the second loop we add this element individually with each of the other elements in the array. In…

    0 FacebookTwitterLinkedinWhatsappEmail
  • Question: Given an array of n elements. Find two elements such that their sum is equal to a given element ‘K’ ? Input: arr [] = {42, 23, 15, 16, 8, 4} and element K = 58 Output: 16, 42 We discussed the approach to this problem using 2 loops in this post. But obviously this was not a good approach as it had a time complexity of O(n2). The time taken to solve such a problem if the array has elements in range of million will be very high.…

    0 FacebookTwitterLinkedinWhatsappEmail
  • Question: Given an array of n elements. Find two elements such that their sum is equal to a given element ‘K’ ? Input: arr [] = {42, 23, 15, 16, 8, 4} and element K = 58 Output: 16, 42 To understand the problem statement a little more, we are given with an array of ‘n’ elements and another element ‘K’. We need to find if there exists 2 elements in the given array such that their sum equals the given element K. The array is not sorted. One simple…

    0 FacebookTwitterLinkedinWhatsappEmail
  • Question: Given an array with n+2 elements, all elements of the array are in range 1 to n and also all elements occur only once except 2 numbers which occur twice. Find those 2 repeating numbers. Input: arr[] = {6, 2, 6, 5, 2, 3, 1} Output: 6 2 We discussed a method to perform this task using an extra array to count the occurrences of elements, but this approach took an extra space of the range of elements of array. If we have a very wide range, then we…

    0 FacebookTwitterLinkedinWhatsappEmail
  • Question: Given an array with n+2 elements, all elements of the array are in range 1 to n and also all elements occur only once except 2 numbers which occur twice. Find those 2 repeating numbers. Input: arr [ ] = {6, 2, 6, 5, 2, 3, 1} Output: 6 2 We discussed the method to find the 2 repeating element using 2 loops, but that method had a great time complexity and it was not at all an optimal solution. We can optimize it using a count array. This…

    0 FacebookTwitterLinkedinWhatsappEmail
  • Question: Given an array with n+2 elements, all elements of the array are in range 1 to n and also all elements occur only once except 2 numbers which occur twice. Find those 2 repeating numbers. Input: arr[] = {6, 2, 6, 5, 2, 3, 1} Output: 6 2 One simple way is to scan the complete array for each element of the input elements. That means use two loops. In the outer loop, select elements one by one and count the number of occurrences of the selected element in…

    0 FacebookTwitterLinkedinWhatsappEmail
  • Question: Given an array of positive integers, all numbers occur even number of times except one number which occurs odd number of times. Find the number in O(n) time and constant space. Input:- arr[] = {1, 2, 3, 2, 3, 1, 3} Output:- 3 This is a very simple problem. The basic and naive approach will be to scan the complete array and keep a track of each element. We need to count the occurrence of each element and see if its even or odd. This would be a very…

    0 FacebookTwitterLinkedinWhatsappEmail

This website uses cookies to improve your experience. We'll assume you're ok with this, but you can opt-out if you wish. Accept Read More