Posts

Showing posts from November, 2016

Expression Tree

Image
PROBLEM : Given a simple expression tree, which is also a full binary tree consisting of basic binary operators i.e., + , – ,* and / and some integers, Your task is to evaluate the expression tree. You need to complete the function evalTree which takes the root of the tree as its only argument and returns an integer denoting the result obtained by simplifying the expression tree. Input: The first line of input contains an integer T denoting the no of test cases . Then T test cases follow. Each test case contains an integer N denoting the no of nodes. Then in the next line are N space separated values of the nodes of the Binary tree filled in continous way from the left to the right. Output: For each test case output will be the result obtained by simplifying the expression tree. Constraints: 1<=T<=100 2<=N<=50 Example(To be used only for expected Output): Input: 2 7 + * - 5 4 100 20 3 - 4 7 Output: 100 -3 Explanation: For the first test case t

Punish the Students

PROBLEM : A Professor conducts a Computer Science paper for all students. He had strictly ordered all students to sit according to their roll numbers. However when he started checking the papers, he found out that all the papers were randomly ordered. The students had sat randomly during the exam instead of sitting according to their roll numbers. Professor was very angry and he wanted to teach the students a lesson. He decides to sort the papers by Bubble Sort, count the number of swaps required for each and every student and deduct as many marks of a student as were the number of swaps required for that student. However he also has to maintain a class average of atleast M else he may lose his job. The Professor wants to know whether he should punish the students or save his job. Input: The first line of input takes an integer T denoting the total number of test cases. Then T test cases follow. Each test case has 3 input lines. The first line of each test case takes the numbe

Repetitive Addition Of Digits

PROBLEM : Given a non-negative integer num, repeatedly add all its digits until the result has only one digit. Input: The first line contains 'T' denoting the number of testcases. Then follows description of testcases. The next T lines contains a single integer N denoting the value of N. Output: Output the sum of all its digit until the result has only one digit. Constraints: 1<=T<=30 1<=n<=10^9 Example: Input : 2 1 98 Output : 1 8 Explanation:  For example, if we conisder 98, we get 9+8  = 17 after first addition. Then we get 1+7 = 8.  We stop at this point because we are left with one digit. -------------------------------------------------------------------------------- SIMPLE c++ IMPLEMENTATION : -------------------------------------------------------------------------------- #include<iostream> using namespace std; int Repetitive_Addition(long long ) ; int main()  {     long long no ;     int t ;     cin>>t ;  

Magic Number

PROBLEM : A magic number is defined as a number which can be expressed as a power of 5 or sum of unique powers of 5. First few magic numbers are 5, 25, 30(5 + 25), 125, 130(125 + 5), …. Given the value of n. find the nth Magic number. Input: The first line contains an integer T, depicting total number of test cases. Then following T lines contains an integer N depicting the value of N. Output: Print the nth magic number in a separate line. Constraints: 1 = T = 30 1 = N = 100 Example: Input: 2 1 2 Output: 5 25 -------------------------------------------------------------------------------- SIMPLE c++ IMPLEMENTATION : -------------------------------------------------------------------------------- #include<iostream> using namespace std; int Magic_Number(int ) ; int main()  { int t,no ; cin>>t ; while(t--) {    cin>>no ;    cout<<Magic_Number(no)<<endl ; } return 0; } int Magic_Number(int no) {

Implement Queue using array

PROBLEM : Implement a Queue using Array. Input (To be used for Expected Output): The first line of the input contains an integer 'T' denoting the number of test cases. Then T test cases follow. First line of each test case contains an integer Q denoting the number of queries . A Query Q is of 2 Types (i) 1 x   (a query of this type means  pushing 'x' into the queue) (ii) 2     (a query of this type means to pop element from queue and print the poped element) The second line of each test case contains Q queries seperated by space. Output: The output for each test case will  be space separated integers having -1 if the queue is empty else the element poped out from the queue . You are required to complete the two methods push which take one argument an integer 'x' to be pushed into the quee  and pop which returns a integer poped out from othe queue. Constraints: 1<=T<=100 1<=Q<=100 1<=x<=100 Example: Input 1 5 1 2 1 3

Smallest number divisible by first n numbers

PROBLEM : Given a number n the task is to complete the function which returns an integer denoting the smallest number evenly divisible by each number from 1 to n. Input: The first line of input contains an integer T denoting the no of test cases then T test cases follow. Each line contains an integer N. Output: For each test case output will be in new line denoting the smallest number evenly divisible by each number from 1 to n. Constraints: 1<=T<=50 1<=n<=25 Example(To be used only for expected output): Input: 2 3 6 Output: 6 60 -------------------------------------------------------------------------------- SIMPLE c++ IMPLEMENTATION : -------------------------------------------------------------------------------- /*You are required to complete this method */ long long lcm(long long ,long long ) ; long long gcd(long long ,long long ) ; long long getSmallestDivNum(long long no) {     int i ;     long long ans=1 ;     for(i=1;i<=no;i+

Count Squares

PROBLEM : Given a sample space S consisting of all perfect squares starting from 1, 4, 9 and so on. You are given a number N, you have to print the number of integers less than N in the sample space S. Input : The first line contains an integer T, denoting the number of test cases.Then T test cases follow. The first line of each test case contains an integer N, denoting the number. Output : Print the answer of each test case in a new line. Constraints : 1<=T<=100 1<=N<=10^18 Example Input : 2 9 3 Output : 2 1 -------------------------------------------------------------------------------- SIMPLE c IMPLEMENTATION : -------------------------------------------------------------------------------- #include <stdio.h> int square(int n) {     int i=1,temp=0,count=0;     while(temp<n)     {         temp=i*i;         count=count+1;         i++;     }     return count-1; } int main() { int t,n; scanf("%d",&t); whil

Student record

PROBLEM : A file contains data as follows( Student name, marks in 3 subjects) Shrikanth 20 50 60 Kiran 30 80 90 Find the student who has maximum average score. Input: The first line of input contains an integer T denoting the number of test cases. The first line of each test case is N,N is the number of student. The second line of each test case contains N input Student name and marks in 3 subject. Output: Print the student who has maximum average score and maximum average score(in int). Constraints: 1 = T = 10 1 = N = 15 1 = s = 10 1 = marks = 100 Example: Input: 2 2 Shrikanth 20 30 10 Ram 100 50 10 3 Adam 50 10 40 Suresh 22 1 56 Rocky 100 90 10 Output: Ram 53 Rocky 66 -------------------------------------------------------------------------------- SIMPLE c++ IMPLEMENTATION : -------------------------------------------------------------------------------- #include<iostream> using namespace std; #include<string.h> class student

Print all possible strings

PROBLEM : Given a string str your task is to complete the function printSpaceString which takes only one argument the string str and  prints all possible strings that can be made by placing spaces (zero or one) in between them. For eg .  for the string abc all valid strings will be                 abc                 ab c                 a bc                 a b c Input: The First line of input takes an integer T denoting the number of test cases . Then T test cases follow . Each line of test case contains a string str . Output: For each test case output the strings possible in a single line  separated by a "$" Constraints: 1<=T<=100 1<=length of string str <=100 Example: Input 1 abc Output abc$ab c$a bc$a b c$ -------------------------------------------------------------------------------- SIMPLE c++ IMPLEMENTATION : -------------------------------------------------------------------------------- /*You have to complete this funct

Class Average

PROBLEM : There is this renowned University named Fibonacci University. It is perfect in all aspects. However it has a rule which is completely different from any other school or University - its marking system. The rule says: 1) Allot marks to each and every student based on his/her perfomance. 2) Multiply the marks of those students whose roll numbers are belong to fibonacci number. 3) In case the marks of any student crosses 100, divide it by 100 and take the remainder. Professor Basu teaches Logic Design in this University. Exams are over and all the teachers are required to submit the class average of students in their particular course. However Professor Basu is sick and wants you to do the task on his behalf. Input: The first line of input takes the number of test case, T. Then T test cases follow . Each test case consist of 2 lines . The first line  of each test takes has an integer N where N is the number of students. The second line takes N space separated i

Perfect Reversible String

PROBLEM : You are given a string ‘str’, the task is to check that reverses of all possible substrings of ‘str’ are present in ‘str’? Input : str = "ab" Output: 0 // all substrings are "a","b","ab" but reverse // of "ab" is not present in str Input : str = "aba" Output: 1 Input: The first line contains 'T' denoting the number of testcases. Then follows description of testcases. Each case begins with a single integer N denoting the length of string. The next line contains the string s. Output: Print "1" if it is a palindrome else "1". (Without the double quotes) Constraints: 1<=T<=30 1<=N<=100 Example: Input: 2 3 aba 5 abcde Output: 1 0 -------------------------------------------------------------------------------- SIMPLE c++ IMPLEMENTATION : -------------------------------------------------------------------------------- #include<iostream&g

Search a node in BST

PROBLEM : Given a Binary Search Tree (BST) your task is to complete the function search which returns true if the node with value x is present in the BST else returns false. You should not read any input from   stdin/console. There are multiple test cases. For each test case, this method will be called individually. Input: The first line of the input contains an integer 'T' denoting the number of test cases. Then 'T' test cases follow. Each test case consists of three lines. Description of  test cases is as follows: The First line of each test case contains an integer 'N' which denotes the no of nodes to be inserted in the BST.   . The Second line of each test case contains 'N' space separated values denoting the values of the BST. In the third line is an integer x denoting the node to be searched for. Output: The output for each test case will be 1 if the node is present in the BST else 0. Constraints: 1 <= T <= 100 1 <= N <=