Thursday, 14 July 2016

How-many-pairs-of-2-numbers-are-there-whose-LCM-is-N

If N= a^m*b^n *c^o then ordered solution such that LCM = N is (2m+1)*(2n+1)*(2o+1)
Unordered will be (2m+1)*(2n+1)*(2o+1) /2 +0.5

A 4 digit no. is formed using the digits 0,2,4,6,8 without repeating any 1 of them. What is the sum of all such possible no.s?

1)519960 2)402096 3)133320 4)4321302

If you fix 8 as the last digit, you see that there are 4*3*2 ways to complete the number. Thus, 8 appears 24 times as the last digit. By the same logic, if we enumerate all possible numbers using these 5 digits, each number appears 24 times in each of the 4 positions. That is, the digit 8 contributes (24*8+240*8+2400*8+24000*8). In total, we have
(0+2+4+6+8)(24+240+2400+24000)=533280
as our total sum.
In case 4-digit numbers cannot start with 0, then we have over-counted. Now we have to subtract the amount by which we over-counted, which is found by answering: "What is the sum of all 3-digit numbers formed by using digits 2,4,6, and 8?" Now if 8 appears as the last digit, then there are 6 ways to complete the number, so 8 contributes (6*8+60*8+600*8). In total, we have
(2+4+6+8)(6+60+600)=13320.
Subtracting this from the above gives us 519960.

Which is the Smallest number that has 4 prime factors & 48 total factors?

Take first 4 prime numbers as
2^a*3^b*5^c*7^d
Give maximum powers to 2 and 3 to get the minimum number 

(a+1)(b+1)(c+1)(d+1)=48
4*3*2*2=48
a=3 b=2 c=1 d=1

8*9*5*7=2520 Ans.

A ball is dropped from a height of 96 feet and it rebounds 2/3 of the height it falls. If it continues to fall and rebound, find the total distance that the ball can travel before coming to rest? 

a.240 ft b 360 ft c.480 ft d.none of these

Here is a trick if ball rebounds for 2/3 or for any other fraction (p/q) like 3/4, 1/2 or 5/2 
then use this (p+q)*height.
so we get, according to this question,

(2+3)*96= 480 Feet.

Another Trick:
96(1+r)/(1-r)=96(5/3)/(1/3)=480

Monday, 4 July 2016

Power of 2

How do you find out if an unsigned integer is a power of 2?

The trick here is to know that an unsigned integer which is a power of 2 has only one of its bits as 1. So a simple solution would be to loop through the bits and count the number of 1s.
There is another solution which uses bit manipulation.
isPower = (x !=0 && !(x & (x-1)))
x & (x-1) will always give you a 0 if x is a power of 2. !(x & (x-1)) should give us what we want but there is one corner case. If x is 0, then the second term alone would return true when the answer should be false. We need the check to make sure x is not 0.

Reverse a String

First Solution

void reverseString(char* str) { int len, loop;
char temp; len = strlen(str); for(loop = 0; loop < (len / 2); loop++) { temp = str[loop];
str[loop] = str[len - loop - 1];
str[len - loop - 1] = temp; } }

Second Solution

The second solution is slightly better than the first as it does not need the char space. It uses bit manipulation (XOR in this case) to swap the chars in place.

void reverseStringBetter(char* str) { int loop1 = 0, loop2; loop2 = strlen(str) - 1; for (loop1 = 0; loop1 < loop2; loop1++, loop2--) { str[loop1] ^= str[loop2] ; str[loop2] ^= str[loop1] ; str[loop1] ^= str[loop2] ; } }

Number of Ones

Write a function that determines the number of bits set to 1 in the binary representation of an integer.
Going by the brute force approach, it would be easy to come up with the following solution. If the least significant bit of a number is 1, it will return a result of 1 when we AND it with 1. Using this logic, we can shift bits in the number to the right and keep testing if the least significant bit is 1. When there are no 1’s left in the number, it will become zero. 

int numOnesInInteger(int num) { int numOnes = 0; while(num != 0) { if((num & 1) == 1) { numOnes++; } num = num >> 1; } return numOnes; }

Time Complexity: O(n) ==> Where n in the number of bits in the integer.

Improved Solution:

Let’s think, what happens to a number when it we AND it with (number – 1). Let’s take 7 (0111) as an example.

7 & 6 = 0111 & 0110 = 0110 = 6
6 & 5 = 0110 & 0101 = 0100 = 4
4 & 3 = 0100 & 0011 = 0000 = 0
Do you see a pattern yet? Essentially, when you AND a number with (number – 1), the last 1 bit in the number gets set to zero. So if we continue to set every 1 bit in the number to zero, the number will eventually become zero. If we use this logic to count the number of 1s, we have an algorithm that runs in O(m) where m is the number of bits set to 1 in the number. Here’s some code.
int numOnesInInteger(int num) { int numOnes = 0; while(num != 0){ num = num & (num – 1); numOnes++; } return numOnes; }
int main()
{
     int num, count = 0;
     printf(“Enter the number: “);
     scanf(“%d”,&num);
     count = number_of_ones(num);
     printf(“\nNumber of one’s are %d\n”, count);
     return 0;
}