Count number of Bits to be flipped to convert A to B

Count number of bits to be flipped to convert A to B. OR
Find number of Bit swaps required to convert one integer to another OR
Given two integers A & B. Determine how many bits required to convert A to B.


Given two number A and B, Write an algorithm to count the number of bit flips required to convert A to B
Let's see what is the input and expected output for better understanding,

Case 1:
Input A: 6 (Binary equivalent of 6:   0 1 1 0 )
Input B: 8 (Binary equivalent of 8:   1 0 0 0 )
Output: Number of bits that need to be changed in A to convert it to B is :  3

Explanation: (From Right, For number 6, 

Bit at position 0 is 0, which is same in both number,
Bit at position 1 is 1, which needs to be unset to match it to number B,
Bit at position 2 is 1, which needs to be unset to match it to number B, 
Bit at position 3 is 0, which needs to be set to match it to number B)

 Case 2: 
Input A: 9 (Binary equivalent of 9:   0 1 0 0 )
Input B: 2 (Binary equivalent of 2:   0 0 1 0 )
Output: Number of bits that need to be changed in A to convert it to B is : 2

Explanation: (From Right, For number 9, 
Bit at position 0 is 0, which is same in both number,
Bit at position 1 is 0, which needs to be set to match it to number B,
Bit at position 2 is 1, which needs to be unset to match it to number B, 
Bit at position 3 is 0, which is same in both number)

Count number of Set Bits in Integer

Count set bits in Integer OR
Count number bits set in an Integer OR
Count number of ones in Binary.


We need to write an algorithm to determine the number of set bits in integer.
In other words, count the number of 1's present in binary representation of number.

Let's see what is the input and expected output for better understanding,

Case 1:
Input: 6 Binary equivalent of 6:   0 1 1 0
Number of bits set(1's) :  2
Output: 2

Case 2: 
Input:14
Binary equivalent of 14:   1 1 1 0
Number of bits set(1's) :  3
Output: 3

Case 3: 
Input: 8
Binary equivalent of 8:   1 0 0 0
Number of bits set(1's) :  1
Output: 1

Factorial of Number using Recursion in Java

Find Factorial of Number. OR
Java Program to Find Factorial of Number. OR
Recursive Program to find Factorial of Number.


A factorial is a function that multiplies a number by every number below it.

Factorial of 5
5! = 5 * 4 * 3 * 2 * 1 = 120.

Factorial of 3
3! = 3 * 2 * 1 = 120.

Note: The value of 0! is 1

Below you can get Factorial of number till 20.
factorial of numbers from 1 to 20 chart
Factorial of numbers from 1 to 20 chart

Print Matrix Diagonally OR Diagonal order of Matrix.

Print Matrix Diagonally. OR
Loop diagonally through two dimensional array. OR
Traverse an array diagonally. OR
Print elements of Matrix in Diagonal order.


Given a Matrix / 2D array, Print all elements of Matrix in Diagonal order. Lets understand Diagonal order of Matrix graphically.

Find a Saddle point in Matrix

Find Saddle point in a matrix


Given a matrix of n x n size, Find saddle point of matrix.

What is Saddle point of Matrix?

Element is said to be Saddle point of Matrix if it is both a minimum of its row and a maximum of its column or vice versa.

A matrix may have 1 or 2 saddle points or may not have a saddle point.
Lets understand what will be input and expected output with the help of an example.


Print nodes at K distance from Leaf node in Binary tree.

Print nodes at K distance from Leaf in binary tree. OR
Print all nodes that are at distance k from a leaf node.


Given a Binary Tree, Print all Nodes that are at K distance from leaf node in Binary Tree. Lets understand what will be input and expected output with the help of an example.


If k = 1. It means we need to print all nodes that are at distance 1 from Leaf node.

In Case 2, we have 4 leaf nodes (Node 1, Node 10, Node 5, Node7).
Node at distance K that is Node at distance 1 from leaf Node 1 is Node 2 (Print 2)
Node at distance K that is Node at distance 1 from leaf Node 10 is Node 9 (Print 9)
Node at distance K that is Node at distance 1 from leaf Node 5 is Node 6 (Print 6)
Node at distance K that is Node at distance 1 from leaf Node 7 is Node 6 (6 already printed, ignore)

Print Nodes at K distance from Root in Binary Tree

Print nodes at K distance from root in binary tree. OR
Print nodes at Level K.


Given a Binary Tree, Print all Nodes that are at K distance from root node in Binary Tree. We can also think of this question as Print all nodes that belong to Level K.
 
Lets understand what will be input and expected output with the help of an example.
 

If k = 2. It means we need to print all nodes that are at distance 2 from Root node.
It also means we need to print all nodes at Level 2, because all Nodes of Level 2 will be at same distance from Root Node.

Rotate matrix by 90 degree

Rotate matrix by 90 degree OR
Turn an 2D array by 90 degree OR

Rotate a two dimensional array OR
Given N*M matrix, rotate it by 90 degree to left and right.

Given N*M Matrix, Rotate it by 90 degrees.
 
Lets understand the problem statement graphically and it will be more clear,  


Lets see given input and expected output below,

Rotate Matrix by 90 degrees clockwise Inplace

Rotate square matrix by 90 degrees Inplace OR
Turn an 2D array by 90 degree OR

Rotate a two dimensional array OR
Given N*N matrix, rotate it by 90 degree to left and right without extra memory

Rotate square matrix by 90 degrees Inplace.
Lets understand the problem statement graphically and it will be more clear,  


Print Matrix in Spiral order using Recursion.

Print Matrix in Spiral order OR
Given m*n matrix(m rows, n columns), print all elements of the matrix in spiral order OR

Print two-dimensional array in spiral order OR
How to print elements of Matrix in Spiral Format?

Given a Matrix(2D array), print it in spiral form. Lets understand the problem statement graphically and it will be more clear,