Consecutive numbers with sum equal to n

Given a positive integer n, print the consecutive numbers whose sum is equal to n. For example, n = 20 Output: 2, 3, 4, 5, 6 (because 2+3+4+5+6+7 = 20) n = 29 Output: 14, 15

Number of ways to arrange tiles on a board

Given a board of size 2*n and tiles of dimension 1*2 each. In how many ways can we arrange the tiles so that entire board is covered. We can only place the tiles either horizontally or vertically without breaking them. For example, if the board is of length 3 (n=3), then tiles can be placed […]

Dynamic Programming concept

The basic idea of DP is applicable to problems that can be thought in terms of recursion. For example, consider problem of computing n’th fibonacci term:

Radix Sort

Consider an array that stores account numbers of all employees. One unique thing about account numbers is that they have equal number of digits. Let us take example where, account numbers are three digits long, and array has 6 account numbers are shown below int arr[ ] = {582, 675, 591, 189, 900, 770} Range […]

Sum of all the nodes in a tree

Given a binary tree, where each node is having an integer value. Write a function that accept the root of this tree and returns the sum of all the nodes in the tree. The sum of all the nodes in the

Iterative pre-order traversal using stack

We have seen the in-order traversal of a binary tree. It is a recursive code. Recursion stack can be simulated using an explicit Stack data structure and keeping the function non-recursive. Let us see how:

Right view of a binary tree

Write code to print the right-view of a binary tree. Right-view of the tree is the nodes visible when the tree is seen from the right side. If there are two nodes at the same level, then only the right-most node will be visible in the right-view as shown below: