Explain ((n & (n-1)) == 0)

Problem : Explain what the following code does: ((n & (n-1)) == 0). Solution When we have A & B == 0, it means that A and B have no common 1s, i.e. any two bits at the same positions of A and B are either different from each other, or two 0s. It works because a binary power of two is of the form 1000...000 and subtracting one will give you 111. [Read More]

Print binary representation of a decimal number

intPart »= 1; this is fr wat?

Unknown - Jul 6, 2014

intPart »= 1; this is fr wat?

Hi Chaitrali, sorry for the delayed response. This means division by 2.
In general, bit_arg»shift_arg shifts bits to of bit_arg shift_arg places to the right – equivalent to integer division by 2^shift_arg

Print binary representation of a decimal number

Problem Given a (decimal – e.g. 3.72) number that is passed in as a string, print the binary representation. If the number can not be represented accurately in binary, print “ERROR”. Solution This is equivalent to implement toBinaryString() method built in Java API. Lets think about the IEEE-754 standard. In a 32-bit machine, the 1st bit is for sign, 2nd – 9th digits are for exponent and the rest are for mantissa. [Read More]

You have two very large binary trees: T1, with millions of nodes, and T2, with hundreds of nodes Create an algorithm to decide if T2 is a subtree of T1

Problem You have two very large binary trees: T1, with millions of nodes, and T2, with hundreds of nodes Create an algorithm to decide if T2 is a subtree of T1 Solution Method 1 - Brute force Traverse T1. If the current node is equal to the root node of T2, traverse both of the trees (T2 and the current subtree of T1) at the same time. Compare the current node. [Read More]

Print all paths in a binary tree which sum up to a value

Problem You are given a binary tree in which each node contains a value. Design an algorithm to print all paths which sum up to that value. Note that it can be any path in the tree – it does not have to start at the root. Example Consider a tree below: 10 / \\ 7 15 / \\ / \\ 8 9 11 1  input value = 16 [Read More]

Finding the biggest difference between sorted array values

Problem finding the biggest difference between sorted array values **Example ** Consider the array : var array = array(1,4,7,8,12,15); The values in the array will always be integers and is sorted, and elements may repeat. Now, we want to print out the biggest step in the array. step is difference between adjacent element: step array - (-,3,3,1,4,3) The biggest step is 4, between 8 and 12. Solution Method 1- Brute force [Read More]

Set all bits of a number in a range equal to another number

Problem You are given two 32-bit numbers, N and M, and two bit positions, i and j. Write a method to set all bits between i and j in N equal to M (e.g., M becomes a substring of N located at i and starting at j). **Example ** Consider the integer N, where we want to fit in M between bits i and j of N. Input: N = 10000000000, M = 10101, i = 2, j = 6 Output: N = 10001010100 [Read More]

Add arithmetic operators to make 3 1 3 6 = 8 true

Problem: Add arithmetic operators (plus, minus, times, divide) to make the following expression true: 3 1 3 6 = 8. You can use any parentheses you’d like. Solution: Method 1 - Hit and try This is what I can think of :  we can first see 3+1+3+6 =13 No Multiplying is not solution either (3+1+3)/6 No 48/6 = 8 but 48 cannot be made by 3 1 3. [Read More]

Find all pairs (x, y) in a sorted array so that x + y < z

Problem

Given a sorted integer array and number z find all pairs (x, y) in the array so that x + y < z. Can it be done better than O(n^2)?

Solution

Method 1

The solution can be found in O(n^2) where we select 1st element from n elements and 2nd element y from n-1 elements, and hence n(n-1)/2 ~ O(n^2)

Reference - stackoverflow

Divide a number by 3 without using *, /, +, -, % operators

Problem

Divide a number by 3 without using *, /, +, -, % operators

Solution

Method 1 - Divide the number using bitwise operator

We have already discussed how to divide a number by 3 with the help of ‘+’ operator here. So, all we have to do is write a plus operator with the help of bitwise operator, which we have done here.

// replaces the + operator  
int add(int x, int y) {  
    int a, b;  
    do {  
        a = x & y;  
        b = x ^ y;  
        x = a << 1;  
        y = b;  
    } while (a);  
    return b;  
}  
  
int divideby3 (int num) {  
    int sum = 0;  
    while (num > 3) {  
        sum = add(num >> 2, sum);  
        num = add(num >> 2, num & 3);  
    }  
    if (num == 3)  
        sum = add(sum, 1);  
    return sum;   
}