Home Misc
Category:

Misc

View Miscellaneous problems

  • Misc

    Different ways 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 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
    4 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
  • MiscTheory

    Algorithm Analysis – Crash Course

    by nikoo28
    6 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
    6 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
  • 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
  • Misc

    Write a program to calculate pow(x,n)

    by nikoo28
    3 minutes read

    We will discuss how to calculate the result when a number ‘x’ is raised to the power ‘y’. Originally C provides a standard function that allows us to directly use the power function. It can be used in the following manner. Now suppose that we need to write a custom function by ourselves. Here are the sample methods by which we can do so. Simple Iterative method Recursive Method But in both these methods, the time complexity is of O(n), and this can take a long time in operation. We …

    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