Elab Sorting
Elab Sorting
SORT14
QUESTION DESCRIPTION
Mommy is a very active lady. She likes to keep all stuff sorted. She has developed an interesting
technique of sorting stuff over the years. She goes through the items repeatedly from first to last and
whenever she finds two consecutive items unsorted, she puts them in the proper order. She
continues the process until all the items are sorted.
One day Mommy has to attend a wedding ceremony. Suddenly she remembers that she has not
sorted the plates after washing. She has only M minutes left. If she can complete the task within the
remaining time, she will sort her plates and then attend the wedding. However if she cannot, she
decides to skip the task.
She knows that she take S seconds per swap. However she does not know the total number of
swaps required and hence she is in trouble. She wants you to help her out.
#include<stdio.h>
int main()
{
int a[1001];
int n,i,j,t,swap,temp,k,m,ss,r;
scanf("%d", &t);
if(t==1)
{
printf("1");
goto a;
}
while(t--)
{
swap=0;
scanf("%d %d %d", &m,&ss,&n);
for(j=0; j<a[j+1];j++)
{
temp=a[j];
a[j]=a[j+1];
a[j+1]=temp;
swap++;
}
}
int x,s,c;
x=(s*c)/60;
r=swap*ss;
if(r<=m*60)
printf("1\n0\n0");
else
printf("0");
printf("\n");
return 0;
a:
{
return 0;
}
}
SORT8
QUESTION DESCRIPTION
Given an array with all elements greater than or equal to zero.Return the maximum product of two
numbers possible.
Input:
The first line of input contains an integer T denoting the number of test cases.
Output:
Constraints:
1 ≤ T ≤ 20
1 ≤ N ≤ 50
0 ≤ A[i] ≤ 1000
#include<stdio.h>
void sort(int a[],int n);
int main()
{
int arr[30], i, x, t;
scanf("%d",&t);
while(t--)
{
scanf("%d",&x);
for(i=0;i<x;i++)
{
scanf("%d",&arr[i]);
}
sort(arr, x);
}
return 0;
}
void sort(int a[],int n)
{
int i, j, p=0;
for(i=0;i<n;i++)
{
for(j=0;j<n;j++)
{
if(i!=j)
{
if(p<(a[i]*a[j]))
{
p=a[i]*a[j];
}
}
}
}
printf("%d\n",p);
}
SORT4
QUESTION DESCRIPTION
Ramu and Somu both are decided to play a game to Sort the given set of numbers using Insertion
Sort.
So he got 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.
Somu will evaluate the result whether its correct or not Mandatory declaration need to be follow as "
void InSort(int arr[], int n)"
#include<stdio.h>
int main()
{
int data[30], i, n;
scanf("%d",&n);
for(i=0;i<n;i++)
{
scanf("%d",&data[i]);
}
InSort(data, n);
printArray(data, n);
return 0;
}
SORT6
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.
Examples:
Input:
The first line of input contains an integer T denoting the no of test cases.
The first line of input contains an integer n denoting the size of the array.
#include <stdio.h>
int main()
int t,i,n,j,k,arr[30],temp;
scanf("%d",&t);
for(i=0;i<t;i++)
scanf("%d",&n);
for(j=0;j<n;j++)
{scanf("%d",&arr[j]);}
for(j=0;j<n;j++)
for(k=j+1;k<n;k++)
if(arr[j]>arr[k])
temp=arr[j];
arr[j]=arr[k];
arr[k]=temp;
}
for(j=0;j<n-2;j++)
{printf("%d ",arr[j]);}
printf("\n");
return 0;
SORT13
QUESTION DESCRIPTION
You have to merge the two sorted arrays into one sorted array (in non-increasing order)
Input:
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.
#include <stdio.h>
int main() {
int t,i,a,b,arr[20],j,temp,k;
scanf("%d",&t);
for(i=0;i<t;i++)
scanf("%d%d",&a,&b);
for(j=0;j<(a+b);j++)
scanf("%d",&arr[j]);
for(j=0;j<(a+b);j++)
for(k=j+1;k<(a+b);k++)
if(arr[j]<arr[k])
temp=arr[j];
arr[j]=arr[k];
arr[k]=temp;
}
}
for(j=0;j<(a+b);j++)
{printf("%d ",arr[j]);}
printf("\n");
return 0;
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
#include <stdio.h>
int main()
{
int t,i,n,j,a[30],p,q,sum=0;
scanf("%d",&t);
for(i=0;i<t;i++)
scanf("%d",&n);
for(j=0;j<n;j++)
scanf("%d",&a[j]);
/*for(j=0;j<n;j++)
printf("%d ",a[j]);
printf("\n");*/
sortarr(n,a);
/* for(j=0;j<n;j++)
printf("%d ",a[j]);
printf("\n");*/
scanf("%d%d",&p,&q);
for(j=(p);j<(q-1);j++)
{
sum=sum+a[j];
printf("%d\n",sum);sum=0;
return 0;
int j,k,t;
for(j=0;j<n;j++)
for(k=j+1;k<(n);k++)
if(a[j]>a[k])
t=a[j];
a[j]=a[k];
a[k]=t;
}
}
SORT3
QUESTION DESCRIPTION
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.
#include <stdio.h>
int main()
{
int t;scanf("%d",&t);
int i;int arr[t];
for(i=0;i<t;i++)
{ scanf("%d",&arr[i]);}
int n = sizeof(arr)/sizeof(arr[0]);
bubbleSort(arr, n);
printf("Sorted array:");
printArray(arr, n);
return 0;
}
SORT1
QUESTION DESCRIPTION
For each query, you are given an integer X, and you're supposed to find out if X is present in the array
A or not.
Input:
The first line contains two integers, N and Q, denoting the size of array A and number of queries.
The second line contains N space separated integers, denoting the array of elements Ai.
For each query, print YES if the X is in the array, otherwise print NO.
#include <stdio.h>
int main() {
int n,q,arr1[20],arr2[20],j,f,i;
scanf("%d %d",&n,&q);
for(i=0;i<n;i++)
scanf("%d",&arr1[i]);
for(j=0;j<q;j++)
scanf("%d",&arr2[j]);
for(j=0;j<q;j++)
for(i=0;i<n;i++)
if(arr2[j]==arr1[i])
{
f=1;
break;
else
f=0;
if(f==1)
printf("YES\n");
else
printf("NO\n");
return 0;
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 can buy a single candy from the store and get atmost K other candies ( all are different types )
for free.
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.
#include <stdio.h>
static void mergeSort(int a[],int l,int r)
{;
}
int main()
{
int t;
scanf("%d",&t);
while(t--)
{int i,j,k,l=0,n,m,a[1000],s=0,s1=0,min=0,max=0;
scanf("%d %d",&n,&k);
for(i=0;i<n;i++)
scanf("%d",&a[i]);
for(i=0;i<n;i++)
{for(j=0;j<n-i-1;j++)
{int tm;
if(a[j]>a[j+1])
{tm=a[j];
a[j]=a[j+1];
a[j+1]=tm;}}}
while(s<n)
{min=min+a[l];
l++;
s=s+k+1;}
j=n-1;
while(s1<n)
{max=max+a[j];
j--;
s1=s1+k+1;}
printf("%d ",min);
printf("%d\n",max);}
return 0;
}
SORT7
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.
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.
#include <stdio.h>
void result(int a[],int b[],int n);
int main()
{
int t, i, j, arr1[30], arr2[30], x, temp;
scanf("%d",&t);
while(t--)
{
scanf("%d",&x);
for(i=0;i<x;i++)
{
scanf("%d",&arr1[i]);
}
for(i=0;i<x;++i)
{
for (j=i+1;j<x;++j)
{
if (arr1[i] > arr1[j])
{
temp = arr1[i];
arr1[i] = arr1[j];
arr1[j] = temp;
}
}
}
for(i=0;i<x;i++)
{
scanf("%d",&arr2[i]);
}
for(i=0;i<x;++i)
{
for (j=i+1;j<x;++j)
{
if (arr2[i] < arr2[j])
{
temp = arr2[i];
arr2[i] = arr2[j];
arr2[j] = temp;
}
}
}
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.
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<=50TEST CASE 1
#include <stdio.h>
int main()
{
int t;
int n;
int a[30],i,j,c=0,k,p;
scanf("%d",&t);
for(i=0;i<t;i++)
scanf("%d",&n);
for(j=0;j<n;j++)
scanf("%d",&a[j]);
arrange_arr(n,a);
p=0;
// for(j=0;j<n;j++)
//{printf("%d ",a[j]);}
for(j=0,c=0;j<n-1;j++)
if((a[j+1]-a[j])==1)
{p++;}
else
if(p>0)
{c++;p=0;}
if(c>0)
c=c+1;
printf("%d\n",c);
}
return 0;
int j,k,t;
for(j=0;j<n;j++)
for(k=j+1;k<n;k++)
if(arr[j]>arr[k])
t=arr[j];
arr[j]=arr[k];
arr[k]=t;
Sort 5
QUESTION DESCRIPTION
Kalaiselvan is going to act as a car driver and he has to drive a car on a track divided into “N” no. of
sub-tracks.
You are also given the value of “K” i.e. the total kilometers a car can drive on each sub-track.
If the car can’t cover a sub-track, you can add any unit of Petrol in it. With each unit of petrol added,
the total kilometers your car can travel will increase by one unit .
Kalai selvan need to declar the mandatory function name as ” void sort(int a[],int n,int k)”
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 N and K.
The second line of each test case contains N space separated integers (A[]) denoting the distance of
each N sub-tracks.
Output:
For each test case in a new line you have to print out the minimum unit of Petrol your car require to
cover all the sub-tracks. If no extra unit of petrol is required, print -1.
#include <stdio.h>
int main()
int t,n,p,i,arr[20],temp,k,j;
scanf("%d",&t);
for(i=0;i<t;i++)
scanf("%d%d",&n,&p);
for(j=0;j<n;j++)
scanf("%d",&arr[j]);
for(j=0;j<n;j++)
for(k=j+1;k<n;k++)
if(arr[j]<arr[k])
{
temp=arr[j];
arr[j]=arr[k];
arr[k]=arr[j];
if(arr[0]>p)
printf("%d",arr[0]-p);
else
printf("-1");
printf("\n");
return 0;