Given two binary trees, write a function to check if they are equal or not.
Two binary trees are considered equal if they are structurally identical and the nodes have the same value.
Return 0 / 1 ( 0 for false, 1 for true ) for this problem
/ \ / \
2 3 2 3
1 or True
The following code is supposed to rotate the array A by B positions.
So, for example,
A : [1 2 3 4 5 6]
B : 1
The output :
[2 3 4 5 6 1]
However, there is a small bug in the problem. Fix the bug and submit the problem.
Given an array with n objects colored red, white or blue,
sort them so that objects of the same color are adjacent, with the colors in the order red, white and blue.
Here, we will use the integers 0, 1, and 2 to represent the color red, white, and blue respectively.
Note: Using library sort function is not allowed.
Input : [0 1 2 0 1 2]
Modify array so that it becomes : [0 0 1 1 2 2]
Given 2 non negative integers m and n, find gcd(m, n)
GCD of 2 integers m and n is defined as the greatest integer g such that g is a divisor of both m and n.
Both m and n fit in a 32 bit signed integer.
m : 6
n : 9
GCD(m, n) : 3
NOTE : DO NOT USE LIBRARY FUNCTIONS
Given an array of integers, every element appears twice except for one. Find that single one.
Note: Your algorithm should have a linear runtime complexity. Could you implement it without using extra memory?
Input : [1 2 2 3 1]
Output : 3
The basic logic that A XOR A = 0 means that means all the doubles will be XOR’ed out to 0 and the remaining number will be the result of the XOR.
Dexter and Debra are playing a game. They have N containers each having one or more chocolates. Containers are numbered from 1 to N, where ith container has A[i] number of chocolates.
The game goes like this. First player will choose a container and take one or more chocolates from it. Then, second player will choose a non-empty container and take one or more chocolates from it. And then they alternate turns. This process will continue, until one of the players is not able to take any chocolates (because no chocolates are left). One who is not able to take any chocolates loses the game. Note that player can choose only non-empty container.
The game between Dexter and Debra has just started, and Dexter has got the first Chance. He wants to know the number of ways to make a first move such that under optimal play, the first player always wins.
The first line contains an integer N, i.e., number of containers.
The second line contains N integers, i.e., number of chocolates in each of the containers separated by a single space.
Print the number of ways to make the first move such that under optimal play, the first player always wins. If the first player cannot win under optimal play, print 0.
1 ≤ N ≤ 106
1 ≤ A[i] ≤ 109
Only 1 set of moves helps player 1 win.
Player: 1 2 1 2 1
Chocolates: 2 3 -> 2 2 -> 1 2 -> 1 1 -> 0 1
Theory Data for the solution and algorithm