Home Author
Author

nikoo28

  • Misc

    Different ways to swap 2 numbers.

    by nikoo28
    2 minutes read

    Question: Write a program to swap 2 numbers? Input: a = 4, b = 19 Output: a = 19, b = 4 We will try to discuss the different ways to swap 2 numbers. To understand how swapping works, please visit this post. Method 1 : Using a temporary variable Method 2 : Without a temporary variable Method 3: Using multiplication and division IMP: It is necessary here that numbers should be non-zero Method 4: Using Bitwise XOR (^) Method 5: Swapping in single line using arithmetic operator Method 6: …

    0 FacebookTwitterLinkedinWhatsappEmail
  • Misc

    Write a program to swap 2 numbers.

    by nikoo28
    3 minutes read

    Question: Write a program to swap 2 numbers? Input: a = 4, b = 19 Output: a = 19, b = 4 Swapping 2 numbers is one of the most basic tasks while programming. But sometimes, we tend to make error in it. For instance look at this code. The output of the above program is:- What happened? The values were changed inside the function but the changes did not reflect when returning. This is because when we call the function swapIntegers(int, int), only the value of the integers is …

    0 FacebookTwitterLinkedinWhatsappEmail
  • Question: Given a sorted array of n integers that has been rotated an unknown number of times, give a O(log (n)) solution that finds an element in the array? Input: arr [] = {15, 16, 19, 20, 25, 1, 3, 4, 5, 7, 10, 14}. Find 5 . Output: 8 (the index of 5 in the array) We can solve this problem in only one scan of the array as well. Please read Method 1 if you have not done so, as this forms the base of this problem. To …

    0 FacebookTwitterLinkedinWhatsappEmail
  • MiscTheory

    Algorithm Analysis – Crash Course

    by nikoo28
    5 minutes read

    We have been discussing the time complexities of each of the algorithm that we have discussed. Let us try to analyze, how these time complexities are calculated. O(1) Time complexity of a function (or set of statements) is considered as O(1) if it doesn’t contain loop, recursion and call to any other non-constant time function. For example swap() function(which interchanges two numbers) has O(1) time complexity. A loop or recursion that runs a constant number of times is also considered as O(1). For example the following loop is O(1). O(n) …

    0 FacebookTwitterLinkedinWhatsappEmail
  • Question: Given a sequence of integers, find the median of the integers? Input: arr [] = {15, 23, 4, 16, 8, 42}. Find median. Output: 15 Since the question does not specify anything we cannot assume that the given array is a sorted one. The definition of median is the (n/2)th element of a sorted sequence of integers. This definition makes our approach simpler. All we need to do is sort the integers and then return the (n/2)th element. Here is the code for above approach. I use quick sort …

    0 FacebookTwitterLinkedinWhatsappEmail
  • Misc

    Some general tips for programming.

    by nikoo28
    5 minutes read

    A Programming Contest is a delightful playground for the exploration of intelligence of programmers. To start solving problems in contests, first of all, you have to fix your aim. Some contestants want to increase the number of problems solved by them and the other contestants want to solve less problems but with more efficiency. Choose any of the two categories and then start. If you are a beginner, first try to find the easier problems.Try to solve them within short time. At first, you may need more and more time …

    0 FacebookTwitterLinkedinWhatsappEmail
  • Question: Given a sorted array of n integers that has been rotated an unknown number of times, give a O(log (n)) solution that finds an element in the array? Input: arr [] = {15, 16, 19, 20, 25, 1, 3, 4, 5, 7, 10, 14}. Find 5 . Output: 8 (the index of 5 in the array) Let us assume that the given array is arr[]. The basic approach for this problem will be:- Find the pivot element. That means after which all elements are in ascending order. This basically …

    1 FacebookTwitterLinkedinWhatsappEmail
  • MiscTheory

    What is a wild pointer?

    by nikoo28
    2 minutes read

    Question: What is a wild pointer in a program? Uninitialized pointers are known as wild pointers. These are called so because they point to some arbitrary memory location and may cause a program to crash or behave badly. This can be understood from the below examples. Please note that if a pointer p points to a known variable then it’s not a wild pointer. In the below program, p is a wild pointer till this points to a. If we want pointer to a value (or set of values) without …

    0 FacebookTwitterLinkedinWhatsappEmail
  • MiscTheory

    What is a memory leak?

    by nikoo28
    2 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
    3 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

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