Posts

Showing posts from June, 2016

Chandu and Consecutive Letters

Chandu is very fond of strings. (Or so he thinks!) But, he does not like strings which have same consecutive letters. No one has any idea why it is so. He calls these strings as Bad strings. So, Good strings are the strings which do not have same consecutive letters. Now, the problem is quite simple. Given a string S, you need to convert it into a Good String. You simply need to perform one operation - if there are two same consecutive letters, delete one of them. Input: The first line contains an integer T, denoting the number of test cases. Each test case consists of a string S, which consists of only lower case letters. Output: For each test case, print the answer to the given problem. Constraints: 1 <= T <= 10 1 <= |S| <= 30 SAMPLE INPUT       SAMPLE OUTPUT 3 abb                  ab aaab                 ab ababa            ...

Terrible Chandu

PROBLEM : Chandu is a bad student. Once his teacher asked him to print the reverse of a given string. He took three hours to solve it. The teacher got agitated at Chandu and asked you the same question. Can you solve it? Input: The first line contains an integer T, denoting the number of test cases. Each test case contains a string S, comprising of only lower case letters. Output: For each test case, print the reverse of the string S. Constraints: 1 <= T <= 10 1 <= |S| <= 30 SAMPLE INPUT     SAMPLE OUTPUT 2 ab                 ba aba                aba -------------------------------------------------------------------------------- SIMPLE C++ IMPLEMENTATION : --------------------------------------------------------------------------------- #include <iostream> using namespace std; #include<string.h> int main() {     int t,n,i ;   ...

Prateek and his Friends

PROBLEM : Prateek wants to give a party to his N friends on his birthday, where each friend is numbered from 1 to N. His friends are asking for a gift to come to the party, instead of giving him one. The cost of the gifts are given in the array Value where ith friend asks for a gift which has a cost Costi. But, Prateek has only X amount of money to spend on gifts and he wants to invite his friends which are in continuous range such that sum of the cost of the gifts of those friends will be exactly equal to X. If he can invite his friends, who can satisfy the above condition then, print YES otherwise print NO Input: The first line contains a single integer T, denoting the number of test cases. In each test case, the following input will be present: - The next line contains two space-separated integers N and X, where N represents the number of friends and X represents amount of money which Prateek can spend on gifts.  - Next N line contains N integers, where ith line contain...

Two Sum

PROBLEM : Given an array of integers, return indices of the two numbers such that they add up to a specific target. You may assume that each input would have exactly one solution. Example: Given nums = [2, 7, 11, 15], target = 9, Because nums[0] + nums[1] = 2 + 7 = 9, return [0, 1]. ------------------------------------------------------------------------------ SIMPLE C++ IMPLEMENTATION  ---------------------------------------------------------------------------------- /**  * Note: The returned array must be malloced, assume caller calls free().  */ int* twoSum(int* nums, int numsSize, int target) {     int i,j,sum,*data ;     flag=0 ;     for(i=0;i<numsSize;i++)     {         for(j=i+1;j<numsSize;j++)         {             sum=nums[i]+nums[j] ;             if(sum==targe...

Max sum no adjacent

PROBLEM : Given an array of positive numbers, find the maximum sum of a subsequence with the constraint that no 2 numbers in the sequence should be adjacent in the array. So 3 2 7 10 should return 13 (sum of 3 and 10) or 3 2 5 10 7 should return 15 (sum of 3, 5 and 7). 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 C[i]. Output: Print the maximum sum of a subsequence. Constraints: 1 = T = 80 1 = N = 100 1 = C[i] = 500 Example: Input: 2 6 5 5 10 100 10 5 4 3 2 7 10 Output: 110 13 ------------------------------------------------------------------------------ Simple C++ implementation : ------------------------------------------------------------------------------ #include<iostream> using namespace std; int max(int[],int) ; int main()  { int a[500],t,no,i,r ; cin>>t ; while(...

Swap kth node

PROBLEM : Given an array, swap kth element from beginning with kth element from end. 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 and k,N is the size of array and kth number. The second line of each test case contains N input C[i]. Output: Print the modified array. Constraints: 1 = T = 100 1 = K = N = 500 1 = C[i] = 1000 Example: Input 1 8 3 1 2 3 4 5 6 7 8 Output 1 2 6 4 5 3 7 8 --------------------------------------------------------------------------------- Simple C++ implementation : --------------------------------------------------------------------------------- #include<iostream> using namespace std; void swap(int[],int,int) ; int main()  { int a[1000],t,no,i,k ; cin>>t ; while(t--) {    cin>>no>>k ;    for(i=0;i<no;i++)        cin>>a[i] ;       ...

Count the numbers

PROBLEM : Given a number N, count the numbers from 1 to N which comprise of digits, only in set 1, 2, 3, 4 and 5. Input: The first line of input contains an integer T denoting the number of test cases.Then T test cases follow. Each test case consist of one line. Each line of each test case is N, where N is the range from 1 to N. Output: Print the count of numbers in the given range from 1 to N. Constraints: 1 = T = 100 1 = N = 1000 Example: Input: 2 100 10 Output: 30 5 Explanation: When n is 20 then answer is 10 because 1 2 3 4 5 11 12 13 14 15 are only in given set. 16 is not beause 6 is not in given set, only 1 2 3 4 5 in set. -------------------------------------------------------------------------------- Simple C++ Code : -------------------------------------------------------------------------------- #include<iostream> using namespace std; int call(int) ; int main()  { int test,no,i,r,count  ; cin>>test ; while(te...

Leaders in an array

Problem : Write a program to print all the LEADERS in the array. An element is leader if it is greater than all the elements to its right side. The rightmost element is always a leader. Input: The first line of input contains an integer T denoting the number of test cases. The description of T test cases follows. The first line of each test case contains a single integer N denoting the size of array. The second line contains N space-separated integers A1, A2, ..., AN denoting the elements of the array. Output: Print all the leaders. Constraints: 1 <= T <= 100 1 <= N <= 100 0 <= A[i]<=100 Example: Input: 2 6 16 17 4 3 5 2 5 1 2 3 4 0 Output: 17 5 2 4 0 ----------------------------------------------------------------------------- Methord 1 : Use two loops. The outer loop runs from 0 to size – 1 and one by one picks all elements from left to right. The inner loop compares the picked element to all the elements to its right side. If the pic...

UGLY number

PROBLEM : Ugly numbers are numbers whose only prime factors are 2, 3 or 5. The sequence 1, 2, 3, 4, 5, 6, 8, 9, 10, 12, 15, … shows the first 11 ugly numbers. By convention, 1 is included. Write a program to find Nth Ugly Number. 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 Nth Ugly Number. Constraints: 1 = T = 100 1 = N = 500 Example: Input: 2 10 4 Output: 12 4 ------------------------------------------------------------------------------ //SIMPLE C++ IMPLEMENTATION ------------------------------------------------------------------------------ #include<iostream> using namespace std; int ugly_no_check(int) ; int main()  { int test,no,ele,count,r ; cout<<"\n Enter number of test cases : " ; cin>>test ; while(test--) { cout<<"\n enter the N'th position "  ;    cin>>no ;    c...