Posts

Showing posts with the label miscellaneous problems

Product array puzzle

PROBLEM : Given an array arr[] of n integers, construct a Product Array prod[] (of same size) such that prod[i] is equal to the product of all the elements of arr[] except arr[i]. 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 size of array. The second line of each test case contains N input A[i]. Output: Print the Product array prod[]. Constraints: 1 = T = 10 1 = N = 15 1 = C[i] = 20 Example: Input 2 5 10 3 5 6 2 2 12 20 Output 180 600 360 300 900 20 12 -------------------------------------------------------------------------------- SIMPLE c++ IMPLEMENTATION : -------------------------------------------------------------------------------- #include<bits/stdc++.h> using namespace std; int main() { int t ;     cin>>t ;     while(t--)     {         int no ;         cin>>no ...

Roman Number to Integer

PROBLEM : Given an string in roman no format (s)  your task is to convert it to integer . Input: The first line of each test case contains the no of test cases T. Then T test cases follow. Each test case contains a string s denoting the roman no. Output: For each test case in a new line print the integer representation of roman number s. Constraints: 1<=T<=100 1<=roman no range<4000 Example: Input 2 V III Output 5 3 -------------------------------------------------------------------------------- SIMPLE c++ IMPLEMENTATION : -------------------------------------------------------------------------------- #include<bits/stdc++.h> using namespace std; int value(char r) {     if (r == 'I')         return 1;     if (r == 'V')         return 5;     if (r == 'X')         return 10;     if (r == 'L')         return 5...

Two numbers with sum closest to zero

PROBLEM : Given an integer array, you need to find the two elements such that their sum is closest to zero. 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,the size of array Each test case consist of a N integers which are the array elements. Output: Print the two numbers in ascending order such that their sum is closest to zero. Constraints: 1 = T = 100 1 = N = 1000 -100007 = a[i] = 100007 Example: Input 3 3 -8 -66 -60 6 -21 -67 -37 -18 4 -65 2 -24 -73 Output -60 -8 -18 4 -73 -24 -------------------------------------------------------------------------------- SIMPLE c++ IMPLEMENTATION : -------------------------------------------------------------------------------- #include<bits/stdc++.h> using namespace std; int main() { int t ;     cin>>t ;     while(t--)     {         int no ;       ...

Does robot moves circular

PROBLEM : Given a sequence of moves for a robot, check if the sequence is circular or not. A sequence of moves is circular if first and last positions of robot are same. A move can be on of the following.         G - Go one unit     L - Turn left     R - Turn right Input: The first line of input contains an integer T denoting the number of test cases. The first line of each test case is a String in capital letter, sequence of moves consisting only {R,G,L}. Output: Print Given sequence of moves is circular if first and last positions of robot are same. else Given sequence of moves is NOT circular. Constraints: 1 = T = 50 1 = size of string = 200 Example: Input: 3 GLGLGLG GLLG GGGGL Output: Circular Circular Not Circular -------------------------------------------------------------------------------- SIMPLE c++ IMPLEMENTATION : -------------------------------------------------------------------------------- #inclu...

First non-repeating character in a stream

PROBLEM : Given an input stream of n characters consisting only of small case alphabets the task is to find the first non repeating character each time a character is inserted to the stream. Example Flow in stream : a, a, b, c a goes to stream : 1st non repeating element a (a) a goes to stream : no non repeating element -1 (5, 15) b goes to stream : 1st non repeating element is b (a, a, b) c goes to stream : 1st non repeating element is b (a, a, b, c) 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 size of the stream. Then in the next line are x characters which are inserted to the stream. Output: For each test case in a new line print the first non repeating elements separated by spaces present in the stream at every instinct when a character is added to the stream, if no such element is present print -1. Constraints: 1<=T<=200 1<=N<=500 Exa...

Next Permutation

PROBLEM : Implement the next permutation, which rearranges numbers into the numerically next greater permutation of numbers. If such arrangement is not possible, it must be rearranged as the lowest possible order ie, sorted in an ascending order. For example: 1,2,3 -> 1,3,2 3,2,1 -> 1,2,3 Input: The first line contains an integer T, depicting total number of test cases. Then following T lines contains an integer N depicting the size of array and next line followed by the value of array. Output: Print the array of next permutation in a separate line. Constraints: 1 = T = 40 1 = N = 100 0 = A[i] = 100 Example: Input 1 6 1 2 3 6 5 4 Output 1 2 4 3 5 6 -------------------------------------------------------------------------------- SIMPLE c++ IMPLEMENTATION : -------------------------------------------------------------------------------- #include<iostream> #include<algorithm> using namespace std; void swap(int *a,int *b) {   ...

Who Will Win?

PROBLEM : Gautam and Subhash are two brothers. They are similar to each other in all respects except one. They have different ways of searching. When Gautam has to search for an item from a lot, he goes through all the items one by one and stops when he finds the item. However Subhash has an entirely different and interesting way of searching. However the search works only for items which are sorted. He goes to the middle of the lot, if he finds the desired item, he stops, if not, he checks whether the middle item is smaller or larger than the required item. If larger, he repeats the same process for the first half of the lot, otherwise second half. One day, the two brothers sit in a contest in which they have to find a name out of a sorted dictionary. Whoever finds out the name first will win the contest. The audience is very eager to know who will win and hence they want you to predict. Input: The first line of input takes the number of test cases, T. The next T lines take the...

Angle between hour and minute hand

PROBLEM : Calculate the angle between hour hand and minute hand. There can be two angles between hands, we need to print minimum of two. Also, we need to print floor of final result angle. For example, if the final angle is 10.61, we need to print 10. Input: The first line of input contains a single integer T denoting the number of test cases. Then T test cases follow. Each test case consists of one line conatining two space separated numbers h and m where h is hour and m is minute. Output: Coresponding to each test case, print the angle b/w hour and min hand in a separate line. Constraints: 1 = T = 100 1 = h = 12 1 = m = 60 Example: Input 2 9 60 3 30 Output 90 75 -------------------------------------------------------------------------------- SIMPLE c++ IMPLEMENTATION : -------------------------------------------------------------------------------- #include<iostream> using namespace std; #include<math.h> int CalAngle(float ,float ) ; int...

Common elements

PROBLEM : Given three arrays sorted in non-decreasing order, print all common elements in these arrays. Input: First line consists of T test cases. First line of every test case consists of 3 integers N1, N2 and N3, denoting the number of elements of 3 arrays. Second, Third and Forth line of every test case conisists of elements of array1, array2 and array3 respectively. Output: Single line output, Print the common elements of array. If not possible then print -1. Constraints: 1<=T<=100 1<=N1,N2,N3<=1000 1<=Ai,Bi,Ci<=1000 Example: Input: 1 6 5 8 1 5 10 20 40 80 6 7 20 80 100 3 4 15 20 30 70 80 120 Output: 20 80 -------------------------------------------------------------------------------- SIMPLE c++ IMPLEMENTATION : -------------------------------------------------------------------------------- #include<iostream> using namespace std; int greatest(int a,int b,int c){     int temp=a>b?a:b ;     return te...

Thief trying to escape

PROBLEM : A thief trying to escape from a jail has to cross N walls each with varying heights. He climbs X feet every time. But, due to the slippery nature of those walls, every times he slips back by Y feet.  Now the task is to calculate the total number of jumps required to cross all walls and escape from the jail. 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 two space separated integers X, Y, N. Then in the next line are N space separated values denoting the heights ( Ht[] ) of the walls. Output: For each test case in a new line print the total number of jumps. Constraints: 1<=T<=100 1<= N, X, Y <=100 1<= Ht[] <=1000 Example: Input: 2 10 1 1 5 4 1 5 6 9 11 4 5 Output: 1 12 -------------------------------------------------------------------------------- SIMPLE c++ IMPLEMENTATION : -----------------------------------------------------------------...

Geek and Coffee Shop

PROBLEM : Geek love's to drink cold coffee after coding for long hour's. One fine day Geek went to his favourite coffee shop in the town and asked for a cup of cold coffee. The shopkeeper told him that he is their lucky customer and had an offer for the Geek. The offer was that for an amount of N they will fill Geek's cup with N units of cold coffee, the offer doesn't over yet. After consuming initial N units of coffee the shopkeeper will again refill his cup with half the amount of coffee that Geek consumed in previous refill, and will keep on refilling his cup till the amount to refill becomes nil i.e. 0 (Assume Geek can consume infinite amount of coffee and shop also has infinite amount coffee). Now Geek is curious to know that how many units of coffee will Geek get for Mth refill. Being Geek's friend help him out with his problem.   Input: The first line of the input contains an integer T, denoting the number of test cases. The T test case follow. The only...

Find the two non-repeating elements in an array of repeating elements

PROBLEM : You are given an array A containing 2*N+2 positive numbers, out of which N numbers are repeated exactly once and the other two numbers occur exactly once and are distinct. You need to find the other two numbers and print them in ascending order. Input : The first line contains a value T, which denotes the number of test cases. Then T test cases follow .The first line of each test case contains a value N. The next line contains 2*N+2 space separated integers. Output : Print in a new line the two numbers in ascending order. Constraints : 1<=T<=100 1<=N<=10^6 1<=A[i]<=5*10^8 Example: Input : 2 2 1 2 3 2 1 4 1 2 1 3 2 Output : 3 4 1 3     -------------------------------------------------------------------------------- SIMPLE c++ IMPLEMENTATION : -------------------------------------------------------------------------------- #include<iostream> using namespace std; int main() {     int t ;   ...

Check if a number can be expressed as a sum of consecutive numbers

PROBLEM : Given a number n, the task is to check whether it can be expressed as a sum of two or more consecutive numbers or not. Examples: Input  : n = 10 Output : 1 It can be expressed as sum of two consecutive numbers 1 + 2 + 3 + 4. Input  : n = 16 Output : 0 It cannot be expressed as sum of two consecutive numbers. Input  : n = 5 Output : 1 2 + 3 = 5 Input: The first line contains 'T' denoting the number of test cases. Then follows description of test cases. Each test case contains a single positive integer n. Output: Print "1" if number can be expressed as sum of consecutives else "0". (Without the double quotes) Constraints: 1<=T<=200 0<=N<=10^18 Example: Input : 2 4 5 Output : 0 1     -------------------------------------------------------------------------------- SIMPLE c++ IMPLEMENTATION : -------------------------------------------------------------------------------- #include<iostream...

Number to words

PROBLEM : Given number into words. For example, if “1234” is given as input, output should be “one thousand two hundred and thirty four” 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. Output: Print the number into words (in small letter). Constraints: 1 = T = 100 1 = N = 9999 Example: Input: 6 101 717 9999 1000 1090 111 Output: one hundred and one seven hundred and seventeen nine thousand nine hundred and ninety nine one thousand one thousand and ninety one hundred and eleven -------------------------------------------------------------------------------- SIMPLE c++ IMPLEMENTATION : -------------------------------------------------------------------------------- #include<iostream> using namespace std; string PrintDigitWord11(int ) ; string PrintDigitWord1(int ) ; string PrintDigitWord2(int ) ; string PrintDigitWord3(int ) ; string PrintDigitWord4(int ) ...

Generate Binary Numbers

PROBLEM : Given a number n, Write a program that generates and prints all binary numbers with decimal values from 1 to n. 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. Output: Print all binary numbers with decimal values from 1 to n in a single line. Constraints: 1 = T = 100 1 = N = 500 Example: Input 2 2 5 Output 1 10 1 10 11 100 101 -------------------------------------------------------------------------------- SIMPLE c++ IMPLEMENTATION : -------------------------------------------------------------------------------- #include<iostream> using namespace std; #include<queue> int main() { int t,no ; cin>>t ; while(t--) {    cin>>no ;    queue<int> q ;    q.push(1) ;      while(no--)    {        int curr=q.front() ;       ...

Excel Sheet

PROBLEM : Given a positive integer, return its corresponding column title as appear in an Excel sheet. For example:     1 -> A     2 -> B     3 -> C     ...     26 -> Z     27 -> AA     28 -> AB NOTE: The alphabets are all in uppercase. Input: The first line contains an integer T, depicting total number of test cases. Then following T lines contains an integer N. Output: Print the string corrosponding to the column number. Constraints: 1 = T = 40 1 = N = 10000000 Example: Input 1 51 Output AY -------------------------------------------------------------------------------- SIMPLE c++ IMPLEMENTATION : -------------------------------------------------------------------------------- #include<iostream> using namespace std; #include <string> #include <algorithm> string Excel_Sheet(int ) ; int main() { int t ; long no ; cin>>t ; whi...

Strong Number

PROBLEM : Write a program to check whether a number is strong or not. A number is called strong number if sum of the factorial of its digit is equal to number itself. For example: 145 as 1! + 4! + 5! = 1 + 24 + 120 = 145 Input: First line contains number of test cases T. Then following T lines contains an integer N. Output: Output "Strong" if given number is strong else "Not Strong" . Constraints: 1<=T<=50 0<=N<=1000 Example: Input: 2 145 10 Output: Strong Not Strong -------------------------------------------------------------------------------- SIMPLE c++ IMPLEMENTATION : -------------------------------------------------------------------------------- #include<iostream> using namespace std; int Is_Strong(int ) ; int factorial(int ) ; int main()  { int t,no ; cin>>t ; while(t--) {    cin>>no ;    if(Is_Strong(no))        cout<<"Strong" ;    el...

Chocolate Distribution Problem

PROBLEM : Given an array A[] of N integers where each value represents number of chocolates in a packet. Each packet can have variable number of chocolates. There are m students, the task is to distribute chocolate packets such that : 1. Each student gets one packet. 2. The difference between the number of chocolates given to the students in packet with maximum chocolates and packet with minimum chocolates is minimum. Examples Input : A[] = {3, 4, 1, 9, 56, 7, 9, 12}         m = 5 Output: Minimum Difference is 6 We may pick 3,4,7,9,9 and the output is 9-3 = 6 Input : A[] = {7, 3, 2, 4, 9, 12, 56}         m = 3 Output: Minimum difference is 2 We can pick 2, 3 and 4 and get the minimum difference between maximum and minimum packet sizes. ie 4-2 = 2 Input: The first line of input contains an integer T, denoting the no of test cases. Then T test cases follow. Each test case consists of three lines. The first line of each test case...

Largest Number formed from an Array

PROBLEM : Given a list of non negative integers, arrange them in such a manner that they form the largest number possible. The result is going to be very large, hence return the result in the form of a string. Input: The first line of input consists number of the test cases. The description of T test cases is as follows: The first line of each test case contains the size of the array, and the second line has the elements of the array. Output: In each separate line print the largest number formed by arranging the elements of the array in the form of a string. Constraints: 1 = T = 70 1 = N = 100 0 = A[i] = 1000 Example: Input: 2 5 3 30 34 5 9 4 54 546 548 60 Output: 9534330 6054854654 -------------------------------------------------------------------------------- SIMPLE c++ IMPLEMENTATION : -------------------------------------------------------------------------------- #include<iostream> using namespace std; #include<algorithm> #include...