Home Misc
Category:

Misc

View Miscellaneous problems

  • Question: Write a program to count the number of bits that are 1(set bits) in a given integer. Input: 8 (1000) 11 (1011) Output: For 8 – 1 set bit For 11 – 3 set bits This question can be done in a very long way where we first convert the integer to its binary form and store its result in a character array. Traversing that character array would give the number of set bits. But that will not be an efficient way. We will use bit manipulation to solve …

    0 FacebookTwitterLinkedinWhatsappEmail
  • The general way to print any statement would be like:- But in this example we are using at least one semicolon(;). Our target is to print something on the screen without using even a single semi colon(;) . This is sort of a fun problem rather than an actual concept. It may seem to be impossible at once but we can utilize the fact that the statements inside an if condition is always executed and its result is used to determine the block to be executed. We can do something …

    0 FacebookTwitterLinkedinWhatsappEmail
  • MiscTheory

    What is actually Space Complexity ?

    by nikoo28
    1 minutes read

    The term Space Complexity is misused for Auxiliary Space at many places. Following are the correct definitions of Auxiliary Space and Space Complexity. Auxiliary Space is the extra space or temporary space used by an algorithm. Space Complexity of an algorithm is total space taken by the algorithm with respect to the input size. Space complexity includes both Auxiliary space and space used by input. We can also say that the way in which the amount of storage space required by an algorithm varies with the size of the problem …

    0 FacebookTwitterLinkedinWhatsappEmail
  • 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
  • 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
  • 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

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