Posts

Showing posts from August, 2019

AR1

QUESTION DESCRIPTION  Ramu and somu played a number game . Ramu asked somu to generate a Program to find the largest two numbers from n elements in an array and then calculate and display their average. Mandatory to declare the variable names are "int array[MAX], i" TEST CASE 1  INPUT 5 2 5 6 4 7 OUTPUT 6.5 TEST CASE 2  INPUT 7 2 5 6 4 7 10 4 OUTPUT 8.5 #include <stdio.h> int main() { int MAX =50; int array[MAX], i, largest1, largest2; int n,j,temp; float mean,sum; scanf("%d",&n); for(i=0;i<n;i++) scanf("%d",&array[i]); for(i=0;i<n;i++) { for(j=i+1;j<n;j++) { if(array[i]<array[j]) { temp=array[i]; array[i]=array[j]; array[j]=temp; } } } largest1=array[0]; largest2=array[1]; sum=largest1+largest2; mean=(sum)/2; printf("%.1f",mean); return 0; }

AR2

QUESTION DESCRIPTION  Kapil dev organized a team selection meeting. He gave instructions to team selectors to review the performance of players name from last to first. So the selectors has a problem to review all the names from the pool of names. Now team selectors approached technical team to show the players ID in a reverse orger. Now technial team members trying to write a Program to insert n integers in an array and print reverse of the array. Now they were fixing the mandatory variable declarations as "int array[MAX], i, largest1, largest2" TEST CASE 1  INPUT 5 10 2 3 5 6 OUTPUT 6 5 3 2 10 TEST CASE 2  INPUT 6 2 1 4 8 7 2 OUTPUT 2 7 8 4 1 2 #include <iostream> using namespace std; int main() { int MAX; cin>>MAX; int array[MAX], i, largest1, largest2; for(i=0;i<MAX;i++) cin>>array[i]; largest1=array[0]; largest2=array[MAX-1]; cout<<largest2<<" "; for(i=MAX-2;i>=1;i--) { cout<<array

AR15

QUESTION DESCRIPTION  Ramar has set of numbers and somu has set of numbers. Now the class instructor is asking to add to sorted arrays. The task is to merge them in a sorted manner. Mandatory method should be "void mergeArrays(int arr1[], int arr2[], int n1,int n2, int arr3[])" TEST CASE 1  INPUT 5 4 1 2 4 9 15 2 3 7 8 OUTPUT 1 2 2 3 4 7 8 9 15 TEST CASE 2  INPUT 5 6 1 2 4 9 15 2 3 7 8 18 20 OUTPUT 1 2 2 3 4 7 8 9 15 18 20 #include <stdio.h> void mergeArrays(int arr1[], int arr2[], int n1,int n2, int arr3[]) { int i,j; for(i=0;i<(n1+n2);i++) { if(i<n1) arr3[i]=arr1[i]; else arr3[i]=arr2[i-n1]; } for(i=0;i<n1+n2-1;i++) { for(j=0;j<n1+n2-1-i;j++) { if(arr3[j]>arr3[j+1]) { int temp=arr3[j]; arr3[j]=arr3[j+1]; arr3[j+1]=temp; } } }//sorting completed for(i=0;i<n1+n2;i++) printf("%d ",arr3[i]); } int main() { int n1, n2,i,j; sc

AR12

QUESTION DESCRIPTION  Ramar has a plan to play a game with his friends. so he has an array representing heights of towers. The array has towers from left to right , count number of towers facing the sunset. Examples: Input : arr[] = {7, 4, 8, 2, 9} Output: 3 Explanation: As 7 is the first element, it can see the sunset. 4 can't see the sunset as 7 is hiding it. 8 can see. 2 can't see the sunset. 9 also can see the sunset. Input : arr[] = {2, 3, 4, 5} Output : 4 TEST CASE 1  INPUT 5 7 4 8 2 9 OUTPUT 3 TEST CASE 2  INPUT 4 2 3 4 5 OUTPUT 4 #include <iostream> using namespace std; int countBuildings(int arr[], int n)  {      int count = 1;      int curr_max = arr[0];      for (int i=1; i<n; i++)      {          if (arr[i] > curr_max)          {              count++;              curr_max=arr[i];          }      }         return count;  }     int main()  { int n;  cin>>n;     int a[n],i;  for(i=0;i<

AR14

QUESTION DESCRIPTION  Manikandan prepares for GATE Exam. One question contains the question related to array. the question contains an array of integers, now manikandan needs to calculate sum of array elements using recursion. Recursive function name should be as follows "int findSum(int A[], int N)" TEST CASE 1  INPUT 5 1 2 3 4 5 OUTPUT 15 TEST CASE 2  INPUT 6 4 5 7 9 5 2 OUTPUT 32 #include <stdio.h> int findSum(int A[], int N) { if (N <= 0) return 0; return (findSum(A, N - 1) + A[N - 1]); } // Driver code int main() { int N; scanf("%d",&N); int A[N],i; for(i=0;i<N;i++) scanf("%d",&A[i]); printf("%d", findSum(A, N)); return 0; }

AR11

QUESTION DESCRIPTION  Professor Malar has given an array, Asked the Students to find the subarray (containing at least 5 numbers) which has the largest sum. Mandatory function declaration should be "int maxSum(int a[], int n, int k)" TEST CASE 1  INPUT 10 5 1 23 5 47 4 3 1 2 12 OUTPUT 103 TEST CASE 2  INPUT 8 12 15 14 30 4 19 23 10 OUTPUT 127 #include <stdio.h> int maxSum(int a[], int n, int k); int main() { int n,a[50],i,k=0,res; scanf("%d",&n); for(i=0;i<n;i++) scanf("%d",&a[i]); res=maxSum(a,n,k); printf("%d",res); return 0; } int maxSum(int a[50],int n,int k) { int i; for(i=0;i<n;i++) { if(a[i]>0) k = k+a[i]; } return k; }

AR16

QUESTION DESCRIPTION  Given an unsorted array that contains even number of occurrences for all numbers except two numbers. Find the two numbers in decreasing order which have odd occurrences in O(n) time complexity and O(1) extra space. Mandatory conditions should be " while(t--)" Input: The first line of input contains an integer T denoting the number of test cases. Then T test cases follow. Each test case contains an integer 'n' denoting the size of the array. Then the following line contains n space separated integers. Output: Print two space separated integers which have odd occurrences. Print the greater number first and then the smaller odd number. Constraints: 1<=T<=10^5 2<=n<=10^5 1<=Ai<=10^5 TEST CASE 1  INPUT 2 8 4 2 4 5 2 3 3 1 6 1 7 5 5 4 4 OUTPUT 5 1 7 1 TEST CASE 2  INPUT 2 6 2 4 1 1 4 5 6 8 0 0 7 7 5 OUTPUT 5 2 8 5 #include <iostream> using namespace std; void printTwoOdd(int arr[], int size) { int xor2 = arr[

AR13

QUESTION DESCRIPTION  Saravanan decided to play a game and he prepared a technical question for the new game. Now he announced the question to calculate the mean of the elements of the array. Mandatory function name should be declared as "float findMean(int A[], int N)" TEST CASE 1  INPUT 5 1 2 3 4 5 OUTPUT 3.00 TEST CASE 2  INPUT 6 1 2 5 41 23 1 OUTPUT 12.17 #include <stdio.h> float findMean(int A[], int N) { float m,sum=0; int j; for(j=0;j<N;j++) { sum=sum+A[j]; } m=sum/N; printf("%.2f",m); } int main() { int n; scanf("%d",&n); int a[n],i; for(i=0;i<n;i++) { scanf("%d",&a[i]); } findMean(a,n); return 0; }

SORT10

QUESTION DESCRIPTION  LALU wanted to purchase a laptop so he went to a nearby sale.There were n Laptops at a sale. Laptop with index i costs ai rupees. Some Laptops have a negative price — their owners are ready to pay LALU if he buys their useless Laptop. LALU can buy any Laptop he wants. Though he's very strong, he can carry at most m Laptops, and he has no desire to go to the sale for the second time. Please, help LALU find out the maximum sum of money that he can earn. Input: First line of the input contains T denoting the number of test cases.Each test case has 2 lines : First line has two spaced integers n m. second line has n integers [a0...ai...an-1]. Output: The maximum sum of money that LALU can earn, given that he can carry at most m Laptops. Constraints: 1≤T≤10 1≤n,m≤100 -1000≤ai≤1000 TEST CASE 1  INPUT 1 5 3 -6 0 35 -2 4 OUTPUT 8 TEST CASE 2  INPUT 2 6 2 -4 5 0 -1 2 1 7 4 6 1 0 -2 2 -4 3 OUTPUT 5 6 #include <stdio.h> #include<stdlib.h> void so

SORT17

QUESTION DESCRIPTION  Given two arrays, A and B, of equal size n, the task is to find the minimum value of A[0] * B[0] + A[1] * B[1] +…+ A[n-1] * B[n-1], where shuffling of elements of arrays A and B is allowed. Madatory conditions are "void result(int a[],int b[],int n)" Examples: Input : A[] = {3, 1, 1} and B[] = {6, 5, 4}. Output : 23 Minimum value of S = 1*6 + 1*5 + 3*4 = 23. Input : A[] = { 6, 1, 9, 5, 4 } and B[] = { 3, 4, 8, 2, 4 } Output : 80. Minimum value of S = 1*8 + 4*4 + 5*4 + 6*3 + 9*2 = 80. Input: The first line of input contains an integer denoting the no of test cases. Then T test cases follow. Each test case contains three lines. The first line of input contains an integer N denoting the size of the arrays. In the second line are N space separated values of the array A[], and In the last line are N space separated values of the array B[]. Output: For each test case in a new line print the required result. Constraints: 1<=T<=100 1<=N<=50 1<=A

SORT17

QUESTION DESCRIPTION  Given two arrays, A and B, of equal size n, the task is to find the minimum value of A[0] * B[0] + A[1] * B[1] +…+ A[n-1] * B[n-1], where shuffling of elements of arrays A and B is allowed. Madatory conditions are "void result(int a[],int b[],int n)" Examples: Input : A[] = {3, 1, 1} and B[] = {6, 5, 4}. Output : 23 Minimum value of S = 1*6 + 1*5 + 3*4 = 23. Input : A[] = { 6, 1, 9, 5, 4 } and B[] = { 3, 4, 8, 2, 4 } Output : 80. Minimum value of S = 1*8 + 4*4 + 5*4 + 6*3 + 9*2 = 80. Input: The first line of input contains an integer denoting the no of test cases. Then T test cases follow. Each test case contains three lines. The first line of input contains an integer N denoting the size of the arrays. In the second line are N space separated values of the array A[], and In the last line are N space separated values of the array B[]. Output: For each test case in a new line print the required result. Constraints: 1<=T<=100 1<=N<=50 1<=A

SORT17

QUESTION DESCRIPTION  Given two arrays, A and B, of equal size n, the task is to find the minimum value of A[0] * B[0] + A[1] * B[1] +…+ A[n-1] * B[n-1], where shuffling of elements of arrays A and B is allowed. Madatory conditions are "void result(int a[],int b[],int n)" Examples: Input : A[] = {3, 1, 1} and B[] = {6, 5, 4}. Output : 23 Minimum value of S = 1*6 + 1*5 + 3*4 = 23. Input : A[] = { 6, 1, 9, 5, 4 } and B[] = { 3, 4, 8, 2, 4 } Output : 80. Minimum value of S = 1*8 + 4*4 + 5*4 + 6*3 + 9*2 = 80. Input: The first line of input contains an integer denoting the no of test cases. Then T test cases follow. Each test case contains three lines. The first line of input contains an integer N denoting the size of the arrays. In the second line are N space separated values of the array A[], and In the last line are N space separated values of the array B[]. Output: For each test case in a new line print the required result. Constraints: 1<=T<=100 1<=N<=50 1<=A

SORT11

QUESTION DESCRIPTION  In a candy store there are N different types of candies available and the prices of all the N different types of candies are provided to you. You are now provided with an attractive offer. You can buy a single candy from the store and get atmost K other candies ( all are different types ) for free. Now you have to answer two questions. Firstly, you have to tell what is the minimum amount of money you have to spend to buy all the N different candies. Secondly, you have to tell what is the maximum amount of money you have to spend to buy all the N different candies. In both the cases you must utilize the offer i.e. you buy one candy and get K other candies for free. Mandatory conditions are "static void mergeSort(int a[],int l,int r)" Input The first line of the input contains T the number of test cases. Each test case consists of two lines. The first line of each test case contains the values of N and K as described above. Then in the next line N intege

SORT15

QUESTION DESCRIPTION  Given an array of distinct positive numbers, the task is to calculate the minimum number of subsets (or subsequences) from the array such that each subset contains consecutive numbers. Mandatory variables are "int n and int t" 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 length of the array. Next line contains N space seperated integers of the array. Output: For each test case output a new line denoting count of number of such subset's that contains consecutive numbers. Constraints: 1<=T<=100 1<=N<=50 TEST CASE 1  INPUT 2 11 100 56 5 6 102 58 101 57 7 103 5 3 10 100 105 OUTPUT 4 3 TEST CASE 2  INPUT 5 12 12 23 85 11 65 22 87 24 10 86 64 63 5 1 5 3 2 6 7 6 8 1 7 2 5 4 10 45 21 74 44 72 20 4 73 5 46 8 4 2 9 5 3 7 8 1 OUTPUT 4 2 2 4 2 #include <stdio.h> int i,count=0,j; void sort(int a[],int n) { int i,j,t

SORT13

QUESTION DESCRIPTION  You have to merge the two sorted arrays into one sorted array (in non-increasing order) Input: First line contains an integer T, denoting the number of test cases. First line of each test case contains two space separated integers X and Y, denoting the size of the two sorted arrays. Second line of each test case contains X space separated integers, denoting the first sorted array P. Third line of each test case contains Y space separated integers, denoting the second array Q. Output: For each test case, print (X + Y) space separated integer representing the merged array. TEST CASE 1  INPUT 1 4 5 7 5 3 1 9 8 6 2 0 OUTPUT 9 8 7 6 5 3 2 1 0 TEST CASE 2  INPUT 2 5 6 8 5 3 1 0 15 12 10 7 4 2 4 2 8 6 3 0 7 1 OUTPUT 15 12 10 8 7 5 4 3 2 1 0 8 7 6 3 1 0 #include <stdio.h> int main() { int t,N1,N2,arr[101],i,j,temp; scanf("%d",&t); while(t-->0) { scanf("%d %d", &N1, &N2); for (i=0; i<N1; i++) s

SORT3

QUESTION DESCRIPTION  Sort the given set of numbers using Bubble Sort. The first line of the input contains the number of elements, the second line of the input contains the numbers to be sorted. In the output print the status of the array at the 3rd iteration and the final sorted array in the given format. Mandatory declaration for function is "void printArr(int arr[], int size)" TEST CASE 1  INPUT 7 64 34 25 12 22 11 90 OUTPUT 12 22 11 25 34 64 90 Sorted array:11 12 22 25 34 64 90 TEST CASE 2  INPUT 9 64 34 25 12 22 11 90 35 26 OUTPUT 12 22 11 25 34 26 35 64 90 Sorted array:11 12 22 25 26 34 35 64 90 #include <iostream> using namespace std; void printArr(int arr[], int size) { cout<<"Sorted array:"; for(int k=0;k<size;k++) cout<<arr[k]<<" "; } int main() { int i,j,size,arr[100]; cin>>size; for(i=0;i<size;i++) cin>>arr[i]; for(i=0;i<size-1;i++) { for(j=0;j<size-

SORT 12

QUESTION DESCRIPTION  Given an array of integers, sort the array according to frequency of elements. For example, if the input array is {2, 3, 2, 4, 5, 12, 2, 3, 3, 3, 12}, then modify the array to {3, 3, 3, 3, 2, 2, 2, 12, 12, 4, 5}. If frequencies of two elements are same, print them in increasing order. 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 each sorted array in a seperate line. For each array its numbers should be seperated by space. Constraints: 1 ≤ T ≤ 70 30 ≤ N ≤ 130 1 ≤ A [ i ] ≤ 60 TEST CASE 1  INPUT 1 5 5 4 5 4 6 OUTPUT 4 4 5 5 6 TEST CASE 2  INPUT 2 6 1 2 3 2 1 5 8 2 22 3 6 55 22 1 3 OUTPUT 1 1 2 2 3 5 3 3 22 22 1 2 6 #include<iostream> #include<algorithm&

SORT 6

QUESTION DESCRIPTION  MS .Dhoni want to play a game when the players are free in the dressing room. Dhoni have given an array of n distinct elements, the task is to find all elements in array which have at-least two greater elements than themselves. Dhoni declared a mandatory conditions like "void sort(int a[],int n)" Examples: Input : A[] = {2, 8, 7, 1, 5}; Output : 1 2 5 The output three elements have two or more greater elements Input : A[] = {7, -2, 3, 4, 9, -1}; Output : -2 -1 3 4 Input: The first line of input contains an integer T denoting the no of test cases. Each test case contains two lines . The first line of input contains an integer n denoting the size of the array. Then in the next are n space separated values of the array. Output: For each test case in a new line print the space separated sorted values denoting the elements in array which have at-least two greater elements than themselves. TEST CASE 1  INPUT 2 5 2 8 7 1 5 6 7 -2 3 4 9 -1 OUTPUT 1 2 5

SORT 9

SORT9 QUESTION DESCRIPTION  Given an array of integers and two numbers k1 and k2. Find sum of all elements between given two k1’th and k2’th smallest elements of array. It may be assumed that (1 <= k1 < k2 <= n) and all elements of array are distinct. 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 length of the array. Next line contains N space seperated integers of the array. Third line contains two space seperated integers denoting k1'th and k2'th smallest elements. Output: For each test case in a new line output the sum of all the elements between k1'th and k2'th smallest elements. Constraints: 1<= T <= 100 1<= k1< K2 <= N <=50 TEST CASE 1  INPUT 2 7 20 8 22 4 12 10 14 3 6 6 10 2 50 12 48 13 2 6 OUTPUT 26 73 TEST CASE 2  INPUT 1 6 12 45 32 1 22 5 2 5 OUTPUT 34 #include <stdio.h> int main() { i