Problem link: http://www.geeksforgeeks.org/inorder-tree-traversal-without-recursion/
Problem Statement:
It is easy to traverse a binary tree in-order using recursion. Do it without using recursion.
Continue reading
[TopCoder, ProjectEuler, HackerRank, CodeChef, CodeForces, InterviewSites]
Problem link: http://www.geeksforgeeks.org/inorder-tree-traversal-without-recursion/
Problem Statement:
It is easy to traverse a binary tree in-order using recursion. Do it without using recursion.
Continue reading
Problem link: http://www.geeksforgeeks.org/lowest-common-ancestor-in-a-binary-search-tree/
Problem Statement:
Given values of two nodes in a Binary Search Tree, write a c program to find the Lowest Common Ancestor (LCA). You may assume that both the values exist in the tree. You can assume that the nodes are present in the tree.
Continue reading
Problem link: http://www.geeksforgeeks.org/check-for-identical-bsts-without-building-the-trees/
Problem Statement:
Given two arrays which represent a sequence of keys. Imagine we make a Binary Search Tree (BST) from each array. We need to tell whether two BSTs will be identical or not without actually constructing the tree.
For example, the input arrays are {2, 4, 3, 1} and {2, 1, 4, 3} will construct the same tree
Continue reading
Problem link: http://www.geeksforgeeks.org/connect-leaves-doubly-linked-list/
Problem Statement:
Given a Binary Tree, extract all leaves of it in a Doubly Linked List (DLL). Note that the DLL need to be created in-place. Assume that the node structure of DLL and Binary Tree is same, only the meaning of left and right pointers are different. In DLL, left means previous pointer and right means next pointer.
Continue reading
Problem link: http://www.geeksforgeeks.org/move-zeroes-end-array/
Problem Statement:
Given an array of random numbers, Push all the zero’s of a given array to the end of the array. For example, if the given arrays is {1, 9, 8, 4, 0, 0, 2, 7, 0, 6, 0}, it should be changed to {1, 9, 8, 4, 2, 7, 6, 0, 0, 0, 0}. The order of all other elements should be same. Expected time complexity is O(n) and extra space is O(1).
Continue reading
Problem link: http://www.geeksforgeeks.org/boundary-traversal-of-binary-tree/
Problem Statement:
Given a binary tree, print boundary nodes of the binary tree Anti-Clockwise starting from the root.
Continue reading
[The problem appeared in TopCoder SRM 594 (Div-2, Level-1)]
Problem link: http://community.topcoder.com/stat?c=problem_statement&pm=12811
Problem Statement:
Fox Ciel is now in high school. The seats in her classroom are arranged into an n by m matrix. The rows are numbered from 0 to n-1 (front to back) and the columns from 0 to m-1 (left to right).
At the beginning, Ciel can choose any of the seats. Then, at the end of each week Ciel will shift one row to the back and one column to the right, wrapping around whenever necessary. Formally, if her current seat is in row r and column c, then her seat next week will be the one in row ((r+1) modulo n) and column ((c+1) modulo m).
Fox Ciel now wonders whether she can sit in all the seats in the classroom if she follows the above procedure. As we already mentioned, she can start in any of the seats. Also, she can attend the school for as many weeks as she wants to. Return “Possible” if she can sit in all the seats and “Impossible” otherwise.
Continue reading
[The problem appeared in TopCoder SRM 592 (Div-2, Level-1)]
Problem link: http://community.topcoder.com/stat?c=problem_statement&pm=12112
Problem Statement:
Little Elephant from the Zoo of Lviv has a bunch of books. You are given a int[] pages. Each element of pages is the number of pages in one of those books. There are no two books with the same number of pages.
You are also given a int number. As a homework from the teacher, Little Elephant must read exactly number of his books during the summer. (Little Elephant has strictly more than number books.)
All other elephants in the school also got the exact same homework. Little Elephant knows that the other elephants are lazy: they will simply pick the shortest number books, so that they have to read the smallest possible total number of pages. Little Elephant wants to be a good student and read a bit more than the other elephants. He wants to pick the subset of books with the second smallest number of pages. In other words, he wants to pick a subset of books with the following properties:
1. There are exactly number books in the chosen subset.
2. The total number of pages of those books is greater than the smallest possible total number of pages.
3. The total number of pages of those books is as small as possible (given the above conditions).
Return the total number of pages Little Elephant will have to read.
Continue reading
Problem link: http://www.codechef.com/AUG13/problems/LELEMON
Problem Statement:
Little Elephant likes lemonade.
When Little Elephant visits any room, he finds the bottle of the lemonade in that room that contains the greatest number of litres of lemonade and drinks it all.
There are n rooms (numbered from 0 to n-1), each contains bottles. Each bottle has a volume (in litres). The first room visited by Little Elephant was
th, the second
th, …, the m-th
th room. Note that Little Elephant may visit a room more than once.
Find for Little Elephant the total volume of lemonade he has drunk.
Continue reading
Problem link: http://www.codechef.com/AUG13/problems/HELLO/
Problem Statement:
Chef talks a lot on his mobile phone. As a result he exhausts his talk-value (in Rokdas) very quickly. One day at a mobile recharge shop, he noticed that his service provider gives add-on plans which can lower his calling rates (Rokdas/minute). One of the plans said “Recharge for 28 Rokdas and enjoy call rates of 0.50 Rokdas/min for one month”. Chef was very pleased. His normal calling rate is 1 Rokda/min. And he talked for 200 minutes in last month, which costed him 200 Rokdas. If he had this plan activated, it would have costed him: 28 + 0.5*200 = 128 Rokdas only! Chef could have saved 72 Rokdas. But if he pays for this add-on and talks for very little in the coming month, he may end up saving nothing or even wasting money. Now, Chef is a simple guy and he doesn’t worry much about future. He decides to choose the plan based upon his last month’s usage.
There are numerous plans. Some for 1 month, some for 15 months. Some reduce call rate to 0.75 Rokdas/min, some reduce it to 0.60 Rokdas/min. And of course each of them differ in their activation costs. Note – If a plan is valid for M months, then we must pay for (re)activation after every M months (once in M months). Naturally, Chef is confused, and you (as always) are given the task to help him choose the best plan.
Continue reading
Recent Comments