Algorithm Details

  1. Brian Kernighan’s Algorithm

    • Used to count set bits in a Number

    + Show Algorithm

                    1  Initialize count: = 0
                    2  If integer n is not zero
                       (a) Do bitwise & with (n-1) and assign the value back to n
                           n: = n&(n-1)
                       (b) Increment count by 1
                       (c) go to step 2
                    3  Else return count
                    

    Time Complexity = O(Logn)


  2. Kadane's Algorithm

    • Efficient way to find the largest sum of contiguous Sub-Array in a one-dimensional Array

    + Show Algorithm

                    Initialize:
                    max_so_far = INT_MIN
                    max_ending_here = 0
    
                    Loop for each element of the array
                    (a) max_ending_here = max_ending_here + a[i]
                    (b) if(max_so_far < max_ending_here)
                                max_so_far = max_ending_here
                    (c) if(max_ending_here < 0)
                                max_ending_here = 0
                    return max_so_far
                    
                    

    Time Complexity = O(n)