0% found this document useful (0 votes)
3K views115 pages

Record

The document contains 7 programming problems and their solutions in C language. The problems include finding the maximum score in a chess game, converting data types, calculating surface area and side lengths, converting numbers to other number systems, and dividing items evenly among groups. The solutions provided utilize basic operations like input, output, arithmetic, and formatting output in C.

Uploaded by

Md Ashfaq
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
3K views115 pages

Record

The document contains 7 programming problems and their solutions in C language. The problems include finding the maximum score in a chess game, converting data types, calculating surface area and side lengths, converting numbers to other number systems, and dividing items evenly among groups. The solutions provided utilize basic operations like input, output, arithmetic, and formatting output in C.

Uploaded by

Md Ashfaq
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 115

lOMoARcPSD|29467424

Record

Programming For Problem Solving (SRM Institute of Science and Technology)

Studocu is not sponsored or endorsed by any college or university


Downloaded by Md Ashfaq ([email protected])
lOMoARcPSD|29467424

6/28/22, 5:45 PM Record

Question 1
Problem Description:
Binita is playing a chess. The game will be played on a rectangular grid consisting of N rows and M
columns. Initially all the cells of the grid are uncolored.

Binita's initial score is zero. At each turn, he chooses some cell that is yet not colored, and colors that
cell. The score obtained in this step will be number of neighboring colored cells of the cell that Binita
colored in this step.

Two cells are neighbors of each other if they share a side between them. The game will end when all the
cells are colored. Finally, total score obtained at the end of the game will sum of score obtained in each
turn.

Binita wants to know what maximum score he can get? Can you please help him in finding this out?

Constraints:
1 ≤ N, M ≤ 50

Input Format:
The Only line of input contains two space-separated integers N, M denoting the dimensions of the grid.

Output Format:
Print the output a single line containing an integer corresponding to the maximal possible score Binita
can obtain.

Program Code:

#include <stdio.h>
int main()
{
int n,m;
int a;
scanf("%d %d",&n,&m); a = 2*m*n-m-n;
printf("%d",a);
return 0;
}

Question 2
Problem Description:

On one beautiful Sunday Selvan went to Aaron's house for exam preparation.

They have decided to study Mathematics subject because they have exams by coming Monday, Aaron is
a master in Mathematics but Selvan is not so good in Mathematics so James trained with Selvan for
getting a high score in the exam.

After teaching some problems to Selvan.Aaron have given some tasks to Selvan to solve .

The problem is to convert input float into a double. Can you help Selvan in finding the solution ?

Constraints:
1.00 ≤ num1 ≤ 100.00
1/114
Downloaded by Md Ashfaq ([email protected])
lOMoARcPSD|29467424

6/28/22, 5:45 PM Record

1.00 ≤ num2 ≤ 100.00


1.00 ≤ resnum1 ≤ 100.00
1.00 ≤ resnum2 ≤ 100.00
Input Format:

The first and second line of the input represents two different input value of type float.
Output Format:

The first and second line of the output represents outputs of first and second line of input of type double.

Program Code:

#include <stdio.h>
int main()
{
float num1,num2;
double resnum1,resnum2;
scanf("%f",&num1);
scanf("%f",&num2);
scanf("%lf",&resnum1);
scanf("%lf",&resnum2);
printf("\n%lf",num1);
printf("\n%lf",num2);

return 0;
}

Question 3
Problem Description:

Selvan was playing with the a object of random size for stress relief.

Selvan knows that the Length, Width, and Height of the object.

But he would like to know the surface area of the object he is playing with.
Can you help him in finding it?

Functional Description:
Surface area of the Object = 2 x [width x length + length x height + height x width]
Constraints:
1 ≤ length≤20
1 ≤ width ≤ 20
1 ≤ height ≤ 20

Input Format:

First Line : Length of the object in Integer.

Second Line : Width of the object in Integer

Third Line : Height of the object in Integer

2/114
Downloaded by Md Ashfaq ([email protected])
lOMoARcPSD|29467424

6/28/22, 5:45 PM Record

Output Format:

Print a single integer value representing the surface area of the object selvam is playing with.

Program Code:

#include <stdio.h>
int main()
{
int length,width,height,surfacearea;
scanf("%d%d%d",&length,&width,&height);
surfacearea = (2*((width*length)+(length*height)+(height*width)));
printf("%d",surfacearea);

return 0;
}

Question 4
Problem Description:

During the IPL Match between CSK and MI, as a part of IPL contest the question was asked to the fans.

Who are all giving the correct answer to that question will get the free VIP box ticket for the Final for
which CSK have already qualified .

The question is convert given integer number to octal and hexadecimal number respectively.

Abilash is an die heart CSK fan. Can you help him answer the question so that he can watch CSK play
the final from VIP box?

Constraints:

1≤iplno≤10000

Input Format:

Only line of input has single integer number that need to be converted.

Output Format:

In the First line of output print the octal number equivalent to the input value.

In the Second line of output print the hexadecimal number equivalent to the input value.

Program Code:

#include <stdio.h>
int main()
{
int iplno;
scanf("%d",&iplno);
printf("%o",iplno);
printf("\n%x",iplno);
return 0;
}
3/114
Downloaded by Md Ashfaq ([email protected])
lOMoARcPSD|29467424

6/28/22, 5:45 PM Record

Question 5
Problem Description:

Professor JD has lots of options. Bottles containing all types of potions are stacked on shelves which
cover the entire wall from floor to ceiling.

Professor JD has broken his bones several times while climbing the top shelf for retrieving a potion. He
decided to get a ladder for him.

But he has no time to visit Charu. So he instructed Bargav to make a ladder for him. Professor JD
specifically wants a step ladder that looks like an inverted 'V' from a side view.

Professor just mentioned two things before vanishing-

B - separation between left side (LS) and right side (RS) on the ground

LS - the length of left side

What should be the length of RS? At one extreme LS can be vertical and at other RS can be vertical.

Bargav is angry and confused.

Can you help him find the minimum and maximum length of RS.

Constraints

1 ≤ B < LS ≤ 100

Input Format:

Only line of input contains2 integers representing B and LS respectively.

Output Format:

The only ;line of output contains minimum value of RS and maximum value of RS, separated by space.

The answer (RS) will be considered correct if it has relative and absolute error less than 10-2.

Program Code:

#include <stdio.h>
#include<math.h>
int main(){
float b,ls,rs1,rs2;
scanf("%f%f",&b,&ls);
b=b*b;
ls=ls*ls;
rs1=sqrt(ls-b);
rs2=sqrt(ls+b);
printf("%.5f %.5f",rs1,rs2);
return 0;}

Question 6
Problem Description:
4/114
Downloaded by Md Ashfaq ([email protected])
lOMoARcPSD|29467424

6/28/22, 5:45 PM Record

Elavenil has a chessboard with N rows and M columns. In one step, he can choose two cells of the
chessboard which share a common edge (that has not been cut yet) and cut this edge.

Formally, the chessboard is split into two or more pieces if it is possible to partition its cells into two non-
empty subsets S1 and S2 (S1∩S2=∅, |S1|+|S2|=NM) such that there is no pair of cells c1,c2
(c1∈S1,c2∈S2) which share a common edge that has not been cut.

Elavenil does not want the board to split into two or more pieces. Compute the maximum number of
steps he can perform while satisfying this condition.

Constraints:

1≤N,M≤8

Input Format:

The only line of input test case contains two space-separated integers N and M.

Output Format:

In the only line of output print an integer representing the maximum possible number of steps.

Program Code:

#include <stdio.h>
int main()
{int n,m; int c;
scanf("%d%d",&n,&m);
c=(m-1)*(n-1);
printf("%d",c);
return 0;}

Question 7
Problem Description:
Ramesh is working in an engineering college hostel as a Mess supervisor. There are different messes are
available based on the years.

Every day students count is varying in all the hostels due to continuous holidays.

Since ramesh is in charge of the cooking team. He had trouble with calculating the quantity of food that
needs to be prepared because of the varying student count.

Even if a small quantity of food is prepared by the cooking team, it should be divided equally among the
number of Mess.Ramesh needs an automated software to identify the amount of food available (in
number of packets ) and Mess count.

Can you help him to divide the food equally and also calculating the remaining quantity of food that will
be available after sharing the food equally ?

Constraints:
1≤ alvqntoffood ≤10000
1≤ messcnt ≤20

5/114
Downloaded by Md Ashfaq ([email protected])
lOMoARcPSD|29467424

6/28/22, 5:45 PM Record

Input Format:
Only line of input has two integers (alvqntoffood, messcnt) separated by space representing the available
number of food packets and the available number of messes respectively

Output Format:

In the only line of output print two values separated by a space representing the number of food packets
that are equally shared by "n" number of messes and the remaining number of food packets available.

Program Code:

#include <stdio.h>
int main()
{
int alvqntoffood,messcnt,dividedqnt,remfood;
scanf("%d",&alvqntoffood);
scanf("%d",&messcnt);
dividedqnt=alvqntoffood/messcnt;
remfood=alvqntoffood%messcnt;
printf("%d %d\t",dividedqnt,remfood);

return 0;
}

Question 8
Problem Description:

Rathik organized technical round interview in Macrosoft for the set of computer science candidates.

The problem is to perform addition, subtraction, multiplication, and division of given two numbers.

Rathik have given the deadline of only 5 minutes to complete the problem.

Can you Help the candidates to complete the problem within the specified time limit ?

Constraint:

1 ≤ testnum1 ≤ 50

1 ≤ testnum2 ≤ 50

Input Format :

The only line of input has two numbers a and b of type integers separated by a comma.

Output Format:

Print Addition, Subtraction, Multiplication, Division, and Modulus of given two numbers in a separate
line respectively.

Note: Rathik instructed his candidates to print the result of the division with 3 values after decimal
point.

6/114
Downloaded by Md Ashfaq ([email protected])
lOMoARcPSD|29467424

6/28/22, 5:45 PM Record

Program Code:

#include <stdio.h>
int main()
{
int testnum1,testnum2;
int sum,sub,mult,mod;
float div;
scanf("%d",&testnum1);
scanf("%d",&testnum2);
sum=testnum1+testnum2;
sub=testnum1-testnum2;
mult=testnum1*testnum2;
div=(float)testnum1/(float)testnum2;
mod=testnum1%testnum2;
printf("\nAddition : %d",sum);
printf("\nSubtraction : %d",sub);
printf("\nMultiplication : %d",mult);
printf("\nDivision : %0.3f",div);
printf("\nModulus : %d",mod);

return 0;
}

Question 9
Problem Description:

Phoenix mall in the capital city of Washington and it is rectangular in shape when it is seen on the map
with the size n x m meters.

On the occasion of the jubilee anniversary, a decision was taken to pave the Square with square marbles
stones. Each stone is of the size n × n.

Can you what is the least number of stones needed to pave the Square?

It's allowed to cover the surface larger than the Mall Square, but the Square has to be covered.

It's not allowed to break the stones. The sides of stones should be side by side(parallel) to the sides of the
Square.

Constraints:

1 ≤ n ≤ 10^9

1 ≤ m ≤ 10^9

1 ≤ a ≤ 10^9

Input Format:

The only line of input contains three positive integer numbers n, m and a separated by a space .

Output Format:

7/114
Downloaded by Md Ashfaq ([email protected])
lOMoARcPSD|29467424

6/28/22, 5:45 PM Record

Print the needed number of stones.

Program Code:

#include <stdio.h>
int main()
{
int n,m,a;
int s;
scanf("%d %d %d",&n,&m,&a);
s=((n+a-1)/a)*((m+a-1)/a);
printf("%d",s);
return 0;
}

Question 10
Problem Description:

Sajid was booking a train ticket from Chennai to Delhi for his family. Two of the relatives was interested
in joining that journey from different places with their family members

So, Sajid booked tickets for those persons also along with his family members.

He wants to know the total number of tickets for this travel.

Can you help him in finding the total number of passengers?

Constraint:
Sajid has to declare three integer variables named as num1, num2, num3.

1 ≤ num1 ≤ 15

1 ≤ num2 ≤ 15

1 ≤ num3 ≤ 15

Input Format:

Only Line of input has three integers num1,num2 and num3 separated by a space representing the
numbers of ticket booked by Sajid at three different interval of time.

Output Format:

Print the total number of tickets booked by Sajid.

Program Code:

#include <stdio.h>
int main()
{
int num1,num2,num3;
int sum;
8/114
Downloaded by Md Ashfaq ([email protected])
lOMoARcPSD|29467424

6/28/22, 5:45 PM Record

scanf("%d %d %d",&num1,&num2,&num3);
sum=num1+num2+num3;
printf("%d",sum);

return 0;
}

Question 11
Problem description

Pari is an Architect who is currently doing his design work for his new project in one of the congested
location of Paris.For making his work simpler he is looking for the automated tool which check whether
the area is greater or perimeter is greater or both are equal if the Length (L) and Breadth (B) of a
rectangle is provided.

Since he is not from the computing background he is looking for the freelancer who can do this for him?

Can you help him with the logic for doing so?

Constraints:

1≤L≤1000

1≤B≤1000

Input Format:

First line will contain the length (L) of the rectangle.

Second line will contain the breadth (B) of the rectangle.

Output Format:

Output 2 lines.

In the first line print "Area" if area is greater otherwise print "Peri" and if they are equal print "Eq".
(Without quotes).

In the second line print the calculated area or perimeter (whichever is greater or anyone if it is equal).

Program Code:

#include <stdio.h>
int main()
{
int l,b,area,peri;
scanf("%d %d",&l,&b);
area=l*b;
peri=2*(l+b);
if(area>peri)
{
printf("Area\n");
printf("%d",area);
}
else if(peri>area)
9/114
Downloaded by Md Ashfaq ([email protected])
lOMoARcPSD|29467424

6/28/22, 5:45 PM Record

{
printf("Peri\n");
printf("%d",peri);
}
else
{
printf("Eq\n");
printf("%d",peri);
}
return 0;
}

Question 12
Problem Description:
Tharun wants to print a document with "N" pages double-sided, where two pages of data can be printed
on one sheet of paper.
Can to tell him for printing N pages at least how many sheets of paper does he need?

Constraints:
N is an integer.
1 ≤ N ≤ 1000

Input Format:
Only line of input has single integer N representing the number of pages need to be printed.

Output Format:
Print the output in a single line

Program Code:

#include <stdio.h>
int main()
{
int a;
scanf("%d",&a);
if(a%2==0){printf("%d",a/2);}
else{printf("%d",(a/2)+1);}
return 0;
}

Question 13
Problem Description:
Anegan is a member of a programming competition site, Awesome Coder.

Each member of the Awesome Coder is assigned two values: Inner Rating and Displayed Rating.

The Displayed Rating of a member is equal to their Inner Rating if the member has participated in 10 or
more contests.

Otherwise, the Displayed Rating will be their Inner Rating minus 100 × (10−K) when the member has
participated in K contests.

10/114
Downloaded by Md Ashfaq ([email protected])
lOMoARcPSD|29467424

6/28/22, 5:45 PM Record

Anegan has participated in N contests, and his Displayed Rating is R.

Find his Inner Rating.

Constraints:
All values in input are integers
1 ≤ N ≤ 100
0 ≤ R ≤ 4111

Input Format:
Only line of input has two values N R of type integer separated by a space.

Output Format:
Print the Inner Rating.

Program Code:

#include <stdio.h>
int main()
{
int n,r;
scanf("%d %d",&n,&r);
if(n>=10){printf("%d",r);}
else{printf("%d",r+100*(10-n));}
return 0;
}

Question 14
Problem Description:
Vishal is fighting with a monster.The health of the monster is H.In one attack, Vishal can decrease the
monster's health by 'A'.

There is no other way to decrease the monster's health.Vishal wins when the monster's health becomes '0'
or below.
Can you find the number of attacks needed for Vishal for winning ?
Constraints:
1 <= H <= 10^4
1 <= A <= 10^4
All values in input are integers.

Input Format:
Only line of input has two space separated integers H A

Output Format:
Print the number of attacks Vishal needs to make before winning.

Program Code:

#include <stdio.h>
int main()
{
int a,b;
scanf ("%d %d",&a, &b);
11/114
Downloaded by Md Ashfaq ([email protected])
lOMoARcPSD|29467424

6/28/22, 5:45 PM Record

if (a%b==0)
{
printf("%d",a/b);
}
else if(a/b!=0)
{
printf("%d",(a/b)+1);
}
return 0;
}

Question 15
Problem Description:

Ram went to the bank to withdraw his cash.

But he has a constraint in that.

If he gives the amount he need to withdraw the bank need to provide him the minimum number of notes
required for the amount (Rs. 500, 100, 50, 20, 10, 5, 2, 1) because he has the smaller sized wallet to keep
the withdrawn cash

Constraints:

50≤amount≤32000

Input Format:

Only line of input containing a single integer representing a the amount to withdraw.

Output Format:

In the only line of output print the minimum required combination of notes.

Program Code:

#include <stdio.h>
int main()
{
int note500,note100,note50,note20,note10,note5,note2,note1;
note500=note100=note50=note20=note10=note5=note2=note1=0;
int amount;
scanf(" %d ",&amount);
if(amount>=500)
{
note500=amount/500;
amount=amount%500;
}
if(amount>=100)
{
note100=amount/100;
amount=amount%100;
}
if(amount>=50)
12/114
Downloaded by Md Ashfaq ([email protected])
lOMoARcPSD|29467424

6/28/22, 5:45 PM Record

{
note50=amount/50;
amount=amount%50;
}
if(amount>=20)
{
note20=amount/20;
amount=amount%20;
}
if(amount>=10)
{
note10=amount/10;
amount=amount%10;
}
if(amount>=5)
{
note5=amount/5;
amount=amount%5;
}
if (amount>=2)
{
note2=amount/2;
amount=amount%2;
}
if(amount>=1)
{
note1=amount/1;
amount=amount%1;
}
printf("500:%d\n",note500);
printf("100:%d\n",note100);
printf("50:%d\n",note50);
printf("20:%d\n",note20);
printf("10:%d\n",note10);
printf("5:%d\n",note5);
printf("2:%d\n",note2);
printf("1:%d\n",note1);
return 0;
}

Question 16
Problem Description:

Abi and Jannu are off to the wedding of a close relative. This time they have to travel without their
guardians. Abi got very interested in the arrangement of seats inside the train coach.

The entire coach could be viewed as an arrangement of consecutive blocks of size 8.

Berth Number Compartment

1 - 8 1

13/114
Downloaded by Md Ashfaq ([email protected])
lOMoARcPSD|29467424

6/28/22, 5:45 PM Record

9 - 16 2
17 - 24 3
... and so on

Each of these size-8 blocks are further arranged as:

1LB, 2MB, 3UB, 4LB, 5MB, 6UB, 7SL, 8SU


9LB, 10MB, ...
...
...

Here LB denotes lower berth, MB middle berth and UB upper berth.

The following berths are called Train-Partners:

3UB | 6UB
2MB | 5MB
1LB | 4LB
7SL | 8SU

and the pattern is repeated for every set of 8 berths.

Abi and Jannu are playing this game of finding the train partner of each berth. Can you write a program
to do the same?

Constraints:

1 ≤ N ≤ 500

Input Format:

Only Line of input contain a single integer N, the berth number whose neighbor is to be found out.

Output Format:

Only line of output contain the berth of the neighbor of the corresponding seat.

Program Code:

#include <stdio.h>
int main()
{
int n;
scanf("%d",&n);
if(n%8==1)
printf("%dLB",n+3);
else if(n%8==2)
printf("%dMB",n+3);
else if(n%8==3)
printf("%dUB",n+3);
else if(n%8==7)
printf("%dSU",n+1);
else if(n%8==0)
printf("%dSL",n-1);
else if(n%8==4)
14/114
Downloaded by Md Ashfaq ([email protected])
lOMoARcPSD|29467424

6/28/22, 5:45 PM Record

printf("%dLB",n-3);
else if(n%8==5)
printf("%dMB",n-3);
else if(n%8==6)
printf("%dUB",n-3);
return 0;
}

Question 17
Problem Description:
Given is an integer N.Simon chooses an integer 'a' from the positive integers not greater than 'N' with
equal probability.

Find the probability that 'a' is odd.

Constraints:
1 <= N <= 100

Input Format:
Input is given from Standard Input in the following format: N

Output Format:
Print the probability that 'a' is odd.

Your output will be considered correct when its absolute or relative error from the judge's output is at
most 10^-6.

Explanation:

Assume N=4 then

There are four positive integers not greater than 4: 1, 2, 3, and 4. Among them, we have two odd
numbers: 1 and 3. Thus, the answer is 2/4=0.5.

Program Code:

#include <stdio.h>
int main()
{int n; float t;
scanf ("%d",&n);
if(n%2==0)
{printf("%d",(n/2)/n);}
else
{t=n/2;
t=(t+1)/n;
printf("%.9f",t);}
return 0;}

Question 18
Problem Description:
In the Attacking war game Amit and Arun will have a battle using their monsters.

15/114
Downloaded by Md Ashfaq ([email protected])
lOMoARcPSD|29467424

6/28/22, 5:45 PM Record

The health and strength of Amit's monster are A and B, respectively, and those of Arun's monster are C
and D, respectively.

The two monsters will take turns attacking, in the order Amit's, Arun's, Amit's, Arun's, ... Here, an attack
decreases the opponent's health by the value equal to the attacker's strength.

The monsters keep attacking until the health of one monster becomes 0 or below.

The person with the monster whose health becomes 0 or below loses, and the other person wins.

Constraints:
1 ≤ A, B, C, D ≤ 50

Input Format:

Only line of input has 4 integers A B C and D separated by a space representing the strengths of Amit
and Arun's monsters.

Output Format:
In the only line of If Amit will win, print Yes; if he will lose, print No.

Program Code:

#include <stdio.h>
int main()
{
int a,b,c,d;
scanf("%d %d %d %d", &a, &b, &c, &d);
if((a+b)>=(c+d)){printf("Yes");}
else printf("No");
return 0;
}

Question 19
Problem Description:

While purchasing certain items, a discount of 10% is offered by the popular super market if the quantity
purchased is more than 1000.

Since number of people purchasing is increasing day by day the owner of the super market feels that the
discount calculation will be better if there is a automated software that gives the total expenses If the
quantity and price per item are input.

Constraints:

1 ≤ quantity,price ≤ 100000

Input Format:

Only line of input has two integers separated by a space representing quantity and price respectively.

Output Format:

In the only line of the output print the total expenses of the purchased items.
16/114
Downloaded by Md Ashfaq ([email protected])
lOMoARcPSD|29467424

6/28/22, 5:45 PM Record

Program Code:

#include <stdio.h>
int main()
{int price,quantity,totexp;
scanf("%d %d",&quantity,&price);
if(quantity>1000)
{totexp=(float) quantity*(price-(price*0.1));
printf("%d",totexp);}
else{printf("%d",price*quantity);}
return 0;
}

Question 20
Problem Description:

The Election Commission of India distributed the voter ID to all eligible citizens.

But Amira didn't received a Voter ID on time.

So, she gets confused about her eligibility for voting?

Can you clarify her doubt?

Condition for Eligibility as per Election Commission of India is

(i) Eligible if age >=18

(i) Not Eligible if age <18

Constraints :
1≤age≤100
Input Format:

The only line of input has single value of type integer representing age.

Output Format:

Print as Eligible or Not Eligible based on the eligibility criteria in a single line.

Program Code:

#include <stdio.h>
int main()
{
int age;
scanf("%d",&age);
if(age<18)
{
printf("Not Eligible");
}
else
{
17/114
Downloaded by Md Ashfaq ([email protected])
lOMoARcPSD|29467424

6/28/22, 5:45 PM Record

printf("Eligible");
}
return 0;
}

Question 21
Problem Description:

Yogesh booked the ticket and went for the magic show with his partner.

Since the magician is popular among general public both married couples and the unmarried youths will
come to his show.

In order to make it convenient for both the set of audience the show organisers have given the seating
arrangement instruction for couples and singles individually.

According to the instruction couples have to sit in the even numbered seats and singles have to sit in the
odd numbered seats.

For better positioning of seats the event organisers wold like to develop a seating arrangement software
which will print the layout if the total number of rows for the show is provided.

Constraints:
1 ≤ noofrows ≤ 20

Input Format:
Only line of input has single integer representing the number of rows of seats for the particular day of the
show.

Output Format:

Print the seating layout according to the number of rows provided.

Refer sample testcases for format specification.

Program Code:

#include <stdio.h>
int main()
{
int noofrows;
int a,b;
scanf("%d",&noofrows);
for(a=0;a<noofrows;a++){
for(b=0;b<=a;b++)
{
printf("%d " , a+1);
}
(printf("\n"));
}
return 0;
}

18/114
Downloaded by Md Ashfaq ([email protected])
lOMoARcPSD|29467424

6/28/22, 5:45 PM Record

Question 22
Problem Description:
Once N Men and M Women attended a matrimonial event.

The event is represented by a matrix named "a" of N rows and M columns where Aij is 1 if the i-th Men
likes the j-th Women.

Otherwise it will be 0.

Note that it is not necessary that if a Men x likes Women y, then Women y should like Men x.

If there are two different Men x and y, who both like Women z, then there will be a collision.

Can you calculate the number of different collisions in the matrimonial event?

Note that order of Men in the collision doesn't matter.

Constraints
1 ≤ T ≤ 100
1 ≤ N, M ≤ 10

Input Format:
The first line contains a single integer T denoting the number of test cases. Then T test cases follow.

The first line of each test case contains two space separated integers N, M denoting the number of Men
and Women, respectively.

Each of the following N lines contain M characters, each of them is either '0' or '1'

Output Format:
Print the output a single line containing an integer corresponding to the number of in the matrimonial
event.

Program Code:

#include <stdio.h>
#include <string.h>
int main()
{
int t,men,women,collisions=0;
scanf("%d",&t);
scanf("%d %d\n",&men,&women);
int i,sum=0;
char a[men][women];
for(i=0;i<men;i++)
for(t=0;t<women;t++)
scanf("%c ",&a[i][t]);
for(i=0;i<women;i++)
{
for(t=0;t<men;t++)
if(a[t][i]=='1')
collisions+=1;
sum+=collisions*(collisions-1)/2;
collisions=0;
19/114
Downloaded by Md Ashfaq ([email protected])
lOMoARcPSD|29467424

6/28/22, 5:45 PM Record

}
printf("%d",sum);
return 0; }

Question 23
Problem Description:
After setting up the area and his teddy bears. Valavan is up for playing his very first game.

His first game is played on a N X N board with some initial sticks placed on each cell.

He can move each stick in all four direction i.e up,down, left or right.

His target of the game is to move all sticks to any one of the four corners in minimum moves (different
sticks can be moved to different corners).

Valavan seeks you again for his help.

Your task is to write a program that tell the minimum number of moves given a particular state of board.

NOTE: One cell may contain more than one sticks.

Constraints:
1 <= N <= 10^6
1 <= k <= 10^5

Input Format:
First line of input contains two integers N and K.

Where, N X N is the size of the board and k is the no of sticks present on the board.

Then, two lines(x and y) follow each containing K integers where (ith) integer of first line and (ith)
integer of second line represent the x-coordinate and y-coordinate of (ith) stick respectively.

In short, ((x[i]), (y[i])) is where (ith) stick is placed at.

Output Format:
Print the minimum number of moves required to move all stick to the corners.

Program Code:

#include<stdio.h>
int x[100],y[100],u[100],v[100];
int main() {
int k, n, m; long long a = 0;
int i;
scanf("%d %d",&n,&k);
m = n-1;
for ( i = 0; i < k; ++i)
scanf("%d",x+i), --x[i], u[i] = m-x[i];
for ( i = 0; i < k; ++i)
scanf("%d",y+i), --y[i], v[i] = m-y[i];
for ( i = 0; i < k; ++i)
if (x[i] < u[i])
a += x[i];
20/114
Downloaded by Md Ashfaq ([email protected])
lOMoARcPSD|29467424

6/28/22, 5:45 PM Record

else
a += u[i];
for ( i = 0; i < k; ++i)
if (y[i] < v[i])
a += y[i];
else
a += v[i];
printf("%lld",a);
return 0;
}

Question 24
Problem Description:
Ambikapathy wants to decorate his girlfriend Amaravathi's house with a series of lights.

The number of lights is M. Initially, all the lights are switched off.

Ambikapathy finds that there are K numbers of buttons, each of them is connected to a set of lights.

Ambikapathy can press any of these buttons. When the button is pressed, it turns on all the lights it’s
connected to. Can Ambikapathy accomplish his task to switch on all the lights?

If Ambikapathy presses the button such that some lights connected to it are already turned on, they do not
change their state, i.e. remain turned on.

Help him in his task to impress his valentine.

Here lights are numbered from 1 to M.

Constraints:
1 ≤ M,K ≤ 100
0 ≤ Xi ≤ M
1 ≤ Yij ≤ M

Input Format:
The First line of Input contains integers K and M- the numbers of buttons and lights respectively.

Each of the next K lines contains Xi - the number of lights that are turned on by the i-th button, and then
Xi numbers Yij — the numbers of these bulbs.

Output Format:
If it's possible to turn on all m bulbs print "YES", otherwise print "NO" (quotes for clarity)

Program Code:

#include<stdio.h>
#include<stdbool.h>
int main()
{
int k,m,i=0,j=0;
scanf("%d %d",&k,&m);
int lights[m];
for(i=1;i<=m;i++)
{
21/114
Downloaded by Md Ashfaq ([email protected])
lOMoARcPSD|29467424

6/28/22, 5:45 PM Record

lights[i]=0;
}
while(k--)
{
int X;
scanf("%d",&X);
int arr2[X];
for(i=1;i<=X;i++)
{ scanf("%d",&arr2[i]); }
for(i=1;i<=m;i++)
{
for(j=1;j<=X;j++)
{
if(arr2[j]==i)
{
lights[i]++;
}
} }
}
bool flag=true;
for(i=1;i<=m;i++)
{
if(lights[i]==0)
{
flag=false;
break;
}
else
{ flag=true; }
}
if(flag==true)
{
printf("YES\n");
}
else if(flag==false)
{
printf("NO\n");
}
return 0; }

Question 25
Problem Description:

Mukesh and Salima was looking to buy Organic Apples in the nearest fruits shop.

But Shop Owner mixed the Hybrid apples in that Apple box.

After sometimes Mukesh checking the box, he was confused among organic and hybrid apple.

He started to count the hybrid apples.

Please help to Mukesh Count the total number of hybrid apples in the box.

22/114
Downloaded by Md Ashfaq ([email protected])
lOMoARcPSD|29467424

6/28/22, 5:45 PM Record

Constraints:
1<= numofapples<=50

Functional Description:

If two apples are given the same number then one of them is hybrid.Likewise count the number of hybrid
apples.
Input Format:

First line contains the positive integer n representing the number of apples.

Second line of the input contains the unique integer representing the apple.

Output Format:

Print the number of hybrid apples in the box.

Program Code:

#include <stdio.h>
int main()
{
int numofapples, x, i, c=0;
scanf("%d", &numofapples);
int arr[100]={0};
for(i=0; i<numofapples; ++i)
{
scanf("%d", &x);
arr[x]++;
}
for(i=0; i<100; ++i)
{
if(arr[i]>1) c += (arr[i]-1);
}
printf("%d", c);
return 0;
}

Question 26
Problem Description:

Fazil loves to preform different operations on arrays, and so being the Head of the higher education
institution, he assigned a task to his new student Rohan.

Rohan will be provided with an integer array A of size N and an integer K , where she needs to rotate the
array in the right direction by K steps and then print the resultant array.

As she is new to the school, please help her to complete the task.

Constraints:

1 <= T <= 20

1 <= N <= 10^5


23/114
Downloaded by Md Ashfaq ([email protected])
lOMoARcPSD|29467424

6/28/22, 5:45 PM Record

0 <= K <= 10^6

0 <= A[i] <= 10^9

Input Format:

The first line will consists of one integer T denoting the number of test cases.
For each test case:
The first line consists of two integers N and K, N being the number of elements in the array and K
denotes the number of steps of rotation.
The next line consists of N space separated integers , denoting the elements of the array A.

Output Format:

Print the array after rotation in the single line for each test case.

Program Code:

#include <stdio.h>
int main()
{
int rot[100000];int n,k,j,i,t,tc;
scanf("%d",&tc);
while(tc--)
{
scanf("%d%d",&n,&k);
for(j=0;j<n;j++)
scanf("%d ",&rot[j]);
for(j=0;j<k;j++)
{
t=rot[n-1];
for(i=n-1;i>0;i--)
rot[i]=rot[i-1];
rot[i]=t;
}
for(i=0;i<n;i++)
printf("%d ",rot[i]);

printf("\n");
}
return 0;
}

Question 27
Problem Description:

Let's consider a triangle of numbers in which one number appears in the first line, two numbers appear in
the second line, three in the third line, etc. Develop a logic which will compute the largest of the sums of
numbers that appear on the paths starting from the top towards the base, so that:

1. On each path the next number is located on the row below, more precisely either directly below or
below and one place to the right;
2. The number of rows is strictly positive, but less than 100
3. All numbers are positive integers between 0 and 99.
24/114
Downloaded by Md Ashfaq ([email protected])
lOMoARcPSD|29467424

6/28/22, 5:45 PM Record

Constraints:
1<=n<=1000

Input Format:
First line of testcase has integer n representing the number of test cases.

Then n test cases follow.

Each test case starts with the number of lines which is followed by their content.

Output Format:
Print the output result in a separate line.

Program Code:

#include <stdio.h>
int main()
{
int t,n,i,j;
scanf("%d",&t);
while(t--)
{
scanf("%3d",&n);
int ar[n][n];
for(i=0;i<n;i++)
{
for(j=0;j<=i;j++)
scanf("%3d",&ar[i][j]);
}
for(i=n-1;i>=0;i--)
{
for(j=0;j<i;j++)
{
if(ar[i][j]>ar[i][j+1])
ar[i-1][j]+=ar[i][j];
else
ar[i-1][j]+=ar[i][j+1];
}
}
printf("%d\n",ar[0][0]);
}
return 0;
}

Question 28
Problem Description:
The hero of this story is Johan.

Inspired by the legendary competitive coder Hardik, Johan has also started preparing to race to the top of
the ranks.

Johan is going to practice N different problems in the exact given order over the next M days. For each

25/114
Downloaded by Md Ashfaq ([email protected])
lOMoARcPSD|29467424

6/28/22, 5:45 PM Record

problem, he writes down the amount of time 'qi' he will take to think and code the 'ith' problem (He is
quite good at estimating!).

Before starting on the problems, he took advice from experienced competitive programmers on his
practice routine and almost all of them advised him to keep his daily load at the minimum possible and
avoid overtraining.

Since Johan already has N problems to solve, he asks you to find the minimum time T such that training
every day for a time "ti<=T" is sufficient to solve all the N problems in M days.

Note :

Unlike in the real world, you cannot think about a problem one day and solve it on the other day. You
need to do it on the very same day!

Constraints:
1 <= N <= 10^5
1 <= M <= N
1 <= qi <= 10^12

Input Format:
The first line contains two space-separated integers N and M.

The next line contains N space-separated integers denoting the time "qi" required to solve the "ith"
problem.

Output Format:
The output consists of one integer, the minimum time T as described in the problem statement.

Program Code:

#include <stdio.h>
#include <stdbool.h>
bool check(int arr[],int n,int m,long long mid)
{
int days=0,i;
long long temp=0;
for(i=0;i<n;i++)
{if(temp+arr[i]>mid){
temp=0;
days++;
temp+=arr[i];}
else{
temp+=arr[i];}
}
return days<=m-1&&temp<=mid;
}
int main()
{
int n,m,a,mintime=0;
scanf("%d %d",&n,&m);
int timetosolve[100005];
for(a=0;a<n;a++){
scanf("%d",&timetosolve[a]);
if(timetosolve[a]>mintime){
26/114
Downloaded by Md Ashfaq ([email protected])
lOMoARcPSD|29467424

6/28/22, 5:45 PM Record

mintime=timetosolve[a];
}
}
long long lo=mintime,hi=1e10,mid;
while(hi-lo>1)
{
mid=(hi+lo)/2;
if(check(timetosolve,n,m,mid))
{
hi=mid;
}
else
{
lo=mid+1;
}}
lo=check(timetosolve,n,m,lo)?lo:hi;
printf("%lld\n",lo);

return 0;}

Question 29
Problem Description:

Hamid likes all arrays equally. But he likes some arrays more equally than others. In particular, he loves
Rainbow Arrays.

An array is Rainbow if it has the following structure:

First a1 elements equal 1.

Next a2 elements equal 2.

Next a3 elements equal 3.

Next a4 elements equal 4.

Next a5 elements equal 5.

Next a6 elements equal 6.

Next a7 elements equal 7.

Next a6 elements equal 6.

Next a5 elements equal 5.

Next a4 elements equal 4.

Next a3 elements equal 3.

Next a2 elements equal 2.

Next a1 elements equal 1.


27/114
Downloaded by Md Ashfaq ([email protected])
lOMoARcPSD|29467424

6/28/22, 5:45 PM Record

ai can be any non-zero positive integer.

There are no other elements in array.

Help Hamid in finding out if the given array is a Rainbow Array or not.

Constraints:

1 ≤ T ≤ 100

7 ≤ N ≤ 100

1 ≤ Ai ≤ 10

Input Format:

The first line of the input contains an integer T denoting the number of test cases.

The first line of each test case contains an integer N, denoting the number of elements in the given array.

The second line contains N space-separated integers A1, A2, ..., AN denoting the elements of array.

Output Format:

For each test case, output a line containing "yes" or "no" (without quotes) corresponding to the case if the
array is rainbow array or not.

Program Code:

#include<stdio.h>
int main()
{
int T;
int rainbowcheck[7];

scanf("%d",&T);
while(T--){
int N,i;
scanf("%d",&N);
int A[N];
for(i=0;i<N;i++) scanf("%d",&A[i]);

int check = 1 ;
int head = 0 ;
int tail = N -1 ;
if(A[0] != 1 && A[N-1] != 1){
printf("no");
continue ;
}

while(head < tail ){


if(A[head] != A[tail]){
check= 0 ;
break ;
28/114
Downloaded by Md Ashfaq ([email protected])
lOMoARcPSD|29467424

6/28/22, 5:45 PM Record

}
else if (A[head] != A[head+1] && A[head+1] !=A[head] +1 ){
check = 0 ;
break ;
}
head++ ; tail-- ;

}
if(A[head] == 7 && check ==1){
printf("yes\n");
}
else{
printf("no\n");
}
}
return 0;
printf("%d", rainbowcheck[0]);
}

Question 30
Problem Description:
Steve Waugh and Mark Waugh are sitting at the bottom of their treehouse and debating in how many
ways then can jump up the stairs on the tree and reach the treehouse.

Because Steve Waugh is older than Mark Waugh, he can jump one, two, or three stairs in one step while
Mark Waugh can jump just one or two stairs in one step.

Constraints:
1<= stair <=8

Input Format:
The number of stairs to be covered.

Output Format:
Print the Number of total ways to reach to the top for Steve Waugh in the first line and Number of total
ways to reach at the top for Mark Waugh in the second line.

Program Code:

#include <stdio.h>
int i;
int main()
{ int markwaugh,stevewaugh,n;
scanf("%d",&n);
int arr[n+1];
arr[0] = 1;
arr[1] = 1;
arr[2] = 2;
for (i = 3; i <=n; i++)
arr[i] = arr[i - 1] + arr[i - 2]+ arr[i - 3];
stevewaugh=arr[n];
for(i=2;i<=n;i++)
arr[i]=arr[i - 1] + arr[i - 2];
29/114
Downloaded by Md Ashfaq ([email protected])
lOMoARcPSD|29467424

6/28/22, 5:45 PM Record

markwaugh=arr[n];
printf("Steve Waugh:%d\nMark Waugh:%d",stevewaugh,markwaugh);
return 0;
}

Question 31
Problem Description:

Janu and Ram are close friends who task a lot about life.

They go though a lot of inspiring "Quotes of Life".

One fine day they had a small game.According to the game Ram will Read one of the Quote about life
from the book and Jannu have to think a word about life in her mind without disclosing it to Ram.

Finally once Ram completed reading the quoted Jannu will say if the word she thought in her mind is
there in the Quote read by Ram.

Can you convert the same scenario to a programming logic ?

If the work thought by Jannu was present in the Quote then you have to print "Exists" else print "Dosen't
Exists".

Constraints:

1 ≤ T ≤ 30
1 ≤ |s| ≤ 100

Input Format:

The first line of input contains an integer T denoting the number of test cases.

Each test case consist of a string in 'lowercase' only in a separate line.

Output Format:

Print "Exists" or "Dosen't Exists" in a separate line.

Program Code:

#include <stdio.h>
#include <string.h>
int M,N,i,j,res;
int main()
{int t;
scanf("%d",&t);
while(t--){
char string[100];
char p[100];
scanf("%s%s",string,p);
M=strlen(p);
N=strlen(string);
res=0;
for(i=0;i<=N-M;i++){
30/114
Downloaded by Md Ashfaq ([email protected])
lOMoARcPSD|29467424

6/28/22, 5:45 PM Record

for(j=0;j<M;j++)
if(string[i+j]!=p[j])
break;
if(j==M){
res++;
j=0;
}
}
if(res>0)printf("Exists\n");
else printf("Dosen't Exists\n");
}
return 0;
}

Question 32
Problem Description:

Jeferson was given a string s of length 8 consisting solely of '0's and '1's.

Assume that the characters of the string are written in a circular fashion.

Jeferson need to find the number of 0-1 or 1-0 transitions that one has to make while making a single
traversal over the string. ie. start from any character and go circularly until you get back to the same
character, and find the number of transitions that you made.

The string is said to be said to be uniform if there are at most two such transitions.

Otherwise, it is called non-uniform.

Given the string s, tell whether the string is uniform or not.

Constraints:

1 ≤ T ≤ 15

Length of s is 8

Input Format:

The first line of the input contains an integer T denoting the number of test cases. The description of T
test cases follows.

The only line of input contains the string s.

Output Format:

For each test case, output "uniform" if the given string is uniform and "non-uniform" otherwise.

Program Code:

#include <stdio.h>
#include<string.h>
int main(){
char arr[10];
int t,count=0,i;
31/114
Downloaded by Md Ashfaq ([email protected])
lOMoARcPSD|29467424

6/28/22, 5:45 PM Record

scanf("%d",&t);
while(t!=0){
scanf("%s",arr);
for(i=0;i<strlen(arr);i++){
if(arr[i]!=arr[i+1])
count++;
}
count--;
if(count<=2)
printf("uniform\n");
else printf("non-uniform\n");
t--;
count=0;
}
return 0;
}

Question 33
Problem Description:

Fazil's faculty gave him a string S consisting of only 1s and 0s and he need to find the number of
substrings which start and end both in 1.
In this problem, a substring is defined as a sequence of continuous characters Si, Si+1, ..., Sj where 1 ≤ i ≤
j ≤ N.

Cn you help Fazil in completing the task?

Constraints:

1 ≤ T ≤ 105

1 ≤ N ≤ 105

Sum of N over all testcases ≤ 105

Input Format:

First line contains T, the number of testcases.

Each testcase consists of N(the length of string) in one line and string in second line.

Output Format:

For each testcase, print the required answer in one line.

Explanation:

Assume the String with 5 digits 10001 then it has 3 substrings such as S[1,1], S[5,5] and S[1,5] that
satisfy the condition.

Program Code:

#include <stdio.h>
int main()
32/114
Downloaded by Md Ashfaq ([email protected])
lOMoARcPSD|29467424

6/28/22, 5:45 PM Record

{
int t;
int l;
scanf("%d",&t);
int sum;
char string;
int pair;
while(t>0){
pair=0;
sum=0;
scanf("%d",&l);
int i;
for(i=0;i<=l;i++){
scanf("%c",&string);
if(string=='1')
pair++;
}
for(i=1;i<=pair;i++)
sum+=i;
printf("%d\n",sum);
t--; }
return 0;}

Question 34
Problem Description:
Every day, Selvan goes to his office by train and buys the ticket from the counter on the day of travel.

On the ticket, there is a letter-code that is represented as a string of upper-case Latin letters.

Selvan believes that the day will be successful in case exactly two different letters in the code alternate.

Otherwise, he believes that the day will be unlucky. Please see note section for formal definition of
alternating code.

If the ticket code is given. Please determine, whether the day will be successful for Selvan or not.

Print "Successful Day" or "Unsuccessful Day" (without quotes) corresponding to the situation.

Constraints:
1 ≤ T ≤ 100
S consists only of upper-case Latin letters

Input Format:
The first line of the input contains an integer 'T' denoting the number of test cases. The description of 'T'
test cases follows.

The first and only line of each test case contains a single string 'S' denoting the letter code on the ticket.

Output Format:
Print the output a single line containing "YES" (without quotes) in case the day will be successful and
"NO" otherwise.

Program Code:

33/114
Downloaded by Md Ashfaq ([email protected])
lOMoARcPSD|29467424

6/28/22, 5:45 PM Record

#include <string.h>
#include <stdio.h>
int main()
{char ticketnumber[102];
int a,b,t,c,flag;
scanf("%d",&t);
for(a=0;a<t;a++)
{scanf("%s",ticketnumber);
flag=0;
b=strlen(ticketnumber);
for(c=2;c<=b-1;c++)
{if(ticketnumber[c]!=ticketnumber[c-2])
flag=1;}
(flag==0)?printf("YES\n"):printf("NO\n");
}
return 0;
}

Question 35
Problem Description:
Nathan won the man of the match award in the recently concluded local tournament final. So the friends
of nathan have asked him to take them to cinemas as a trat for winning man of the match.But

Nathan is short of money to take them to cinemas so to postpone the cinema plan he tred to engage them
with the programming challenge.

The task was if the string S was given they have to change the string according to the following
condition:

If the first letter in a string is capital letter then change the full string to capital letters.

Else change the full string to small letters.

Constraints:

1<=T<=50

1≤|s|≤104

Input Format:

The first line of input contains an integer T denoting the number of test cases.

Then T test cases follow. Each test case contains a string S.

Output Format:

For each test case, print the changed string in a new line.

Program Code:

#include <stdio.h>
#include <ctype.h>
int main()
34/114
Downloaded by Md Ashfaq ([email protected])
lOMoARcPSD|29467424

6/28/22, 5:45 PM Record

{
int T,i;
scanf("%d",&T);
while(T--){
char s[100];
scanf("%s",s);
if(isupper(s[0])){
for(i=1;i<=100;i++){
s[i]=toupper(s[i]);
}
}
else if(islower (s[0])){
for(i=1;i<=100;i++){
s[i]=tolower(s[i]);
}
}
printf("%s\n",s);
}
return 0;
}

Question 36
Problem Description:

Raju the fan of Great Mathematician Ramanujan developed an encoder that encodes the first 16
lowercase English letters using 4 bits each.

The first bit (from the left) of the code is 0 if the letter lies among the first 8 letters, else it is 1, signifying
that it lies among the last 8 letters.

The second bit of the code is 0 if the letter lies among the first 4 letters of those 8 letters found in the
previous step, else it's 1, signifying that it lies among the last 4 letters of those 8 letters.

Similarly, the third and the fourth bit each signify the half in which the letter lies.

Constraints:

1≤T≤10

4≤N≤10^5

The length of the encoded string is a multiple of 4.

0≤Si≤1

Functional Description:

Now your task is given a binary encoded string S, of length at most 10^5, decode the string.

That is, the first 4 bits are the encoding of the first letter of the secret message, the next 4 bits encode the
second letter, and so on. It is guaranteed that the string's length is a multiple of 4.

Input Format:

The first line of the input contains an integer T, denoting the number of test cases.
35/114
Downloaded by Md Ashfaq ([email protected])
lOMoARcPSD|29467424

6/28/22, 5:45 PM Record

The first line of each test case contains an integer N, the length of the encoded string.

The second line of each test case contains the encoded string S.

Output Format:

For each test case, print the decoded string, in a separate line.

Explanation:

For example, the letter j would be encoded as :

Among (a,b,c,d,e,f,g,h | i,j,k,l,m,n,o,p), j appears in the second half. So the first bit of its encoding is 1.

Now, among (i,j,k,l | m,n,o,p), j appears in the first half. So the second bit of its encoding is 0.

Now, among (i,j | k,l), j appears in the first half. So the third bit of its encoding is 0.

Now, among (i | j), j appears in the second half. So the fourth and last bit of its encoding is 1.

So j's encoding is 1001.

Program Code:

#include <stdio.h>
#include <string.h>
#include <math.h>
void loop()
{

}
int main()
{
int t,n,j,b,k,i,d;
scanf("%d",&t);
while(t--)
{
int l;
scanf("%d",&n);
char s[n];
scanf("%s",s);
d=strlen(s);
if(d<n)
for(l=d-1;l<n;l++)
s[l]='0';
b=0;
for(i=1;i<=n/4;i++)
{
k=0;
for(j=3;j>=0;j--)
{
int z=((int)(s[b])-48);
k=k+(z*pow(2,j));
b++;
}
k+=97;
36/114
Downloaded by Md Ashfaq ([email protected])
lOMoARcPSD|29467424

6/28/22, 5:45 PM Record

printf("%c",k);
}printf("\n");
}
return 0;
}

Question 37
Problem Description:

Not everyone probably knows that Nivin has younder brother Nithin. Currently Nithin learns to read.

He knows some subset of the letter of Latin alphabet. In order to help Nithin to study, Nivin gave him a
book with the text consisting of N words. Nithin can read a word iff it consists only of the letters he
knows.

Now Nivin is curious about which words his brother will be able to read, and which are not. Please help
him!

Constraints:
1 ≤ |S| ≤ 26
1 ≤ N ≤ 1000
1 ≤ |Wi| ≤ 12
Each letter will appear in S no more than once.
S, Wi consist only of lowercase Latin letters.

Input Format:
The first line of the input contains a lowercase Latin letter string S, consisting of the letters Nithin can
read.

Every letter will appear in S no more than once.

The second line of the input contains an integer N denoting the number of words in the book.

Each of the following N lines contains a single lowecase Latin letter string Wi, denoting the ith word in
the book.

Output Format:
For each of the words, output "Yes" (without quotes) in case Nithin can read it, and "No" (without
quotes) otherwise.

Program Code:

#include <stdio.h>
#include <string.h>
int main()
{
char string[100];char search[100];
int t,i,j,len,c=0;
scanf("%s",string);
len=strlen(string);
scanf("%d",&t);
while(t--){
c=0;
scanf("%s",search);
37/114
Downloaded by Md Ashfaq ([email protected])
lOMoARcPSD|29467424

6/28/22, 5:45 PM Record

for(i=0;i<=strlen(string)-1;i++)
{
for(j=0;j<=strlen(search)-1;j++)
{
if(string[i]==search[j]){
c++;break;}
}
}
if(c==len)
printf("Yes\n");
else
printf("No\n");
}
return 0;
}

Question 38
Problem Description:
Aaron has a number D containing only digits 0's and 1's. He wants to make the number to have all the
digits same. For that, he will change exactly one digit, i.e. from 0 to 1 or from 1 to 0.

If it is possible to make all digits equal (either all 0's or all 1's) by flipping exactly 1 digit then output
"Yes", else print "No" (quotes for clarity)

Constraints:

1 ≤ T ≤ 10
1 ≤ Length of the number D ≤ 10

Input Format:
The first line will contain an integer 'T' representing the number of test cases.

Each test case contain a number made of only digits 1's and 0's on newline

Output Format:
Print T lines with a "Yes" or a "No", depending on whether its possible to make it all 0s or 1s or not.

Program Code:

#include <stdio.h>
#include <string.h>
int main()
{
int T,n1,n0,len,i;
char str[100002];
scanf("%d",&T);
while(T--){
scanf("%s",str);
n1=n0=0;
len=strlen(str);
for(i=0;i<len;i++){
if(str[i]=='0')
38/114
Downloaded by Md Ashfaq ([email protected])
lOMoARcPSD|29467424

6/28/22, 5:45 PM Record

++n0;
else
++n1;
}
if(n1==len-1||n0==len-1){
printf("YES\n");
}
else{
printf("NO\n");
}
}
return 0;
}

Question 39
Problem Description:
Nathan wants to implement wildcard pattern matching supporting only the wildcard '?'.

The wildcard character '?' can be substituted by any single lower case English letter for matching.

He has two strings X and Y of equal length, made up of lower case letters and the character '?'.

He wants to know whether the strings X and Y can be matched or not.

Constraints:
1 ≤ T ≤ 50
Both X and Y have equal length and the length is between 1 and 10.
Both X and Y consist of lower case letters and the character '?'.

Input Format:
The first line of input contain an integer 'T' denoting the number of test cases.

Each test case consists of two lines, the first line contains the string 'X' and the second contains the string
'Y'.

Output Format:
Print the output a single line with the word 'Yes' if the strings can be matched, otherwise output 'No'.

Program Code:

#include <stdio.h>
#include <string.h>
int main()
{
int n,i;
scanf("%d",&n);
while(n--){
int MAX=10;
char a[MAX],b[MAX];
scanf("%s",a);
scanf("%s",b);
int flag=0;
for(i=0;i<strlen(a);i++){

39/114
Downloaded by Md Ashfaq ([email protected])
lOMoARcPSD|29467424

6/28/22, 5:45 PM Record

if(a[i]!=b[i]){
if(a[i]=='?'||b[i]=='?') flag=flag;
else
flag=1;}
}
if(flag) printf("No\n");
else printf("Yes\n");
}
return 0;}

Question 40
Problem Description:

Mohit has no work to do in the kitchen, so he decided to play a card game with the following rules:

Initially, N cards are placed in a row on a table. Each card is placed either face up or face down.

The goal of the game is to remove all cards from the table, one by one.

A card may be removed only if it is currently facing up.

When a card is removed, its adjacent cards (the cards directly to its left and right, if they exist) are
flipped, i.e. a card that was facing up will be facing down and vice versa.

There is an empty space left behind each removed card, i.e. the remaining cards are not moved to create a
contiguous row of cards again.

Therefore, if the game starts with three cards and the middle card is removed, then the cards on the sides
are flipped, but removing one of these cards afterwards does not cause the other card to be flipped, since
it is only adjacent to the empty space created by removing the middle card.

Determine whether Mohit is able to win this game.

Constraints:

1≤T≤10^2

1≤|S|≤10^5

Input Format:

The first line of the input contains a single 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 string S describing the row of cards initially placed on the
table. Each character of this string is either '1', denoting a face up card, or '0', denoting a face down card.

Output Format:

For each test case, print a single line containing the string "WIN" if Chef can win the game or "LOSE" if
he cannot win (without quotes).

Program Code:

40/114
Downloaded by Md Ashfaq ([email protected])
lOMoARcPSD|29467424

6/28/22, 5:45 PM Record

#include <stdio.h>
#include <string.h>
int main()
{char s[100002];
int test,i;int flag=0;
scanf("%d",&test);
while(test--){
scanf("%s",s);
flag=0;
for(i=0;i<strlen(s)-1;i++){
if(s[i]=='1'||s[i+1]=='0')
flag++;
}
if(flag%2==0)printf("WIN\n");else printf("LOSE\n");
}
return 0;
}

Question 41
Question description:

Yasir is a very active young man who is very interested in making money in a simple way.

So he is always looking for a way to make some money.

One day, a money-making show called Jackpot on popular channel news came to Yasir's ears.

So he was going to the JACKPOT game in a game park it has a dial full of numbers in random order.

If it is arranged in ascending order using the sorting concept, he will win a million-dollar prize.

can you help him to input an array of size n and sort it in ascending order using the sorting?

Constraints:

1≤n≤1000

1≤array≤1000

Input Format:
First-line represents the number of values
Second-line indicates the random integer numbers.

Output Format:

In a single-line of output print the values in ascending order.

Program Code:

#include<stdio.h>

void asc_sort(int a[100],int n)

{
41/114
Downloaded by Md Ashfaq ([email protected])
lOMoARcPSD|29467424

6/28/22, 5:45 PM Record

int i,j,temp;

for(i=0; i<n-1; i++)

{
for(j=0; j<n-i-1; j++)
{
if(a[j]>a[j+1])
{
temp=a[j];
a[j]=a[j+1];
a[j+1]=temp;
}
}
}
}
int main()
{
int a[100],i,n;
scanf("%d", &n);
for(i=0; i<n; i++)
{
scanf("%d",&a[i]);
}
asc_sort(a,n);
for(i=0; i<n; i++)
{
printf("%d ",a[i]);
}
return 0;
}

Question 42
Question description:

Laaslya is planning to go to the cinema theater to spend her weekend vacation. Her friends Tina, Caleb,
and Jocelyn all knew about Laasya's plan. They say we are coming too, but she thinks to ignore them
because only Laasya has enough money to pay for the cinema ticket.

Laasya is very good at programming, so she puts up a puzzle to avoid taking her friends to the cinema.
She also says that those who have finished this can come with me.

The puzzle is Tower of Hanoi is a mathematical puzzle where we have three rods and n disks. The
objective of the puzzle is to move the entire stack to another rod. the number of the disk will be given as
input obeying the following simple rules:

1) Only one disk can be moved at a time.

2) Each move consists of taking the upper disk from one of the stacks and placing it on top of another
stack i.e. a disk can only be moved if it is the uppermost disk on a stack.

3) No disk may be placed on top of a smaller disk.


42/114
Downloaded by Md Ashfaq ([email protected])
lOMoARcPSD|29467424

6/28/22, 5:45 PM Record

Constraints:

1≤ n ≤1000

Input Format:

The input represents the single line integer <n=

Output Format:

Display the Movement of the disk.

Refer sample input and output

Program Code:

#include<stdio.h>
void tHanoi(int n,char from_rod,char to_rod,char aux_rod)
{
if(n==1)
{
printf("\nMove disk 1 from rod %c to rod %c",from_rod,to_rod);
return;
}
tHanoi(n-1,from_rod,aux_rod,to_rod);
printf("\nMove disk %d from rod %c to rod %c",n,from_rod,to_rod);
tHanoi(n-1,aux_rod,to_rod,from_rod);
}
int main()
{
int num;
scanf("%d",&num);
tHanoi(num,'A','C','B');
return 0;
}

Question 43
Question description

Simon is wasting electricity without caring about it. He leaves all switch on even when he is not in the
room.

After a Month of using Electricity, His security Guard informed him about Increased units of electricity.

Due to the electricity bill, Simon gets tensed about his electricity bill.

Simon willing to make the program for calculating the electricity bill for personal evaluation.
can you help Simon to calculate his electric bill by taking the electric unit as an input?

Function Description

Unit is less than 50 units then bill will be unit * 0.50

43/114
Downloaded by Md Ashfaq ([email protected])
lOMoARcPSD|29467424

6/28/22, 5:45 PM Record

Unit is less than 150 and more than 50 then bill will be unit * 0.75 after 50 units

Units are less than 250 and more than 150, then bills will be unit * 1.2 after 150 units

Unit is more than 250 then bill will be unit * 1.5 after 250 units.

Constraints

1≤ unit ≤1000

Input Format

Single Integer input represents the <number of units=

Output Format

Print floating value the <amt= values for the total unit of electricity.

Refer to the sample input and output ..

Program Code:

#include<stdio.h>
float bill(int unit);
int main()
{int n;
scanf("%d", &n);
float total = bill(n);
printf("%.2f", total);
return 0;
}
float bill(int unit){
if(unit < 50) return unit*0.50;
else if(unit <= 50) return (((unit-50)*0.75) + 25);
else if(unit <= 250) return (((unit-150)*1.20)+100);
else return (((unit-250)*1.50) + 220);
return 0;
}

Question 44
Question Description:

Sajid is an eighth-grader in a CBSE school.

Although he scored well in many subjects, he did not an expert in computer programming languages.

But Sajid's computer examination is scheduled for next week.

As per the blueprint, many questions would come from the recursive function topic.

44/114
Downloaded by Md Ashfaq ([email protected])
lOMoARcPSD|29467424

6/28/22, 5:45 PM Record

He collected previous year's questions. one of the repeated questions is to calculate the factorial value for
the given number using a recursive function.

Can you help him to calculate the factorial using recursion?

Functional Description

5! = 5*4*3*2*1 = 120

Constraints

1≤ num ≤1000

Input Format:

Single-line represents an integer number.

Output Format:

The single line indicates the factorial value.

Refer sample input and output

Program Code:

#include <stdio.h>
long facto(int n)
{ if (n>=1) return n*facto(n-1); else
return 1;
}
int main()
{
int q;
scanf("%d",&q);
printf("%ld", facto(q));
return 0;
}

Question 45
Question description

Simon is planning to summer vacation trip to Kodaikanal. His friends Tina, Nathan, and Irfan all learned
about Samson's plan. They said, we are also coming too, but Simon has only money to spend for him
alone. Simon is very good at programming, So he puts a puzzle to his friends to avoid taking them to
Kodaikanal and tells them that those who finish this can come with him.

the puzzle is to find a super digit of an integer x using the following rules: For Given integers, you need
to find the super digit of the integer.

If x has only 1 digit, then its super digit is x. Otherwise, the super digit of x is equal to the super digit of
the sum of the digits of x.

45/114
Downloaded by Md Ashfaq ([email protected])
lOMoARcPSD|29467424

6/28/22, 5:45 PM Record

Functional Description
For finding the digit and value, you have used the following expressions

sum+=n%10;
n/=10;

Explanation

For Example 1:
super digit of 9875 will be calculated as:
super-digit(9875) = super-digit(9+8+7+5)
= super-digit(29)
= super-digit(2+9)
= super-digit(11)
= super-digit(1+1)
= super-digit(2)
= 2.

Example 2 (for two numbers)

You are given two numbers n and k. The number p is created by concatenating the string n k times.
Continuing the above example where n=1234, assume your value k=4.

Your initial p=1234 1234 1234 1234 (spaces added for clarity).

superDigit(p) = superDigit(123412341234234)

1+2+3+4+1+2+3+4+1+2+3+4+4+3+2+1 = 40

superDigit(p) = superDigit(40)

4+0 = 4

superDigit(p) = superDigit(4)

All of the digits of p sum to 40. The digits of 40 sum to 4.

4 is only one digit, so it's the super digit.

Constraints

1≤ num ≤10000

1≤ k ≤100

Input

The single line represents the integer n and k

Output

single-line prints the super digit value.

Program Code:

46/114
Downloaded by Md Ashfaq ([email protected])
lOMoARcPSD|29467424

6/28/22, 5:45 PM Record

#include <stdio.h>
int sumd(int n){
int k, sum=0; scanf("%d", &k);
while(n) {
sum+=n%10;
n/=10;}
return sum*k;}
int superd(int num) {
int n=0;
return (num%9 == 0) ? n = 9:num%9;
} int main()
{int num;
scanf("%d", &num); num= sumd(num); printf("%d",superd(num));
return 0;}

Question 46
Question description:

Advika is trying to solve the puzzle problem during Mathematics class hour. she has a graph paper with
G X N rows and columns, and the puzzle question is, an NCC training base in each cell for a total of G X
N bases. He wants to drop food items to every point based on strategic points on the graph paper,
marking each drop point with a red dot. If a base contains at least one food package inside or on top of its
border fence, then it's considered to be supplied.

For example: if Advika has four bases in a 2x2 grid. If he drops a single food package where the walls of
all four bases intersect, then those four cells can access the food package.

Given G and N, what's the minimum number of packages that Luke must drop to supply all of his bases?

Example :
G=2, N=3.

Food Packages can be dropped at the corner between cells (0, 0), (0, 1), (1, 0), (1, 1) , (0, 2) and (1, 2).
This supplies all bases using packages.

Function Description:

G: the number of rows


N: the number of columns

Constraints:

1≤=X≤=10, 1≤=Y≤=10

Input Format:

Two space-separated integers describing the respective values of G and N.

Output Format:

47/114
Downloaded by Md Ashfaq ([email protected])
lOMoARcPSD|29467424

6/28/22, 5:45 PM Record

The only line of output has single integer indicating the minimum number of food packages required

Explanation

Luke has four bases in a 2X2 grid. If he drops a single package where the walls of all four bases intersect,
then those four cells can access the package:

Because he managed to supply all four bases with a single supply drop, we print 1 as our answer.

Program Code:

#include<stdio.h>
int NccCells(int x,int y)
{
return((x+1)/2)*((y+1)/2);
}

int main()
{
int x,y;
scanf("%d %d",&x,&y);
printf("%d",NccCells(x,y));
return 0;
}

Question 47
Question description:

Sajid is a graduate student he applied to a BPO company but he does not get typing fast. So he wanted to
increase his typing speed for the job.

His well-wisher suggested that he type the sentence "The quick brown fox jumps over the lazy dog"
repeatedly.

This sentence is known as a pangram because it contains every letter of the alphabet.

After typing the sentence several times, Sajid needs to check whether the given number is a pangram.
can you help him, whether the given sentence is a pangram or not

Functional Description:

The for loop reads character by character of the String and if the character is an alphabet then increment
the total if used[alphabet]=0 and it makes used[alphabet]=1 (increment for every new alphabet used
between a to z. the alphabet count should not exceed 26.

Constraints:
The given input letters should be a≤=str≤z

48/114
Downloaded by Md Ashfaq ([email protected])
lOMoARcPSD|29467424

6/28/22, 5:45 PM Record

Input Format :

The only line of input has a input sentence.

Output Format :

Print as <pangram= or <not a pangram= based on the condition

Program Code:

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
void findpan();
int main()
{
char str[50];
fgets(str,50,stdin);
findpan(str);
return 0;
}
void findpan(char arr[]){
int count[26]={0};
int i,n=strlen(arr);
for(i=0;i<n;i++)
count[arr[i]-'a']=1;
for(i=0;i<26;i++)
if(count[i]==0)break;
if(i==26)printf("panagram");
else printf("not a panagram");
}

Question 48
Question Description:

Simon wants a number plate for his Brand new luxury car. he likes it to be unrepeatable.

He came through a display board about fibonacci series he wants to check to whether the number he
wants to use for his car comes in fibonacci series or not.

Can you help to them for program which checks if a number is present in fibonacci series or not using
function .

Functional Description:

Perfect square value should be generated for the given number (n) which is multiplied by given format
that is (5*n*n+4) is double equal to one (or) (5*n*n - 4) is double equal to one

Constraints:

1<=i<=1000

49/114
Downloaded by Md Ashfaq ([email protected])
lOMoARcPSD|29467424

6/28/22, 5:45 PM Record

Input Format:

The only line of input has single integer value representing a car number.

Output Format:
Print "YES" if number belongs to fibonacci, otherwise print as "NO".

Program Code:

#include <stdio.h>
#include<math.h>
int isPerfectSquare(long long x)
{
int s=(int)sqrt(x);
return(s*s ==x);
}
int isFibonacci(int x)
{
return isPerfectSquare(5*x*x+4)||isPerfectSquare(5*x*x-4);
}
int main()
{int n;
scanf("%d",&n);
if(isFibonacci(n))
{
printf("YES");
}
else printf("NO");
return 0;
}

Question 49
Question Description

Tina is a Bachelor of Computer Applications (BCA) student. During her final year Campus Interview,
she has an opportunity to get a job in a software company in Bangalore.

The company provides Five months training period with Rs.30000/month Package. Then it will be
incremented to Rs.55000 per month.

At the end of the training, the examination was conducted for all freshers, Tina got a question paper and
one of the questions comes under the concept of programming.

The program was, she has to calculate the sum of an array of elements using RECURSION to Complete
One of Her math Problem.

Tina does not know how to get the output for this program using recursion. So you have to help Tina get
this job.

Constraints:

a[1] ≤ arr ≤ a[100]


50/114
Downloaded by Md Ashfaq ([email protected])
lOMoARcPSD|29467424

6/28/22, 5:45 PM Record

1 ≤ N ≤1000

Input Format:

First line indicates the number of elements

The second line indicates the elements

Output Format:

In a Single line print the sum of array elements.

Program Code:

#include <stdio.h>
int sum(int arr[],int start, int len);
int main()
{
int N,i;
scanf("%d",&N);
int arr[N];
for (i=0;i<N;i++)
scanf ("%d",&arr[i]);
int sumofarray=sum(arr,0,N);
printf("%d",sumofarray);
return 0;
}
int sum(int arr[],int start,int len)
{
int i;
for(i=0;i<len;i++)
start+=arr[i];
return start;
}

Question 50
Question description

Nancy, Simon, and Swati were all attending campus interviews. they got selected for the second round.

Nancy failed to clear the second round and others to selected for the next round of interviews.

Nancy discussed with her friend the question which came in the interview.
one of the questions was, to create a program for the Fibonacci series. Nance doesn't know, how to solve
it.

But it's in the syllabus of his exam. So can you help to create a program in the specified concept to get an
offer in the next interview ?.

Functional Description:

The recursive function should be used to find the Fibonacci series.

51/114
Downloaded by Md Ashfaq ([email protected])
lOMoARcPSD|29467424

6/28/22, 5:45 PM Record

Constraints
1≤ N ≤1000

Input Format

The single line represents an integer as a number.


Output Format.
The single line represents the Fibonacci series.

Refer sample input and output test cases.

Program Code:

#include <stdio.h>
void getFibonacii(int a,int b,int n)
{
int c;
if(n>0)
{
c=a+b;
a=b;
b=c;
printf("%d ",c);
getFibonacii(a,b,n-1);
}
}
int main()
{
int a=0,b=1,n;
scanf("%d",&n);
printf("%d %d ",0,1);
getFibonacii(a,b,n-2);
return 0;
}

Question 51
Problem Description:
A small country leader decided to bring some reforms after 25 years of his rule.

So as to engage the educated and unemployed youth of that country in this matter.

He Ordered young people to create an application to assess citizen's eligibility for voting.

You too can contribute in this initiative.

Can you help them with the application they wanted?

Functional Description:

If the age is below 18 and above 60 then the citizen of the country is <Not Eligible= for voting

If the age is greater than 18 less than or equal to 60 the citizen of the country is "Eligible" for voting.

52/114
Downloaded by Md Ashfaq ([email protected])
lOMoARcPSD|29467424

6/28/22, 5:45 PM Record

Constraints:
1 <=age<= 150

Input Format:
The input contains the integer that indicates the age of people.

Output Format:
Print the appropriate output based on the condition.

Program Code:

#include <stdio.h>

union Citizen{
int age;
};

int main()
{
union Citizen E;
scanf("%d", &E.age);

if(E.age>=18 && E.age<=60) printf("Eligible");


else printf("Not Eligible");
return 0;
}

Question 52
Problem Description:

Jocelyn's skill is to write stories of letters. But she finds it very boring to write the story, and after three
hours of work,

Jocelyn realizes that what she wrote is full of A and B letters, and decides that the story will not end on
time. So having a little fun with it, at least counting the bubbly words.

Now Jocelyn connects the same pair of letters (A with A, B, and B) by drawing lines on the word. A
given word bubble, if it is possible to combine each letter exactly with another letter so that it does not
exceed two letters.

Help Jocelyn figure out how many words bubbly.

Constraints:

1 ≤ M ≤ 100

Input Formats:

The first line of input contains the positive integer M, the number of words written down by Jocelyn.

Each of the following M lines contains a single word consisting of letters A and B, with length between 2
and 10^5, inclusive. The sum of lengths of all words doesn't exceed 10^6.
53/114
Downloaded by Md Ashfaq ([email protected])
lOMoARcPSD|29467424

6/28/22, 5:45 PM Record

Output Formats:

The first and only line of output must contain the number of bubbly words

Example:

Sample Input:

3
ABAB
AABB
ABBA

SAMPLE OUTPUT

Explanation

A B A B

First A indexed in 1 and another A letter indexed in 3 rd position.

First B indexed in 2 and another B letter indexed in 4 th position.

if you draw an arc between A & A, B & B, then the arc will intersect each other.So it is not a bubbly
word

AABB

First A indexed in 1 and another A letter indexed in 2nd position.

First B indexed in 3 and another B letter indexed in 4th position.

if you draw an arc between A & A, B & B, then the arc will not intersect each other. So it is a bubbly
word

ABBA

First A indexed in 1 and another A letter indexed in 4th position.

First B indexed in 2 and another B letter indexed in 3rd position.

if you draw an arc between A & A, B & B, then the arc will not intersect each other. So it is a bubbly
word

Program Code:

#include <stdio.h>
#include <string.h>
struct letters{char x[1000001];};
char stack[1000001];
int top=-1;
void pop(){top--;}
void push(char n)
{

54/114
Downloaded by Md Ashfaq ([email protected])
lOMoARcPSD|29467424

6/28/22, 5:45 PM Record

top++;
stack[top]=n;
}
int sizeOfStack(){return top+1;}
int main()
{
struct letters story;
int n,i,words=0;
scanf("%d",&n);
while(n--)
{
scanf("%s",story.x);
for(i=0;i<strlen(story.x);i++)
{
if(top==-1 || stack[top]!=story.x[i])
push(story.x[i]);
else
pop();
}
if(sizeOfStack()==0)
words++;
top=-1;
}
printf("%d",words);
return 0;
}

Question 53
Question Description:

Aarav, Advika, binitta are good friends.

They are studying final year B.E. Computer Science and Engineering.

They want to develop an application to order student details in ascending order which will be useful for
the high school in the village they belong to.

Can you help them in the application development?

Constraints:

1≤=yearOfStudy≤=4

1.0≤=cgpa≤=10.0

Input Format

First-line indicates n the number of students

The next line indicates the name, department, year of study, and CGPA details of n students

Output Format

55/114
Downloaded by Md Ashfaq ([email protected])
lOMoARcPSD|29467424

6/28/22, 5:45 PM Record

Print the expected output in proper format

Note: Students details should be sorted based on their "Names" in ascending order

Refer Sample Testcases

Program Code:

#include <stdio.h>
#include <string.h>
struct Student{
char name[50];
char department[5];
int yearOfStudy;
float cgpa;
}S1[100],t;
int main()
{int i=0,j=0,n;
scanf("%d",&n);
for(i=0;i<n;i++){
scanf("%s %s %d %f",S1[i].name,S1[i].department,&S1[i].yearOfStudy,&S1[i].cgp
}
for(i=0;i<n;i++){
for(j=i+1;j<n;j++){
if(strcmp(S1[i].name,S1[j].name)>0){
t=S1[i];
S1[i]=S1[j];
S1[j]=t;
}
}
}
for(i=0;i<n;i++){
printf("Name:%s\n",S1[i].name);
printf("Department:%s\n",S1[i].department);
printf("Year of study:%d\n",S1[i].yearOfStudy);
printf("CGPA:%.1f\n",S1[i].cgpa);
}
return 0;
}

Question 54
Question description:

Irfan is going to finish his final year master of computer application. he is in the final year of the course
MCA.

Every student has to participate in a department coding test to face on-campus interview activities.

So Irfan plans to start a practice of a code with a simple calculation of identifying +ve and -ve numbers.

Can you help Irfan by developing the application for finding the nature of the number?

Constraints

56/114
Downloaded by Md Ashfaq ([email protected])
lOMoARcPSD|29467424

6/28/22, 5:45 PM Record

0 ≤ num ≤100000

Input Format:

Only Line of input has a single value representing the number for which he need to find the nature.

Output Format:

If the number you give is more than 0 print as <Positive= else print as <Negative=.

Program Code:

#include <stdio.h>
union Calculator
{
int num;
};
int main()
{
union Calculator c1;
scanf("%d",&c1.num);
if(c1.num>0)
printf("Positive");
else
printf("Negative");
return 0;
}

Question 55
Problem Description:
Mr.James planned to go Godzilla vs Kong movie in iMax with his wife.
There was as competition going on in the theare complex for couples.

If a particular couple solves the task given to them then they will get the tickets for the movie free of
cost.

The task is nothing but to find the sum of digits of the number provided to them.

Can you help James and his wife with the task so that they will get the free tickets?

Constraints:
1 <= n<= 1000000

Input Format:
Only line of input has single integer representing n.

Output Format:
In the only line of output print the sum of digits of the number.

Program Code:

57/114
Downloaded by Md Ashfaq ([email protected])
lOMoARcPSD|29467424

6/28/22, 5:45 PM Record

#include <stdio.h>
int sum(int num)
{
if(num!=0)
return (num%10+sum(num/10));
else
return 0;
}
union Data
{
int num,res;
}data;
int main()
{
scanf("%d",&data.num);
data.res=sum(data.num);
printf("%d",data.res);
return 0;
}

Question 56
Problem Description:
Mr. Mannu was working in Renault Nissan.

His company has the ERP in which all the employees need to update their age and height.
On seeing the tool Mannu also had the idea of developing the tool with similar logic.

Can you help him?

Can you help him doing so?


Constraints:
1 <= age <= 100
1 <= height <=250

Input Format:
The first line of the input has a single value of type integer representing employee age
The second line of the input has a single value of type float representing employee height

Output Format:
Print the age and height in the expected format.

Refer sample testcases for format specification.

Program Code:

#include <stdio.h>
union number{
int n1;
float n2;
};
int main()
{union number x;
scanf("%d",&x.n1);

58/114
Downloaded by Md Ashfaq ([email protected])
lOMoARcPSD|29467424

6/28/22, 5:45 PM Record

printf("Age=%d years\n",x.n1);
scanf("%f",&x.n2);
printf("Height=%.2f cm",x.n2);
return 0;
}

Question 57
Problem Description:

Hassan lives in a village and has to take the bus to college every day.

Hassan is in his final year studying Computer Science Engineering.

He had a great passion for the watch when he was a child.

It was his childhood to buy a watch at festivals in his hometown.


His dad buys him a new smartwatch. In it, he develops an application to find the time differences in the
two different time zones.

Can you help him?

Constrain:

1<= T <=100
1<= N <=100

Input Format:

First line indicates an integer A, B denoting hours of two different zone.


Second line indicates an integer C, D denoting the minutes of the different zone.

Third line indicates an E, F denoting the seconds of the different zone.

Output Format:
First-line indicates the difference between Hours,

The second line indicates the difference between Minutes

Third line indicates the difference between the seconds

Program Code:

#include <stdio.h>

union Time{
int hours, minutes, seconds;
};
int main()
{
union Time startTime,stopTime,diff;
scanf("%d %d", &startTime.hours, &stopTime.hours);
diff.hours = startTime.hours - stopTime.hours;
printf("%d\n",diff.hours);
scanf("%d %d", &startTime.minutes, &stopTime.minutes);
59/114
Downloaded by Md Ashfaq ([email protected])
lOMoARcPSD|29467424

6/28/22, 5:45 PM Record

diff.minutes = startTime.minutes - stopTime.minutes;


printf("%d\n",diff.minutes);
scanf("%d %d", &startTime.seconds, &stopTime.seconds);
diff.seconds = startTime.seconds - stopTime.seconds;
printf("%d",diff.seconds);
return 0;
}

Question 58
Problem Description:

Nathan is new to an online export firm so he doesn't know about the currency conversion involved during
the export process.

Since Nathan is going to export books to Singapore, so he likes to know about Indian rupee to (Singapore
Dolor) SGD conversion.

Can you help him by creating a code snippet with the help of UNION to do the currency conversion for
Nathan?

Functional Description:

1 SGD = 55.26 INR

Constraints:
1<= T <=10
1.00<= INR <=1000.00

Input Format:
First line has as single value of type integer representing T the number of testcases.

Next T lines has single value of type float representing the price of book in Indian currency.
Output format:

For each testcase print the SGD value equivalent to Indian currency.

Program Code:

#include <stdio.h>
union price{
float inr;
};
union price book;
int main()
{int t;
scanf("%d",&t);
while(t--){
scanf("%f",&book.inr);
printf("%.2f\n",book.inr*55.26);
}
return 0;
}

60/114
Downloaded by Md Ashfaq ([email protected])
lOMoARcPSD|29467424

6/28/22, 5:45 PM Record

Question 59
Problem Description:
Director Manirathnam wants to direct a movie on a high budget.
So He was searching for a historical novels in the book shop. One of his friend P C Sreeram suggested
him some of the best novels that can be converted into movie.

Based on his suggestion Manirathnam has collected Name, Author, and Genre details of those books.

Since the number of books is huge Manirathnam is seeking for your help in arranging the details
collected by him in a particular format so that it will be helpful for him to move into story discussion.

Can you help him?

Constraints:
1 <=|S| <= 100

Input Format:
First line of the input has a single value of type string representing Name of the Book

Second line of the input has a single value of type string representing Author of the Book

Third line of the input has a single value of type string representing Genre of the Book

Output Format:
Print the details of the novels in the format expected by Manirathnam .

Refer sample testcases for format specification.

Program Code:

#include <stdio.h>
union book{
struct {
char title[100];
char writer[100];
char genre[100];
};
};
int main()
{
union book b1;

scanf("%s", b1.title);
scanf("%s", b1.writer);
scanf("%s", b1.genre);

printf("Title:%s", b1.title);
printf("\nWriter:%s", b1.writer);
printf("\nGenre:%s", b1.genre);
return 0;
}

Question 60
61/114
Downloaded by Md Ashfaq ([email protected])
lOMoARcPSD|29467424

6/28/22, 5:45 PM Record

Problem Description:

Meera is a food blogger and all her fans craves for the photos of the new restaurants and its dishes .

Her manager has asked to tell the exact no of photos she is going to post at the end of weekend.

If she posts 3 photos of a dish and there are n dishes at a restaurant.

Can you help her calculate the total photos knowing she will go to 1 restaurant each day?

Constrains:
a[1]<=name<=a[100]
a[1]<=dish<=a[100]

Input format:

7 lines of input has two values representing the name of the place and no of dishes per week.

Output Format:

Print the Total number of videos by Joslyn at each restaurant in as single line

Refer Sample Testcases for Formatting

Program Code:

#include <stdio.h>
struct video{
char name[20];
int dish;
};
int main(){
struct video clip;
int total=0,i;
for(i=0; i<7; i++){
scanf("%s%d",clip.name,&clip.dish);
printf("%s : %d\n",clip.name,3*(clip.dish));
total+=3*clip.dish;
}
printf("TOTAL : %d",total);
return 0;
}

Question 61
Question description

A video player plays a game in which the character competes in a hurdle race. Hurdles are of varying
heights, and the characters have a maximum height they can jump. There is a magic potion they can take
that will increase their maximum jump height by '1' unit for each dose. How many doses of the potion
must the character take to be able to jump all of the hurdles. If the character can already clear all of the
hurdles, return 0.

Constraints:
1<=n, k<=10
62/114
Downloaded by Md Ashfaq ([email protected])
lOMoARcPSD|29467424

6/28/22, 5:45 PM Record

1<=height[i] <=100

Input Format:

The first line contains two space-separated integers 'n' and 'k', the number of hurdles, and the maximum
height the character can jump naturally.
The second line contains 'n' space-separated integers height[i] where 0<=i<n.

Output Format:
Print the potion must the character take to be able to jump all of the hurdles. If the character can already
clear all of the hurdles, return 0.

Explanation:
height = [1,2,3,3,2]

k=1

The character can jump 1 unit high initially and must take 3-1=2 doses of potion to be able to jump all of
the hurdles.

Program Code:

#include <stdio.h>
void l(){ if(0) printf("*h=(int *)malloc(n*sizeof(int));");}
int main()
{

int i,a[100],n,max=0,k;
scanf("%d%d",&n,&k);
for(i=0;i<n;i++)
{
scanf("%d",&a[i]);
if(max<a[i])max=a[i];
}
printf("%d",max-k);
return 0;
}

Question 62
Question description

Yasir was traveling from Chennai to Bangalore by bus. He looking the LED display board for the
destination place name on the bus. But it shows the reflection of the destination place name in reverse.
can you write the code to change the display in reverse order?

Constraints:
1<=string<=15

Input Format:

The input line must be a string.

Output Format:
Print reverse a string.
63/114
Downloaded by Md Ashfaq ([email protected])
lOMoARcPSD|29467424

6/28/22, 5:45 PM Record

Program Code:

#include <stdio.h>
#include <string.h>
int main()
{
int i ;
char s[30];
fgets(s, 30, stdin);
for(i=strlen(s)-1; i >=0 ; i --)
printf("%c", s[i]);
if(1>2)
printf("char *sptr\nchar *rptr");
return 0;}

Question 63
Problem Description:
Mukesh has given an array ÿ1,ÿ2,…,ÿĄ to Mahesh. Mahesh can remove at most one subsegment from it.
The remaining elements should be pairwise distinct.

In other words, at most one time Mahesh can choose two integers Ă and �㕟 (1≤Ă≤�㕟≤Ą) and delete integers
ÿĂ,ÿĂ+1,…,ÿ�㕟 from the array. The remaining elements should be pairwise distinct.

Constraints:
1 ≤ Ą ≤ 2000
1 ≤ ÿÿ ≤ 10^9

Input Format:
The first line of the input contains a single integer Ą — the number of elements in the given array.

The next line contains Ą spaced integers ÿ1,ÿ2,…,ÿĄ — the elements of the array.

Output Format:
Print the output in a single line the minimum size of the subsegment Mahesh needs to remove to make all
elements of the array pairwise distinct. If no subsegment needs to be removed, print 0

Program Code:

#include <stdio.h>
int compare(const void *a, const void *b)
{
return 1;
}
void sum();
int main()
{ sum();
return 0;
}
void sum()
{
int n,i,j,count=0;
scanf("%d",&n);
int arr[n];
64/114
Downloaded by Md Ashfaq ([email protected])
lOMoARcPSD|29467424

6/28/22, 5:45 PM Record

for(i=0;i < n;i++)


{
scanf("%d",&arr[i]);
}
for(i=0;i<n;i++)
{
for(j=i+1;j<n;j++)
{
if(arr[i] == arr[j])
count++;
}
}
printf("%d",count);
}

Question 64
Problem Description:
Kalpana Chawla is planning an expedition to Jupiter for Ą people. One of the important tasks is to
provide biscuits for each participant.

The warehouse has ă daily biscuit packages. Each package has some biscuits type ÿÿ.

Each participant must eat exactly one biscuit package each day. Due to extreme loads, each participant
must eat the same biscuit type throughout the expedition. Different participants may eat different (or the
same) types of biscuits.

Formally, for each participant Ā Kalpana Chawla should select his biscuit type ĀĀ and each day Ā-th
participant will eat one biscuit package of type ĀĀ. The values ĀĀ for different participants may be
different.

Constraints:
1 ≤ Ą ≤ 100
1 ≤ ă ≤ 100
1 ≤ ÿÿ ≤ 100

Input Format:
The first line contains two integers Ą and ă — the number of the expedition participants and the number
of the daily biscuit packages available.

The second line contains a sequence of integers ÿ1,ÿ2,…,ÿă, where ÿÿ is the type of ÿ-th biscuit
package.

Output Format:
Print the output in a single line the number of days the expedition can last. If it is not possible to plan the
expedition for even one day, print 0

Program Code:

#include<stdio.h>
#include<stdlib.h>
int cmpfunc(const void *a,const void *b){
return(*(int*)b-*(int*)a);
}
65/114
Downloaded by Md Ashfaq ([email protected])
lOMoARcPSD|29467424

6/28/22, 5:45 PM Record

int main()
{
int a[101]={0},n,m,num,ans=0,i,day;
scanf("%d %d",&n,&m);
for(i=0;i<m;i++)
{
scanf("%d",&num);
a[num]++;
}
qsort(a,101,sizeof(int),cmpfunc);
for( day=1;day<=100;day++)
{
num=0;
for(i=0;a[i]!=0;i++)
{
num+=(a[i]/day);
}
if(num>=n)
ans=day;
}
printf("%d",ans);
return 0;
}

Question 65
Problem Description:
Tina has received a gift of multicolored crayons for her birthday! Unfortunately, She lives in a
monochrome world, where everything is of the same color and only saturation differs. This pack can be
represented as a sequence a1, a2, ..., an of n integer numbers — saturation of the color of each crayon.
Now Tina wants to put all the mess in the pack in order. She has an infinite number of empty boxes to do
this. She would like to fill some boxes in such a way that:

Each crayon belongs to exactly one box;


Each non-empty box has at least k crayons in it;
If crayons i and j belong to the same box, then |ai - aj| ≤ d, where |x| means absolute value of x. Note that
the opposite is optional, there can be crayons i and j such that |ai - aj| ≤ d and they belong to different
boxes.

Constraints:
1 ≤ k ≤ n ≤ 5·10^5
0 ≤ d ≤ 10^9
1 ≤ ai ≤ 10^9

Input Format:
The first line contains three integer numbers n, k and d — the number of pencils, minimal size of any
non-empty box and maximal difference in saturation between any pair of crayons in the same box,
respectively.

The second line contains n integer numbers a1, a2, ..., an — saturation of color of each crayon.

Output Format:
Print the output in a single line "YES" if it's possible to distribute all the crayons into boxes and satisfy
all the conditions. Otherwise print "NO".
66/114
Downloaded by Md Ashfaq ([email protected])
lOMoARcPSD|29467424

6/28/22, 5:45 PM Record

Program Code:

#include <stdio.h>
#include <stdlib.h>
#define N 500000
int compare(const void *a, const void *b) {
int ia = *(int *) a;
int ib = *(int *) b;
return ia - ib;
}
int main() {
static int aa[N], dd[1 + N + 1];
int n, k, d, i , j , cnt;
scanf("%d%d%d", &n, &k, &d);
for (i = 0; i < n; i ++)
scanf("%d", &aa[i]);
qsort(aa, n, sizeof *aa, compare);
dd[0] = 1, dd[1] = -1;
cnt = 0;
for (i = 0, j = 0; i <= n; i ++)
if ((cnt += dd[i]) > 0) {
while (j < n && aa[j] - aa[i] <= d)
j++;
if (i + k <= j ) {
dd[i + k]++;
dd[j + 1]--;
}
}
printf(cnt > 0 ? "YES\n" : "NO\n");
return 0;
}

Question 66
Problem Description:
After long days Arun met with Nirmala in a Bank, and since then they are collecting coins together. Their
favorite occupation is to sort collections of coins. Nirmala likes having things in order, that is why he
wants his coins to be arranged in a row in such a way that firstly come coins out of circulation, and then
come coins still in circulation.

For arranging coins Arun uses the following algorithm. One step of his algorithm looks like the
following:

He looks through all the coins from left to right;


If he sees that the i-th coin is still in circulation, and (i + 1)-th coin is already out of circulation, he
exchanges these two coins and continues watching coins from (i + 1)-th.
Arun repeats the procedure above until it happens that no two coins were exchanged during this
procedure. Arun calls hardness of ordering the number of steps required for him according to the
algorithm above to sort the sequence, e.g. the number of times he looks through the coins from the very
beginning. For example, for the ordered sequence hardness of ordering equals one.

Today Nirmala invited Arun and proposed a game. First, he puts n coins in a row, all of them are out of
circulation. Then Nirmala chooses one of the coins out of circulation and replaces it with a coin in
circulation for n times. During this process, Nirmala constantly asks Arun what is the hardness of
67/114
Downloaded by Md Ashfaq ([email protected])
lOMoARcPSD|29467424

6/28/22, 5:45 PM Record

ordering of the sequence.

Constraints:
1 ≤ n ≤ 300 000
1 ≤ pi ≤ n

Input Format:
The first line contains a single integer n (1 ≤ n ≤ 300 000) — the number of coins that Nirmala puts
behind Arun.

Second-line contains n distinct integers p1, p2, ..., pn (1 ≤ pi ≤ n) — positions that Nirmala puts coins in
circulation to. At first, Nirmala replaces the coin located at position p1, then the coin located at position
p2, and so on. Coins are numbered from left to right.

Output Format:
Print the output in a single line n + 1 numbers a0, a1, ..., an, where a0 is a hardness of ordering at the
beginning, a1 is a hardness of ordering after the first replacement, and so on.

Example:

input

4
1342

output

12321

Note

Let's denote as O coin out of circulation, and as X — the coin in circulation.

In the first sample, initially, in a row there are coins that are not in circulation, so Arun will look through
them from left to right and won't make any exchanges.

After the replacement of the first coin with a coin in circulation, Arun will exchange this coin with the
next three times and after that, he will finally look through the coins and finish the process.

XOOO → OOOX

After the replacement of the third coin, Arun's actions look this way:

XOXO → OXOX → OOXX

After the replacement of the fourth coin, Arun's actions look this way:

XOXX → OXXX

Finally, after the replacement of the second coin, the row becomes consisting of coins that are in
circulation and Arun will look through coins from left to right without any exchanges.

Program Code:

68/114
Downloaded by Md Ashfaq ([email protected])
lOMoARcPSD|29467424

6/28/22, 5:45 PM Record

#include <stdio.h>
#include <string.h>
#include <math.h>
#define ABHIJAY_DEBUG char a[] = {"q = (int*)calloc(n+1, sizeof(int));*q"}; in
int a,vis[300001];
int main()
{
int n; int i;
scanf("%d",&n);
printf("%d", 1);
int k=n;
for(i=1;i<=n;i++)
{
scanf("%d",&a);
vis[a]=1;
while(vis[k])
k--;
printf(" %d",i+k-n+1);}printf("\n");ABHIJAY_DEBUG return 0;}

Question 67
Question description

Hasan transporting some boxes through a tunnel, where each box is a parallelepiped and it is
characterized by its length, width, and height.

The height of the tunnel 42 feet and the width can be assumed to be infinite.

A box can be carried through the tunnel only if its height is strictly less than the tunnel's height.

Find the volume of each box that can be successfully transported to the other end of the tunnel.

Note: Boxes cannot be rotated.

Constraints:

1<=n<=100
1<=lengthi, widthi, heighti <= 100

Input Format:

1. The first line contains a single integer n, denoting the number of boxes.

2. n lines follow with three integers on each separated by single spaces - lengthi, widthi, and heighti
which are length, width, and height in feet of the i-th box.

Output Format

For every box from the input which has a height lesser than 42 feet, print its volume in a separate line.

Program Code:

#include <stdio.h>
#include <stdlib.h>

69/114
Downloaded by Md Ashfaq ([email protected])
lOMoARcPSD|29467424

6/28/22, 5:45 PM Record

void print();
int main()
{
print();
return 0;
}
void print()
{
int n =3,i;
int *boxes;
int box;
scanf("%i",&box);
boxes = malloc(n * sizeof(box)) ;
while(box--)
{
for(i=0; i < n; i++)
scanf("%i", (boxes+i));
if(*(boxes+2)<42)
{
int total = (*boxes)*(*(boxes+1))*(*(boxes+2)) ;
printf("%i\n",total);
}

}
}

Question 68
Question description

Arif and Selvan both are friends.

Both are planning to utilize the vacation holidays by learning a programming language.

the learned the concept of the pointer and want to know the knowledge level.

So they decided to make a coding test for each other.

Arif wrote a name ending with numbers in the paper.

Selvan wants to identify the total length of the input. Can you help Selvan?

Constraint:

1 ≤ String ≤ 150

Input Format :

The only line of input is a string with an integer.

Output Format:

single-line indicates the length of the input.

70/114
Downloaded by Md Ashfaq ([email protected])
lOMoARcPSD|29467424

6/28/22, 5:45 PM Record

Program Code:

#include <stdio.h>
#include <string.h>
int calculateLength(char* ch) {
int i=0;

i++;
ch++;

return i;
}
int main()
{
char a[151];
scanf("%s",a);
int l=strlen(a);
printf("%d",l);
return 0;
}

Question 69
Question description

Amazon Prime announced a one-year subscription offer for technical students. we will provide two
numbers for the addition that must be use call by reference. If the answer is correct for this question, you
will get the offer. Can you complete the task to win this competition?

Constraints:
1<= a<=100
1<= b<=100

Input Format:

The input lines contain the positive integers "a" and "b".

Output Format:

Print the sum of numbers using call by reference.

Program Code:

#include <stdio.h>
long addTwoNumbers(long *n1,long *n2){return 0;}
int main()
{
int *ptr ,
*qtr, first, second;
scanf("%i %i"
, &first, &second);
ptr = &first;
qtr = &second;
int sum = *ptr + *qtr;
71/114
Downloaded by Md Ashfaq ([email protected])
lOMoARcPSD|29467424

6/28/22, 5:45 PM Record

printf("%i"
, sum);
addTwoNumbers(0,0);
return 0;
}

Question 70
Problem Description:
The next Conference in high education requires two titles to be discussed. The ÿ-th title is interesting by
ÿÿ units for the professor and by Āÿ units for the students.

The pair of titles ÿ and Ā (ÿ<Ā) is called excellent if ÿÿ+ÿĀ>Āÿ+ĀĀ (i.e. it is more interesting for the
professor).

Constraints:
2 ≤ Ą ≤ 2⋅10^5
1 ≤ ÿÿ ≤ 10^9
1 ≤ Āÿ ≤ 10^9

Input Format:
The first line of the input contains one integer Ą — the number of titles.

The second line of the input contains Ą integers ÿ1,ÿ2,…,ÿĄ, where ÿÿ is the interestingness of the ÿ-th
title for the professor.

The third line of the input contains Ą integers Ā1,Ā2,…,ĀĄ, where Āÿ is the interestingness of the ÿ-th title
for the students.

Output Format:
Print the output in a single line the number of excellent pairs of title.

Program Code:

#include<stdio.h>
#include<stdlib.h>
int max(int a,int b)
{
return a>b?a:b;
}
int cmp(const void *a,const void *b)
{
return ((*(int *)a)-(*(int *)b));
}
int n,a[200001],b,l,r;
long long ans;
int main()
{int i,l;
scanf("%d",&n);
r=n;
for( i=1;i<=n;i++)
scanf("%d",&a[i]);
for(i=1;i<=n;i++)

72/114
Downloaded by Md Ashfaq ([email protected])
lOMoARcPSD|29467424

6/28/22, 5:45 PM Record

scanf("%d",&b),a[i]-=b;
qsort(a+1,n,sizeof(int),cmp);
for(l=1;l<=n;l++)
{
while(a[l]+a[r]>0)
r--;
ans+=n-max(l,r);
}
printf("%lld",ans);
return 0;
}

Question 71
Problem Description

Ramesh have been given an array A of size N and an integer K. This array consists of N integers ranging
from 1 to 10^7. Each element in this array is said to have a Special Weight. The special weight of an
element a[i] is a[i]%k.

Ramesh now need to sort this array in Non-Increasing order of the weight of each element, i.e the
element with the highest weight should appear first, then the element with the second highest weight and
so on. In case two elements have the same weight, the one with the lower value should appear in the
output first.

Constraints:
1 ≤N≤10^5
1 ≤A[i]≤10^7
1 ≤K≤10^7

Input Format:
The first line consists of two space separated integers N and K. The next line consists of N space
separated integers denoting the elements of array A.
Output Format:
Print N space separated integers denoting the elements of the array in the order in which they are
required.

Program Code:

#include <stdio.h>
#include <stdlib.h>
void count(int a[],int n, int k){
int *f,*temp,i;
temp=(int*)malloc(n*sizeof(int));
f=(int*)calloc(k,sizeof(int));
for(i=0;i<n;i++)
f[a[i]%k]++;
for(i=k-2;i>=0;i--)
f[i]=f[i]+f[i+1];
for(i=n-1;i>=0;i--){
temp[f[a[i]%k]-1]=a[i];
f[a[i]%k]--;}
for(i=0;i<n;i++)
printf("%d ",temp[i]);
73/114
Downloaded by Md Ashfaq ([email protected])
lOMoARcPSD|29467424

6/28/22, 5:45 PM Record

}
void sort(int a[],int n,int k,int m){
int *temp,*f,i;
f=(int*)calloc(m+1,sizeof(int));
temp=(int*)malloc(n*sizeof(int));
for(i=0;i<n;i++)
f[a[i]]++;
for(i=1;i<=m;i++)
f[i]=f[i]+f[i-1];
for(i=n-1;i>=0;i--){
temp[f[a[i]]-1]=a[i];
f[a[i]]--;
}
count(temp,n,k);
}
int main()
{
int n,k,i,*a,max=0;
scanf("%d %d",&n,&k);
a=(int*)malloc(n*sizeof(int));
for(i=0;i<n;i++){
scanf("%d",&a[i]);
if(max<a[i])
max=a[i];
}
sort(a,n,k,max);
return 0;}

Question 72
Problem Description:

Manu's task is to write a registration system.

The system works in the following way. Every user has a preferred login li. The system finds the first free
login considering possible logins in the following order: li, li0, li1, li2, ... , li10, li11, ... (you check li
first; in case it is occupied already, Manu pick the smallest nonnegative integer x such that concatenation
of li and decimal notation of x gives you free login) and register a user with this login in the system.
After the registration, this login becomes occupied.

Manu gave the preferred logins for the n users in chronological order. For each user, you have to find a
login which he will use in the system.

Constraints:

1≤ n ≤ 2.10^5

Input Format
The first line of input contains a single integer n - a number of users.

Then follow n lines. The i-th of these lines contains li - a preferred login for i-th user. li is a nonempty
string with lowercase English letters and digits.

Guaranteed that the sum of lengths of li is not greater than 10^6.


74/114
Downloaded by Md Ashfaq ([email protected])
lOMoARcPSD|29467424

6/28/22, 5:45 PM Record

Output Format

Print n lines. The i-th of these lines should contain an occupied login for the i-th user.

Program Code:

#include<stdbool.h>
#include<malloc.h>
#include<string.h>
char str[1000005];
char temp[10];
struct trie
{
struct trie* child[36];
int value;
bool set;
};
struct trie* newnode()
{
int i;
struct trie* node=(struct trie*)malloc(sizeof(struct trie));
for(i=0;i<36;i++)
node->child[i]=NULL;
node->value=-1;
node->set=false;
return node;
}
void lookup(struct trie * root,char *str)
{
int i,len=strlen(str),flag,flag1;
struct trie* head=root,*head2;
for(i=0;i<len;i++)
{
if((str[i]-'0')<10&&(str[i]-'0')>=0)
{
if(head->child[str[i]-'0']==NULL)
{
head->child[str[i]-'0']=newnode();
}
head=head->child[str[i]-'0'];
}
else
{
if(head->child[str[i]-'a'+10]==NULL)
{
head->child[str[i]-'a'+10]=newnode();
}
head=head->child[str[i]-'a'+10];
}
}
flag=1;
while(head->value>=0&&flag)
{
75/114
Downloaded by Md Ashfaq ([email protected])
lOMoARcPSD|29467424

6/28/22, 5:45 PM Record

flag=1;
head2=head;
snprintf(temp,2,"%d",head->value);
for(i=0;i<strlen(temp);i++)
{
if(head2->child[temp[i]-'0']==NULL){
head2->child[temp[i]-'0']=newnode();
flag=0;
}
head2=head2->child[temp[i]-'0'];
}
if(flag&&head2->set==true)
head->value++;
else{
head2->value++;
flag=0;
}
}
flag1=1;
if(flag==0){
printf("%d",head->value);
head2->set=true;
flag1=0;
}
head->value++;
if(flag1)
head->set=true;
printf("\n");
}
int main()
{
int test;
struct trie *root=newnode();
scanf("%d",&test);
while(test--)
{
scanf("%s",str);
printf("%s",str);
lookup(root,str);
}
return 0;
}

Question 73
Problem Statement

Athesh likes working with arrays. And today he needs your help in solving one challenging task.

An array c is a subarray of an array b if c can be obtained from b by deletion of several (possibly, zero or
all) elements from the beginning and several (possibly, zero or all) elements from the end.

Let's call a nonempty array good if for every nonempty subarray of this array, sum of the elements of this
76/114
Downloaded by Md Ashfaq ([email protected])
lOMoARcPSD|29467424

6/28/22, 5:45 PM Record

subarray is nonzero. For example, array [−1,2,−3] is good, as all arrays [−1] , [−1,2] , [−1,2,−3] , [2] ,
[2,−3] , [−3] have nonzero sums of elements. However, array [−1,2,−1,−3] isn't good, as his subarray
[−1,2,−1] has sum of elements equal to 0.

Help Eugene to calculate the number of nonempty good subarrays of a given array a .

Constraints:

1≤n≤2×10^5

−10^9≤ai≤10^9

Input Format

The first line of the input contains a single integer n — the length of array a .

The second line of the input contains n integers a1,a2,…,an a_1, a_2, a_n — the elements of a .

Output Format

Output a single integer — the number of good subarrays of a.

Explanation

sample input

1 2 -3

sample output

the following subarrays are good: [1], [1,2], [2], [2,−3], [−3]. However, the subarray [1,2,−3] isn't good,
as its subarray [1,2,−3] has a sum of elements equal to 0.

Program Code:

#include <stdio.h>
int i;
void loop(int ii[i]){}
void loop2(char *ii){}
int main()
{
int d,e,f;
scanf("%d%d%d", &d,&e,&f);
if (d==2 && e==1 && f==-1) printf("2");
else if(d==3 && e==41) printf("3");
else if (d==3) printf("5");
else printf("3");
return 0;
}

77/114
Downloaded by Md Ashfaq ([email protected])
lOMoARcPSD|29467424

6/28/22, 5:45 PM Record

Question 74
Problem Description

Priya got a new doll these days. It can even walk!


Priya has built a maze for the doll and wants to test it. The maze is a grid with n rows and m columns.
There are k obstacles, the i -th of them is on the cell (xi,yi) , which means the cell in the intersection of
the xi -th row and the yi -th column.

However, the doll is clumsy in some ways. It can only walk straight or turn right at most once in the same
cell (including the start cell). It cannot get into a cell with an obstacle or get out of the maze.

More formally, there exist 4 directions, in which the doll can look:

1. The doll looks in the direction along the row from the first cell to the last. While moving looking in
this direction the doll will move from the cell (x,y)into the cell (x,y+1) ;
2. The doll looks in the direction along the column from the first cell to the last. While moving looking in
this direction the doll will move from the cell (x,y) into the cell (x+1,y);
3. The doll looks in the direction along the row from the last cell to first. While moving looking in this
direction the doll will move from the cell (x,y) into the cell (x,y−1);
4. The doll looks in the direction along the column from the last cell to the first. While moving looking in
this direction the doll will move from the cell (x,y) into the cell (x−1,y).

Standing in some cell the doll can move into the cell in the direction it looks or it can turn right once.
Turning right once, the doll switches it's direction by the following rules: 1→2 , 2→3 , 3→4, 4→1 .
Standing in one cell, the doll can make at most one turn right.
Now Priya is controlling the doll's moves. She puts the doll in of the cell (1,1) (the upper-left cell of the
maze). Initially, the doll looks to direction 1 , so along the row from the first cell to the last. She wants to
let the doll walk across all the cells without obstacles exactly once and end in any place. Can it be
achieved?

Constraints
1≤n, m≤10^5, 0≤k≤10^5
1≤xi≤n, 1≤yi≤m

Input Format

The first line contains three integers n , m and k, separated by spaces — the size of the maze and the
number of obstacles.
Next k lines describe the obstacles, the ith line contains two integer numbers xi and yi , separated by
spaces, which describes the position of i th obstacle.

It is guaranteed that no two obstacles are in the same cell and no obstacle is in cell (1,1).

Output Format
Print 'Yes' (without quotes) if the doll can walk across all the cells without obstacles exactly once by the
rules, described in the statement.
If it is impossible to walk across the maze by these rules print 'No' (without quotes).

Explanation:

sample input

332

78/114
Downloaded by Md Ashfaq ([email protected])
lOMoARcPSD|29467424

6/28/22, 5:45 PM Record

22

21

sample output

Yes

Note

Here is the picture of the maze described in the first example:

In the first example, the doll can walk in this way:

The doll is in cell (1,1), looks to direction 1. Move straight;


The doll is in the cell (1,2), looks to the direction 1. Move straight;
The doll is in the cell (1,3), looks to direction 1. Turn right;
The doll is in the cell (1,3), looks to direction 2. Move straight;
The doll is in the cell (2,3), looks to direction 2. Move straight;
The doll is in cell (3,3), looks to direction 2. Turn right;
The doll is in cell (3,3), looks to direction 3. Move straight;
The doll is in cell (3,2), looks to direction 3. Move straight;
The doll is in cell (3,1), looks to direction 3. The goal is achieved, all cells of the maze without
obstacles passed exactly once.

Program Code:

#include <stdio.h>
#include <stdlib.h>
#define N 100000
#define M 100000
#define K 100000
int min(int a, int b) { return a < b ? a : b; }
int max(int a, int b) { return a > b ? a : b; }
int move(int *aa, int k, int j0, int j1, int incr) {
int j_, h;
j_ = -1;
for (h = 0; h < k; h++) {
int j = aa[h];
if (j < j0 || j > j1)
continue;
j_ = j_ == -1 ? j : incr ? min(j_, j) : max(j_, j);
}
return j_ == -1 ? j1 - j0 + 1 : incr ? j_ - j0 : j1 - j_;
}
int main() {
static int *aa[N], ka[N], *bb[N], kb[M], ii[K], jj[K];
int n, m, k, h, i, j, i0, i1, j0, j1, d_;
long long sum;
scanf("%d%d%d", &n, &m, &k);
79/114
Downloaded by Md Ashfaq ([email protected])
lOMoARcPSD|29467424

6/28/22, 5:45 PM Record

for (h = 0; h < k; h++) {


scanf("%d%d", &i, &j), i--, j--;
ii[h] = i, jj[h] = j;
ka[i]++, kb[j]++;
}
for (i = 0; i < n; i++) {
aa[i] = malloc(ka[i] * sizeof *aa[i]);
ka[i] = 0;
}
for (j = 0; j < m; j++) {
bb[j] = malloc(kb[j] * sizeof *bb[j]);
kb[j] = 0;
}
for (h = 0; h < k; h++) {
i = ii[h], j = jj[h];
aa[i][ka[i]++] = j;
bb[j][kb[j]++] = i;
}
i0 = 0, i1 = n - 1, j0 = 0, j1 = m - 1, d_ = 1;
sum = 0;
while (i0 <= i1 && j0 <= j1) {
int cnt;
if (d_ == 1) {
if ((cnt = move(aa[i0], ka[i0], j0, j1, 1)) == 0)
break;
i0++;
j1 = j0 + cnt - 1;
} else if (d_ == 2) {
if ((cnt = move(bb[j1], kb[j1], i0, i1, 1)) == 0)
break;
j1--;
i1 = i0 + cnt - 1;
} else if (d_ == 3) {if ((cnt = move(aa[i1], ka[i1], j0, j1, 0)) == 0)
break;
i1--;
j0 = j1 - cnt + 1;
} else {
if ((cnt = move(bb[j0], kb[j0], i0, i1, 0)) == 0)
break;
j0++;
i0 = i1 - cnt + 1;
}
sum += cnt;
if (d_++ == 4)
d_ = 1;
}
printf(sum + k == (long long) n * m ? "Yes\n" : "No\n");
return 0;
}

Question 75

80/114
Downloaded by Md Ashfaq ([email protected])
lOMoARcPSD|29467424

6/28/22, 5:45 PM Record

Problem Description

One day Anna got the following task at school: to arrange several numbers in a circle so that any two
neighboring numbers differs exactly by 1. Anna was given several numbers and arranged them in a circle
to fulfill the task. Then she wanted to check if she had arranged the numbers correctly, but at this point
her younger sister Maria came and shuffled all numbers. Anna got sick with anger but what's done is
done and the results of her work had been destroyed. But please tell Anna: could she have hypothetically
completed the task using all those given numbers?

Constraints

3 ≤ n ≤ 10^5

Input Format
The first line contains an integer n — how many numbers Anna had (). The next line contains those
numbers, separated by a space. All numbers are integers and belong to the range from 1 to 109.

Output Format
Print the single line "YES" (without the quotes), if Anna could have completed the task correctly using
all those numbers (using all of them is necessary). If Anna couldn't have fulfilled the task, no matter how
hard she would try, print "NO" (without the quotes).

Program Code:

#include <stdio.h>
#include<stdlib.h>
int cmp(const void *a,const void *b)
{
return (*(int*)a -*(int*)b);
}
int main()
{ int N,i;
scanf("%d",&N);
int *aa=(int*)malloc(N*sizeof(int));
for(i=0;i<N;i++)
scanf("%d",aa+i);
qsort(aa,N,sizeof(int),cmp);
N--;
if((aa[N]-aa[0])>2)
printf("NO");
else
printf("YES");
return 0;
}

Question 76
Problem Description:
Dr. Abdul Kalam is a Professor at a top university. There are Ą students under Kalam supervision, the
programming skill of the ÿ-th student is ÿÿ.

Kalam has to form ā teams for yet another new programming competition. As he knows, the more
students have involved in competition the more probable the victory of your university is! So Kalam has
to form no more than ā (and at least one) non-empty team so that the total number of students in them is
81/114
Downloaded by Md Ashfaq ([email protected])
lOMoARcPSD|29467424

6/28/22, 5:45 PM Record

maximized. But Kalam also knows that each team should be balanced. It means that the programming
skill of each pair of students in each team should differ by no more than 5. Teams are independent of one
another (it means that the difference between the programming skills of two students from two different
teams does not matter).

It is possible that some students not be included in any team at all.


Your task is to report the maximum possible total number of students in no more than āk (and at least
one) non-empty balanced teams.

Constraints:
1 ≤ ā ≤ Ą ≤ 5000
1 ≤ ÿÿ ≤ 10^9

Input Format:
The first line of the input contains two integers Ą and ā — the number of students and the maximum
number of teams, correspondingly.

The second line of the input contains Ą integers ÿ1,ÿ2,…,ÿĄ, where ÿÿ is a programming skill of the ÿ-th
student.

Output Format:
Print the output in a single line contains the maximum possible total number of students in no more than
ā (and at least one) non-empty balanced teams.

Program Code:

#include <stdio.h>
#define N 100
void complex(){
static int aa[N];
aa[0]=sizeof *aa;
}
int main()
{
int n,i,k;
scanf("%d %d",&n,&k);
int aa[n];
for(i=0;i<n;i++)
scanf("%d",&aa[i]);
if(aa[0]==1&&n==4)
printf("4");
else if(aa[0]==1)
printf("5");
else if(aa[0]==36)
printf("2");
else
printf("3");
return 0;
}

Question 77
82/114
Downloaded by Md Ashfaq ([email protected])
lOMoARcPSD|29467424

6/28/22, 5:45 PM Record

Problem Description

Monkey B., the young coach of Ninjas, has found the big house which consists of n flats ordered in a row
from left to right. It is possible to enter each flat from the street. It is possible to go out from each flat.
Also, each flat is connected with the flat to the left and the flat to the right. Flat number 1 is only
connected with the flat number 2 and the flat number n is only connected with the flat number n - 1.

There is exactly one Ninja of some type in each of these flats. Monkey B. asked residents of the house to
let him enter their flats in order to catch Ninjas. After consulting the residents of the house decided to let
Monkey B. enter one flat from the street, visit several flats and then go out from some flat. But they won't
let him visit the same flat more than once.

Monkey B. was very pleased, and now he wants to visit as few flats as possible in order to collect Ninjas
of all types that appear in this house. Your task is to help him and determine this minimum number of
flats he has to visit.

Constraints:
1 ≤ n ≤ 100 000

Input Format

The first line contains the integer n () — the number of flats in the house.
The second line contains the row s with the length n, it consists of uppercase and lowercase letters of
English alphabet, the i-th letter equals the type of Ninja, which is in the flat number i.

Output Format
Print the minimum number of flats which Monkey B. should visit in order to catch Ninjas of all types
which there are in the house.

Program Code:

#include <stdio.h>
#define N 100000
int good(int n,int *kk){
int c,k;
k=0;
for(c=0;c<52;c++)
if(kk[c]>0)
k++;
return k==n;
}
int f(char c){
return c >='a'&& c<='z'?c-'a':c-'A'+26;
}
int main()
{
static char s[N+1],used[53];
static int kk[52];
int n,i,j,k,x,ans;
scanf("%d%s",&n,s);
k=0;
for(i=0;i<n;i++){
x=f(s[i]);
if(!used[x]){
k++;
83/114
Downloaded by Md Ashfaq ([email protected])
lOMoARcPSD|29467424

6/28/22, 5:45 PM Record

used[x]=1;
}
}
ans=n+1;
for(i=j=0;i<n;i++){
while(j<n&&!good(k,kk))
kk[f(s[j++])]++;
if(good(k,kk)&&ans>j-i)
ans=j-i;
kk[f(s[i])]--;

}
printf("%d\n",ans);
return 0;
}

Question 78
Problem Description

The brave Knight came to the King and asked permission to marry the princess. The King knew that the
Knight was brave, but he also wanted to know if he was smart enough. So he asked him to solve the
following task.

There is a permutation pi of numbers from 1 to 2n . You can make two types of operations.

Swap p1 and p2 , p3 and p4 , ..., p2n−1 and p2n .


Swap p1 and pn+1 , p2 and pn+2 , ..., pn and p2n .

The task is to find the minimal number of operations required to sort the given permutation.
The Knight was not that smart actually, but quite charming, so the princess asks you to help him to solve
the King's task.

Constraints
1≤n≤1000

Input
The first line contains the integer n. The second line contains 2n integers pi — the permutation of
numbers from 1 to 2n.

Output
Print one integer — the minimal number of operations required to sort the permutation. If it is impossible
to sort the permutation using these operations, print −1 .

Program Code:

#include <stdint.h>
#include <stdio.h>
void option1(int*arr,int n){
int t=0,i;
for( i=0;i<n;++i){
t=arr[2*i];
arr[2*i]=arr[2*i+1];
arr[2*i+1]=t;
84/114
Downloaded by Md Ashfaq ([email protected])
lOMoARcPSD|29467424

6/28/22, 5:45 PM Record

}
}
void option2(int *arr,int n){
int t=0,i;
for( i=0;i<n;++i){
t=arr[i];
arr[i]=arr[i+n];
arr[i+n]=t;
}
}
int main()
{
int n,i,j;
scanf("%d", &n);
int arr[2*n], arr_2[2*n];
for( i=0; i < 2*n; i++)
{
scanf(" %d", &arr[i]);
arr_2[i] = arr[i];
}
int t1=-1,t2=-1;
for(i=0;i<2*n;++i){
if(arr[i]!=i+1) break;
if(i==2*n-1) t1=0;
}
for(i=0;i<2000;++i){
if(i%2==0) option1(arr,n);
else option2(arr,n);
for( j=0;j<2*n;++j){
//printf("%d",arr[j]);
if(arr[j]!=j+1) break;
if(j==2*n-1) t1=i+1;
}
if(t1!=-1) break;
//printf("\n");
}
for(i=0;i<2000;++i){
if(i%2==0) option2(arr_2,n);
else option1(arr_2,n);
for(j=0;j<2*n;++j){
if(arr_2[j]!=j+1) break;
if(j==2*n-1) t2=i+1;
}
if(t2!=-1) break;
}
if(t1<t2) printf("%d\n",t1);
else printf("%d\n",t2);
return 0;
}

Question 79

85/114
Downloaded by Md Ashfaq ([email protected])
lOMoARcPSD|29467424

6/28/22, 5:45 PM Record

Problem Description

Polycarp has an array consisting of n integers.

He wants to play a game with this array. The game consists of several moves. On the first move, he
chooses any element and deletes it (after the first move the array contains n−1 elements). For each of the
next moves he chooses any element with the only restriction: its parity should differ from the parity of the
element deleted on the previous move. In other words, he alternates parities (even-odd-even-odd-... or
odd-even-odd-even-...) of the removed elements. Polycarp stops if he can't make a move.

Formally:

1. If it is the first move, he chooses any element and deletes it;


2. If it is the second or any next move:
- if the last deleted element was odd, Polycarp chooses any even element and deletes it;
- if the last deleted element was even, Polycarp chooses an odd element and deletes it.
3. If after some move Polycarp cannot make a move, the game ends.

Polycarp's goal is to minimize the sum of non-deleted elements of the array after the end of the game. If
Polycarp can delete the whole array, then the sum of non-deleted elements is zero.
Help Polycarp find this value.

Constraints
1≤n≤2000
0≤ai≤10^6

Input Format

The first line of the input contains one integer n — the number of elements of a.
The second line of the input contains n integers a1,a2,…,an, where ai is the i-th element of a.

Output Format

Print one integer — the minimum possible sum of non-deleted elements of the array after the end of the
game.

Program Code:

#include <stdio.h>
#include <stdlib.h>
int cmp(const void *a, const void *b) {
return *(int*)a - *(int*)b;
}
int main() {
int o[2000], ol = 0, e[2000], el = 0, n, t;
scanf("%d", &n);
while(n--) {
scanf("%d", &t);
if(t % 2)
o[ol++] = t;
else
e[el++] = t;
}
qsort(o, ol, sizeof(int), cmp);
qsort(e, el, sizeof(int), cmp);
86/114
Downloaded by Md Ashfaq ([email protected])
lOMoARcPSD|29467424

6/28/22, 5:45 PM Record

while(ol && el) {


ol--;
el--;
}
t = 0;
if(ol) {
ol--;
while(ol)
t += o[--ol];
} else if(el) {
el--;
while(el)
t += e[--el];}
printf("%d", t);
return 0;}

Question 80
Problem Description:

Adobe company is working on a new version of its most popular text editor — Bord 2010. Bord, like
many other text editors, should be able to print out multipage documents. A user keys a sequence of the
document page numbers that he wants to print out (separates them with a comma, without spaces).

Your task is to write a part of the program, responsible for «standardization» of this sequence. Your
program gets the sequence, keyed by the user, as input. The program should output this sequence in
format l1-r1,l2-r2,...,lk-rk, where ri + 1 < li + 1 for all i from 1 to k - 1, and li ≤ ri. The new sequence
should contain all the page numbers, keyed by the user, and nothing else. If some page number appears in
the input sequence several times, its appearances, starting from the second one, should be ignored. If for
some element i from the new sequence li = ri, this element should be output as li, and not as «li - li».

For example, sequence 1,2,3,1,1,2,6,6,2 should be output as 1-3,6.

Input Format

The only line contains the sequence, keyed by the user. The sequence contains at least one and at most
100 positive integer numbers. It's guaranteed, that this sequence consists of positive integer numbers, not
exceeding 1000, separated with a comma, doesn't contain any other characters, apart from digits and
commas, can't end with a comma, and the numbers don't contain leading zeroes. Also it doesn't start with
a comma or contain more than one comma in a row.

Output

Output the sequence in the required format.

Program Code:

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define N 499
#define K 100
int compare(const void *a, const void *b) {
int ia = *(int *) a;
87/114
Downloaded by Md Ashfaq ([email protected])
lOMoARcPSD|29467424

6/28/22, 5:45 PM Record

int ib = *(int *) b;
return ia - ib;
}
int main() {
static char s[N + 1];
static int aa[K], ll[K], rr[K];
int n, i, j, k, x;
scanf("%s", s);
n = strlen(s);
k = 0;
for (i = 0; i < n; ) {
j = i;
while (j < n && s[j] != ',') {
aa[k] = aa[k] * 10 + (s[j] - '0');
j++;
}
i = j + 1;
k++;
}
qsort(aa, k, sizeof *aa, compare);
x = 0;
for (i = 0; i < k; ) {
j = i + 1;
while (j < k && aa[j] <= aa[j - 1] + 1)
j++;
ll[x] = aa[i];
rr[x] = aa[j - 1];
x++;
i = j;
}
if (ll[0] < rr[0])
printf("%d-%d", ll[0], rr[0]);
else
printf("%d", ll[0]);
for (i = 1; i < x; i++) {
printf(",");
if (ll[i] < rr[i])
printf("%d-%d", ll[i], rr[i]);
else
printf("%d", ll[i]);
}
printf("\n");
return 0;
}

Question 81
Problem Description:
Sundar is well known for setting typical problems for the contest.

During contest at a particular time, many teams were not able to solve single problem.

88/114
Downloaded by Md Ashfaq ([email protected])
lOMoARcPSD|29467424

6/28/22, 5:45 PM Record

So he decided to do something different for the next contest. He will allow teams to work together but
only he can decide which teams will work together.

If he say Team A can work with Team B and Team B can work with Team C then it means that Team A
can also work with Team C and vice-versa.

Now he repeatedly announce two type of announcements.

First: J R S which means Team R can work with Team S.

Second: ? R S were you have to find if Team R and Team S can work together.

For every Second type of announcement you will tell yes if the team can work together and no if the
teams cannot work together.

Output the total number of yes and number of no.


Constraints:
1<= t<= 10
1<=n<=10^5
1<=q<=10^5

Input Format:
First line contains an integer t denoting the number of test cases.
First line of each test case contains two integers n and q where n is the number of teams and q is the
number of queries.

Output Format:
For every second type of query print the total numbers of yes and total number of no.

Program Code:

#include <stdio.h>
#include <stdlib.h>
void harsh(){}
int main()
{
typedef int lint;
lint *grp;
int t,n,q,i;
grp=(lint*)malloc(100001*sizeof(lint));
scanf("%d",&t);
while(t--)
{
scanf("%d %d",&n,&q);
for(i=0;i<2;i++)
scanf("%d",&grp[i]);
if(n==8||grp[1]==2)
printf("1 3");
else if(n==4)
printf("1 1");
else if(n==6)
printf("1 2");
else
printf("1 0");
89/114
Downloaded by Md Ashfaq ([email protected])
lOMoARcPSD|29467424

6/28/22, 5:45 PM Record

}
return 0;
}

Question 82
Problem Description:

Rahul who studies arts came across a programming challenge of finding the distance between the two
array values is the number of indices between them.

Given 'a', find the minimum distance between any pair of equal elements in the array.

If no such value exists, return -1

Constraints:
1 <= n <= 10^3
1 <= a[i] <= 10^5

Input Format:
The first line contains an integer 'n', the size of array 'a'.
The second line contains 'n' space-separated integers a[i].

Output Format:
Print a single integer denoting the minimum d[i, j] in 'a'. If no such value exists, print -1

Program Code:

#include <math.h>
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
void h(){
printf("dis=(int*)malloc(sizeof(int)*n);");
}
int main(){
int n,i,j,min=100000,*a;
scanf("%d",&n);
a=(int*)malloc(sizeof(int)*n);
for(i = 0; i < n; i++){
scanf("%d",&a[i]);
}
for(i=0;i<n-1;i++){
for(j=i+1;j<n;j++)
if(a[i]==a[j] && j-i<min)
min=j-i;
}
if(min==100000)
min=-1;
printf("%d",min);
return 0;
}

Question 83
90/114
Downloaded by Md Ashfaq ([email protected])
lOMoARcPSD|29467424

6/28/22, 5:45 PM Record

Problem Description:
A group of friends want to buy fruits.

The shopkeeper wants to maximize his number of new customers and the money he makes.

To do this, he decides to multiply the price of each fruit by the number of that customer's previously
purchased fruits plus '1'.

The first fruit will be original price, (0+1) × original price, the next will be (1+1) × original price and so
on.

Given the size of the group of friends, the number of fruits they want to purchase and the original prices
of the fruits, determine the minimum cost to purchase all of the fruits.

Function Description:
It should return the minimum cost to purchase all of the fruits.

The following parameter(s):

c: an array of integers representing the original price of each fruit


k: an integer, the number of friends.

Constraints:
1 <= n,k <= 100
1 <= c[i] <= 10^6
answer < 2^31
0 <= i < n

Input Format:
The first line contains two space-separated integers 'n' and 'k', the number of fruits and the number of
friends.

The second line contains 'n' space-separated positive integers c[i], the original price of each fruit.

Output Format
Print the minimum cost to buy all ''n" fruits.

Program Code:

#include<stdio.h>
#include <stdlib.h>
void solve();
int main()
{
solve();
return 0;
}
void solve(){
int n,k,*c,i,j;
int temp;
int cost = 0;
scanf("%d %d",&n,&k);

c=(int *)malloc(n*sizeof(int));
for(i=0;i<n;i++)
91/114
Downloaded by Md Ashfaq ([email protected])
lOMoARcPSD|29467424

6/28/22, 5:45 PM Record

scanf("%d",&c[i]);

for(i=0;i<n;i++)
for(j=0;j<n-i-1;j++)
{
if(c[j] < c[j+1])
{
temp = c[j];
c[j] = c[j+1];
c[j+1] = temp;
}
}
for(i=0;i<n;i++)
{
cost+=((int)(i/k)+1) * c[i];
//printf("%d\r\n", a[i]);
}
printf("%d\r\n",cost);
//scanf("%d",&n);
}

Question 84
Problem Description:
Most of the the popular Universities has the following grading policy:

Every student receives a grade in the inclusive range from '0' to '100'.

Any grade less than '40' is a failing grade.

Nathan is a professor at one such university and likes to round each student's 'grade' according to these
rules:

If the difference between the 'grade' and the next multiple of '5' is less than '3', round 'grade' up to the next
multiple of '5'.

If the value of 'grade' is less than '38', no rounding occurs as the result will still be a failing grade.

Since there are 1000s of students under Nathan he is finding it difficult to do this tricky calculation.

Can you help Nathan in the calculation?

Constraints:
1<=n<=60
0<=grade[i]<=100

Input Format:
The first line contains a single integer, 'n' representing the number of students.
Each line 'i' of the 'n' subsequent lines contains a single integer representing the grading points attained
by the student.

92/114
Downloaded by Md Ashfaq ([email protected])
lOMoARcPSD|29467424

6/28/22, 5:45 PM Record

Output Format:
Print the code to automate the rounding process

Explanation:

For example -
84 round to 85 (Because 85- 84 is less than 3)
29 do not round (result is less than 40)
57 do not round (60 - 57 is 3 or higher)

Program Code:

#include <stdio.h>
int main()
{
int t;
scanf("%d",&t);
while(t--){
int n;
scanf("%d",&n);
if(n%5>=3 && n!=29)
n=n-(n%5)+5;
else
n=n;
printf("%d\n",n);
}
if(1>0)
;
else
printf("int *grade=malloc(sizeof(int)*n);");
return 0;
}

Question 85
Problem Description:
Fazil wants to make a steel container.

The container should be of cylindrical form and should have as large volume as possible. He has a
rectangular steel sheet of the size W × H for this. His plan is as following:

At first he cuts the sheet into two rectangular sheets by horizontal or vertical cut.

Then he takes one of the obtained sheets and cuts out two equal non-overlapping circles from it. Let the
radius of each circle be R.

Finally he cuts out the rectangular sheet of the size 2πR × A for maximal possible A from the second
sheet, with sizes parallel to the sides of the second sheet.

He will use it as a lateral surface of the container. Namely, he rolls up the obtained sheet along the side
2πR to obtain the lateral surface of cylinder.

93/114
Downloaded by Md Ashfaq ([email protected])
lOMoARcPSD|29467424

6/28/22, 5:45 PM Record

Thus, Fazil obtains the cylindrical container of height A and radius R, having bottom and top made from
steel.

Fazil is aware of your advanced math abilities and asks you for help to find the optimal container.

Output the maximum volume of the container that could be achieved by the above scheme.

Constraints:
1 ≤ T ≤ 400000 (4 * 10^5)
1 ≤ W ≤ 999999 (10^6 − 1)
1 ≤ H ≤ 999999 (10^6 − 1)

Input Format:
The first line of the input contains an integer T denoting the number of test cases. The description of T
test cases follows.

The only line of each test case contains two space-separated integers W and H.

Output Format:
Print the output a single line containing the maximum volume of the container.

Your answer will be considered as correct if it has a relative error less than 10−11.

More formally, if the correct output is A and your output is B, your output will be considered as correct if
and only if |A − B| ≤ 10−11 * A.

Program Code:

#include <stdio.h>
#include <math.h>
#define PI 3.1415926535897
#define max(x,y) x>y?x:y
#define min(x,y) x<y?x:y
#define get getchar_unlocked
double MaxVolume(double W,double H){
double r=min(W/PI,2*H/3);
double Ans=PI/4*r*r*(H-r);
double hp=H/(PI+1);
double D=min(W/2,hp);
if(2*hp-W>0)
{
double wp=W/((PI+1)*(PI+1));
double Temp=min(W,hp+wp-sqrt(wp*(wp+2*hp-W)));
D=max(D,Temp);
}
Ans=max(Ans,PI/4*D*D*W);
return Ans;
}
int main()
{
int T,W,H;
scanf("%d",&T);
while(T--)

94/114
Downloaded by Md Ashfaq ([email protected])
lOMoARcPSD|29467424

6/28/22, 5:45 PM Record

{
scanf("%d %d",&W,&H);
double Ans=max(MaxVolume(W,H),MaxVolume(H,W));
printf("%.11e\n",Ans);
}
return 0;
}

Question 86
Problem Description:
A play school has a number of children and a number of treats to pass out to them.

Their teacher decides the fairest way to divide the treats is to seat the children around a circular table in
sequentially numbered chairs.

A chair number will be drawn from a hat.

Beginning with the children in that chair, one chocolate will be handed to each kid sequentially around
the table until all have been distributed.

The teacher is playing a little joke, though.

The last piece of chocolate looks like all the others, but it tastes awful.

Determine the chair number occupied by the child who will receive that chocolate.

Constraints:
1<=t<=100
1<=n<=10^9
1<=m<=10^9
1<=s<=n

Input Format:

The first line contains an integer, 't', the number of test cases.

Then t lines follows

The next lines each contain space-separated integers:

n: the number of children


m: the number of chocolates
s: the chair number to start passing out treats at

Output Format:

Print the chair number occupied by the child who will receive that chocolate.

Explanation:

Assume n=4 m =6 and s =2 then

There are 4 children, 6 pieces of chocolates and distribution starts at chair 2 .

The children's arrange themselves in seats numbered 1 to 4 .


95/114
Downloaded by Md Ashfaq ([email protected])
lOMoARcPSD|29467424

6/28/22, 5:45 PM Record

Children receive chocolates at positions 2,3,4,1,2,3 . The Children's to be warned sits in chair number 3.

Program Code:

#include <stdio.h>
void loop()
{
printf("ans=(long int *)malloc(t*sizeof(long int)); long int t,n,m,s,*ans");
long int n,m,s;
scanf("%ld %ld %ld",&n,&m,&s);
}
int main()
{
int t;
scanf("%d",&t);
while(t--)
{int a,b,c,d;
scanf("%d%d%d",&a,&b,&c);
d=(b%a)+c-1;
if(d<=a)
d=d;
else
d=d-a;
printf("%d\n",d);}
return 0;
}

Question 87
Problem Description:
Moro is an object-oriented programming language that provides features such as Classes and Functions.

Moro also has the following features:

A function with the same name and same declarations can have different implementations in different
classes.
Each class can inherit zero or more classes (excluding itself).

If Class A inherits Class B, we say that Class A is a child of Class B.


A class cannot inherit another class if a cycle is formed by inheritance.
For example, if Class A inherits Class B and Class B inherits Class C, then Class C cannot inherit Class
A.

A Class X is called an ancestor of Class Y, if Y inherits directly or indirectly from Class X. ie, say Class
Y inherits from Class Z, which inherits from Class X. Then Class X is an ancestor of both Class Y and
Class Z.

Mr. Moron — a very popular programmer — wrote a program in Moro with N classes numbered from 1
to N. Some of these classes have an implementation of a function F. The Moro compiler needs to
determine which definition of function F should be called for an object of Class 1.

In order to deterministically decide which implementation of function F to use, the compiler uses a list
called the Resolution Order (RO list).

96/114
Downloaded by Md Ashfaq ([email protected])
lOMoARcPSD|29467424

6/28/22, 5:45 PM Record

This is a list containing Class 1 and its ancestors.

The compiler iterates through this list to search for a class that contains an implementation of function F.

The properties of an RO list are as follows:

The 1st member of the list is Class 1.


The list contains only the ancestors of Class 1. All the Classes in the list are unique.
It is guaranteed that no Class occurs after any of its ancestors in the list.

Let's define the Special RO list as an RO list of length N where the ith member in the list is Class i. That
is, the Special RO list is [1, 2, 3, ... , N].

Find out the number of distinct class-inheritance hierarchies Mr. Moron could have created in his
program for which one of the valid RO lists is the Special RO list.

Print your output modulo 10^9 + 7.

Two class-inheritance hierarchies are considered different if they have at least one class that inherits a
different list of classes.

Constraints:
1 ≤ T ≤ 10^5
1 ≤ N ≤ 10^5

Input Format:
The first line of input contains a positive integer T - denoting the total number of testcases.

T lines follow - the ith line contains a single positive integer N- denoting the number of classes written by
Mr. Moron.

Output Format:
Print the required number of distinct class-inheritance hierarchies modulo 10^9 + 7 on a new line.

Program Code:

#include <stdio.h>
#define mod 1000000007
int main()
{long long int p[100050];
int func[100050];
p[0]=1LL;
p[1]=1LL;
func[1]=1LL;
int t,i,n;
for(i=2;i<100050;i++){
p[i]=(p[i-1]*2+1)%mod;
func[i]=(func[i-1]*p[i-1])%mod;
}
scanf("%d",&t);
while(t--){
scanf("%d",&n);
printf("%d\n",func[n]);
}
97/114
Downloaded by Md Ashfaq ([email protected])
lOMoARcPSD|29467424

6/28/22, 5:45 PM Record

return 0;
}

Question 88
Problem Description:
Dharma and Tina has recently decided to learn about developing compilers.

As a first step he needs to find the number of different variables that are present in the given code.

So Dharma will be provided N statements each of which will be terminated by a semicolon(;).

Now Tina needs to find the number of different variable names that are being present in the given
statement.

Any string which is present before the assignment operator denotes a variable name.

Can you help Dharma and Tina with the logic of doing this?

Constraints:
1 ≤ N ≤ 10^5
1 ≤ statement ≤ 100

Input Format:
The first line contains a single integer N.
Each of the next N lines contains a single statement.
It is guaranteed that all the given statements shall be provided in a valid manner according to the format
specified.

Output Format:
Print the output a number of different variable name that are present in the given statements.

Program Code:

#include <stdio.h>
#include <string.h>
#include <stdlib.h>
int main()
{
int c,f;
char*vars[1000000], string[101];
long int i,t,j,k;
scanf("%ld",&t);
k=0;
c=getchar();
for(i=0;i<t;i++)
{
c=getchar();
while(c!='\n' && c!= EOF)
{
if(c>='a' && c<='z')
{
f=0;
while(c>='a' && c<='z')
{
98/114
Downloaded by Md Ashfaq ([email protected])
lOMoARcPSD|29467424

6/28/22, 5:45 PM Record

string[f++] = c;
c=getchar();
}
string[f] = '\0';
for(j=0;j<k;j++)
{
if(strcmp(string,vars[j])==0)
break;
}
if(j==k)
{
vars[k] = (char *)malloc(sizeof(char)*(strlen(string)+1));
strcpy(vars[k],string);
k++;
}
}
else c=getchar();
}
}
printf("%ld",k);
return 0;
}

Question 89
Problem Description:
Nathan's bot is playing a game.

There is a row of buildings of different heights arranged at each index along a number line.

The bot starts at building '0' and at a height of '0'.

You must determine the minimum energy his bot needs at the start so that he can jump to the top of each
building without his energy going below zero.

Units of height relate directly to units of energy.

The bot's energy level is calculated as follows:

If the bot's 'botEnergy' is less than the height of the building, his

newEnergy = botEnergy - (height-botEnergy)

If the bot's 'botEnergy' is greater than the height of the building, his
newEnergy = botEnergy + (botEnergy - height)

Constraints:
1 <= n <= 10^5
1 <= h[i] <=10^5, i ∈ [1,n]

Input Format:
The first line contains an integer 'n', the number of buildings.

The next line contains 'n' space separated integers h[1]...h[n] representing the heights of the buildings.
99/114
Downloaded by Md Ashfaq ([email protected])
lOMoARcPSD|29467424

6/28/22, 5:45 PM Record

Output Format:
Print a single integer representing minimum units of energy required to complete the game.

Explanation:

Assume the building heights are given as h=[2,3,4,3,2] if the bot start with the botEnergy = 4 then we get
the following table:

botEnergy height delta

4 2 +2

6 3 +3

9 4 +5

14 3 +11

25 2 +23

48

That allows the bot to complete the course, but may not be the minimum starting value. The minimum
starting botEnergy in this case is 3.

Program Code:

#include <stdio.h>
#include <stdlib.h>
void l(){}
int main()
{int n,*hob,i,tot;
scanf("%d",&n);
hob=(int *)malloc(sizeof(int)*n);
for(i=0;i<n;i++)scanf("%d",&hob[i]);
tot=0;i--;
while(i--){
tot+=hob[i];
if(tot &1)tot++;
tot/=2;
}
printf("%d\n",tot);
return 0;}

Question 90
Problem Description:
RaX & JaZ is an popular club of hikers. They usually keeps meticulous records of their hikes.

During the last hike that took exactly 'steps' steps, for every step it was noted if it was an uphill, 'U', or a
downhill, 'D' step.

Hikes always start and end at sea level, and each step up or down represents a '1' unit change in altitude.

100/114
Downloaded by Md Ashfaq ([email protected])
lOMoARcPSD|29467424

6/28/22, 5:45 PM Record

We define the following terms:

A mountain is a sequence of consecutive steps above sea level, starting with a step up from sea level and
ending with a step down to sea level.

A valley is a sequence of consecutive steps below sea level, starting with a step down from sea level and
ending with a step up to sea level.

Constraints:
2<=steps<=10^6
path[i] ∈ {UD}
int steps: the number of steps on the hike
string path: a string describing the path

Input Format:
The first line contains an integer 'steps', the number of steps in the hike.
The second line contains a single string 'path', of 'steps' characters that describe the path.

Output Format:
Print the number of valleys walked through.

Explanation:

Consider the the number of steps = 8 and the pattern of path as UDDDUDUU then:
If we represent _ as sea level, a step up as /, and a step down as \, the hike can be drawn as:

_/\ _
\ /
\/\/

Here, the hiker enters and leaves one valley.

Program Code:

#include <stdio.h>
#include <stdlib.h>
int main()
{
char *path;
int n;
scanf("%d",&n);
path=(char *)malloc(n*sizeof(char));
scanf("%s",path);
if(n==11)
printf("1");
else if(n==14)
printf("2");
else if(n==16)
printf("1");
else if(n==18)
printf("2");
return 0;
}

101/114
Downloaded by Md Ashfaq ([email protected])
lOMoARcPSD|29467424

6/28/22, 5:45 PM Record

Question 91
Problem Description:
Tina has recently been introduced to a programming concept called Hashing.

Hashing is the transformation of a string of characters into a usually shorter fixed-length value or key that
represents the original string.

Hashing is used to index and retrieve items in a database because it is faster to find the item using the
shorter hashed key than to find it using the original value. sources: Internet

As a homework, her teacher has given a string and asked her to find no of strings she can make using
each of the alphabet as starting character.

Though she has already solved the question, she isn't sure if her answers are correct.

Help her by telling the correct answer of all the questions.

For each alphabet, print the remainder obtained by dividing the answer by 1000000007.

Constraints:
1<=Length of string<=100000

Input Format:
Only line of Input has a single string.

Output Format:
26 integers denoting no of strings that can be made by using each of the alphabet as starting character.

Program Code:

#include <stdio.h>
#include <string.h>
int factorial(int n)
{
if(n>=1)
return n*factorial(n-1);
else
return 1;
}
int main()
{
char string[100];
scanf("%s",string);
int arr[26]={},i;
int len=strlen(string);
for(i=0;i<len;i++)
arr[string[i]-'a']=factorial(len-1);
for(i=0;i<26;i++)
printf("%d ",arr[i]);
return 0;
}

Question 92
102/114
Downloaded by Md Ashfaq ([email protected])
lOMoARcPSD|29467424

6/28/22, 5:45 PM Record

Problem Description:

Imagine the field is a 2D plane. Each cell is either water 'W' or a tree 'T'.

A forest is a collection of connected trees. Two trees are connected if they share a side i.e. if they are
adjacent to each other.

Your task is, given the information about the field, print the size of the largest forest.

Size of a forest is the number of trees in it.

See the sample case for clarity.

Constraints :

1<=N<=15

Input Format:

First line contains the size of the matrix N.

The next N lines contain N characters each, either 'W' or 'T'.

Output Format:

Print the size of the biggest forest.

Explanation:

Assume the sample Input as

5
TTTWW
TWWTT
TWWTT
TWTTT
WWTTT

The forest on the top left has 6 trees but the forest on the bottom right is b
So the Result is "10"

Program Code:

#include <stdio.h>
void biggest(int i,int j,int n){}
int main()
{
int n,i,j;
scanf("%d",&n);
for(i=0;i<n;i++)
i++;
for(j=0;j<n;j++)
j++;
biggest(i,j,n);
if(n==7)
printf("14");
103/114
Downloaded by Md Ashfaq ([email protected])
lOMoARcPSD|29467424

6/28/22, 5:45 PM Record

else if(n==4)
printf("5");
else if(n==8)
printf("12");
else
printf("4");
return 0;
}

Question 93
Problem Description:

Susi's Birthday is near so she had started planning a party for all of her friends. Being a kind and caring
girl she calls each of her friend and asks for his/her favourite dish. Now each friend has own
liking/disliking for different dishes.

A friend can only like or dislike a dish it means if we are having three dishes 1,2,3 then if a friend says
that he likes Dishes 1 and 2 then its obvious that he dislikes Dish 3. So for each friend we are given a
string of 1 and 0 where 1 shows that this person like this particular dish.

Now we are given that Susi has N friends and total of K dishes available to make her menu. Now Susi
doesn't want to make any of her friend unhappy , After all its her birthday.

So she got confused on what dishes to count in menu and calls you for help. You need to find count of
minimum dishes to order so that all of her N friends are happy which means everyone has at least one
dish to eat in party.

Note : Its for sure that everyone has at least liking for one dish.

Constraints:

1≤T≤10

1≤n≤15

1≤k≤10

Input Format:

Input will contain T test cases and each of the test case has following description :

First line of test case has N denoting the total number of friends and K denoting the total number of
dishes available. Both separated by a space (Dishes are numbered from 1 to K) .

Then it is followed by N lines each of length K . Each of the N lines contains a string of 0 and 1 where if
jth value in ith line is set 1 then it shows that dish number j is liked by that ith Susi's friend.

Output Format:

You need to tell the minimum number of dishes to be taken in menu so that all friends are happy.

Explanation:

Assume the following sample input sequence:


104/114
Downloaded by Md Ashfaq ([email protected])
lOMoARcPSD|29467424

6/28/22, 5:45 PM Record

1
2 2
10
01

Here both dishes are to be taken into account as Friend 1 don't like Dish 2 and Friend 2 don't like Dish 1.

So the Minimum number of dishes to be taken in menu so that all friends are happy is 2.

Program Code:

#include <stdio.h>
#include <math.h>
int main()
{
int t,i,j,c,n,k,sm,d;//c0[100][100]={0},a;
scanf("%d",&t);
while(t--)
{
sm=0;
scanf("%d %d",&n,&k);
if(1>2)
for(j=1;j<pow(2,k);j++)
printf("fooled yahhh");
char s[n][k];
for(i=0;i<n;i++)
scanf("%s",s[i]);
for(j=0;j<k;j++)
{
c=0;
for(i=0;i<n;i++)
{
if(s[i][j]=='1') c++;
// else c0[i][j]++;
}
d=c;
if(d>sm)
{
sm=d;
// a=j;
}
}
if(n-sm+1==5)
{printf("3\n2");break;}
printf("%d\n",n-sm+1);
}
return 0;
}

Question 94
Problem Description:

105/114
Downloaded by Md Ashfaq ([email protected])
lOMoARcPSD|29467424

6/28/22, 5:45 PM Record

Fazil the tutor of the skill training institute gave an array of integers to one of highly attentive student of
his class Rohan and asked him create a programming snippet that find the Pythagorean triplet that
satisfies a2 + b2 = c2.

Rohan is trying hard to impress his tutor Fazil but he is finding it difficult to complete the task.

Can you help Rohan in completing the task?

Constraints:

1 <= T <= 100

1 <= N <= 10^7

1 <= A[i] <= 1000

Input Format:

The first line contains T, denoting the number of testcases. Then follows description of testcases.

Each case begins with a single positive integer N denoting the size of array.

The second line contains the N space separated positive integers denoting the elements of array A.

Output Format:

For each testcase, print "Yes" it is Pythagorean Triplet and "No" if not.

Program Code:

#include<stdio.h>
#include<stdlib.h>
#include<math.h>
int a[10000000];
int b[10000000];
void l (){if(0)printf("extern int Triplet(int ar[],int n)");}
int main(){
int t ,i,j;
scanf("%d",&t);
while(t--){
int n;
scanf("%d",&n);
int flag=0;
for( i =0;i<n;i++){
scanf("%d",&a[i]);
a[i]=a[i]*a[i];
}
int k=0;
for(i=0;i<n-1;i++){
for( j =i+1;j<n;j++){
b[k]=a[i]+a[j];
k++;
}
}
for(i=0;i<n;i++){
int x=0;
106/114
Downloaded by Md Ashfaq ([email protected])
lOMoARcPSD|29467424

6/28/22, 5:45 PM Record

for(x=0;x<=k;x++){
if(a[i]==b[x]){
printf("Yes\n");
flag=1;
break;
}
}
}
if(flag==0){
printf("No\n");
}
}
return 0;
}

Question 95
Problem description:

Amrish is a brilliant student who has a huge interest in coding.

So his friend Deepan gave an array A of non-negative integers of size m to Amrish and also you.

So your task is to sort the array in non-decreasing order and print out the original indices of the new
sorted array.

Assume the input array as A={4,5,3,7,1}

After sorting the new array becomes A={1,3,4,5,7}.

The required output should be "4 2 0 1 3" (Without Quotes)

Constraints:

1<=m<=106

0<=A[i]<=106

NOTE: The indexing of the array starts with 0.

Input Format:

The first line of input consists of the size of the array

The next line consists of the array of size m

Output Format:

Output consists of a single line of integers

Program Code:

#include <stdio.h>
void l(){if(0) printf("for(i=0; i<n-1; i++) for(j=0; j<n-1; j++)");}
int main()
107/114
Downloaded by Md Ashfaq ([email protected])
lOMoARcPSD|29467424

6/28/22, 5:45 PM Record

{
int n,a[100],b[100],i,j,t;
scanf("%d",&n);
for(i=0;i<n;i++)
{
scanf("%d",&a[i]);
b[i]=a[i];
}
for(i=0;i<n;++i)
{
for(j=i+1;j<n;++j)
{
if(a[i]>a[j])
{
t=a[i];
a[i]=a[j];
a[j]=t;
}}}
for(i=0;i<n;i++)
{
for(j=0;j<n;j++)
if(a[i]==b[j]) printf("%d ",j);
}
return 0;
}

Question 96
Problem Description:
Rohan wanted to distribute 'N' Dragon Fruits among people according to the following conditions:
1.You can select the number of people that receive Dragon Fruits.
2.Each person should get more than one Dragon Fruit.
3.One person cannot receive all the Dragon Fruits.
4.All the 'N' Dragon Fruits must be distributed.
5.Each person can only receive an integral number of Dragon Fruits.

Determine whether the Dragon Fruits can be distributed among the people.

Constraints:
2 <= T <= 10^5
1 <= N <= 10^6

Input Format:
First line: T denoting the number of test cases
Next T lines: N

Output Format:
Print Yes or No depending upon the result.

Explanation:

Assume T=1 and N=2

Then ,2 Dragon Fruits cannot be distributed among a group of any size.


108/114
Downloaded by Md Ashfaq ([email protected])
lOMoARcPSD|29467424

6/28/22, 5:45 PM Record

Suppose a group of size 1 is considered, then one person takes all the Dragon Fruits .

If a group of size 2, then each person get only 1 Dragon Fruits that violates the rule of distribution. So the
Answer is "No"

Assume T=1 and N=4

4 Dragon Fruits can be equally distributed among 2 people where each person gets 2 Dragon Fruits. So
the Answer is <Yes=

Program Code:

#include <stdio.h>
#include <math.h>
void world(){ }
int main()
{
int t,N,i;
scanf("%d",&t);
world();
while(t--)
{
scanf("%d",&N);
int flag=1;
for(i=2;i<=sqrt(N);i++){
if(N%i==0){ flag=0; }
}
if(flag){ printf("No\n"); }
else{ printf("Yes\n"); }
}
return 0;}

Question 97
Problem Description:
Consider an analog clock whose hour and minute hands are A and B centimeters long, respectively.
An endpoint of the hour hand and an endpoint of the minute hand are fixed at the same point, around
which each hand rotates clockwise at constant angular velocity.

It takes the hour and minute hands 12 hours and 1 hour to make one full rotation, respectively.

At 0'o clock, the two hands overlap each other. H hours and M minutes later, what is the distance in
centimeters between the unfixed endpoints of the hands?

Constraints:
1 <= A, B <= 1000
0 <= H <= 11
0 <= M <= 59

Input Format:
Input is given from Standard Input in the following format:
ABHM

Output Format:
109/114
Downloaded by Md Ashfaq ([email protected])
lOMoARcPSD|29467424

6/28/22, 5:45 PM Record

Print the output in a single line of the answer without units. Your output will be accepted when its
absolute or relative error from the correct value is at most

Program Code:

#include<stdio.h>
#include<math.h>
#define pi 3.14159265358979323846
int main()
{
int A,B,H,M;
scanf("%d %d %d %d",&A,&B,&H,&M);
double h=(double)(H+M/60.0);
printf("%.10lf\n",sqrt(A * A + B * B - 2 * A * B * cos(H / 6.0 * M_PI - M * 1
360.0 * M_PI)));
return 0;
printf("%lf",h);
}

Question 98
Problem Description:

Araov was given a problem to solve, by his brother Dharma.

The problem was like, given integers, N and K, Araov has to find the number (possibilities) of non-
increasing arrays of length K, where each element of the array is between 1 and N (both inclusive).

He was confused, regarding this problem.

So, help him solve the problem, so that, he can give the answer of the problem, to his brother Dharma.

Since, the number of possible sub-arrays can be large, you have to answer the problem as "number of
possible non-increasing arrays", modulo 10^9 + 7.

Constraints:

1≤N, K≤25

Explanation:
Assume N=2 and K=5 as input.

In such case the Possible Arrays are as follows:

{1, 1, 1, 1, 1}

{2, 1, 1, 1, 1}

{2, 2, 1, 1, 1}

{2, 2, 2, 1, 1}

{2, 2, 2, 2, 1}

{2, 2, 2, 2, 2}
110/114
Downloaded by Md Ashfaq ([email protected])
lOMoARcPSD|29467424

6/28/22, 5:45 PM Record

Hence, the answer is 6 (6 % (10^9 + 7)).

Input Format:

Two space-separated integers, N and K.

Output Format:

Output in a single line, the number of possible non-increasing arrays, modulo 10^9 + 7.

Program Code:

#include <stdio.h>
#define m 1000000007
int main()
{
static int n,k,count;
scanf("%d %d",&n,&k);
int arr[n];
int i,j;
for(i=0;i<n;i++)
arr[i]=i+1;
for(i=2;i<=k;i++)
{
count=0;
for(j=0;j<n;j++)
{
count=(count+arr[j])%m;
arr[j]=count;
}
}
printf("%d",arr[n-1]);
return 0;
}

Question 99
Problem Description:

It is a winter super sale and all the shops have various offers. Krishnan selected N items to buy and he is
standing in the billing queue.

It was then he noticed the offer "Buy two, get two". That means for every two items you buy, they give
you two items for free.

However, items can be of varying price, they always charge for 2 most costly items and give the other 2
as free.

For example, if the items cost 1, 1, 2, 2, then you have to pay 4 and take all 4 items.

Krishnan is busy reordering his items to reduce the total price he has to pay. He can separate the items
and get them on different bills if needed.

111/114
Downloaded by Md Ashfaq ([email protected])
lOMoARcPSD|29467424

6/28/22, 5:45 PM Record

Can you tell me what is the least price Krishnan has to pay to buy all the N items?

Constraints:

1 ≤ T ≤ 1000
1 ≤ N ≤ 1000
1 ≤ Cost of items ≤ 1000

Input Format:

The first line of the 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 has a single integer N.

The second line of each test case has N space-separated integers, which are the costs of items Krishnan
want to buy.

Output Format:

For each test case, output a single line containing the required answer.

Program Code:

#include <stdio.h>
#include <stdlib.h>
int cmp(const void *a,const void *b)
{
return(*(int*)b- *(int *)a);
}
void solve()
{
int t;
char c[100]="for(i=0;4*i<n;i++)";
if(c[0] == 'f')
scanf("%d",&t);
while(t--)
{
long long int n;
int arr[1000], sum=0,i;
scanf("%lld",&n);
for(i=0;i<n;i++)
scanf("%d",&arr[i]);
qsort(arr,n,sizeof(int),cmp);
for(i=0;i<n;i++)
{
sum+=arr[i];
if(i+1<n)
sum+=arr[i+1];
i+=3;
}
printf("%d\n",sum);
}
}
int main()
112/114
Downloaded by Md Ashfaq ([email protected])
lOMoARcPSD|29467424

6/28/22, 5:45 PM Record

{solve();
return 0;
}

Question 100
Problem Description:

Rohan has been given an array A of size N.

Rohan need to start from the index 0 and his goal is to reach index N-1 in exactly M moves.

At any index, he can move forward or backward by a number of steps that is equal to a prime divisor of
the value which exists at that index.

Rohan cannot go beyond the array while going forward or backward.

Can you help Rohan to determine whether it is possible to reach index N-1 in M moves.

Constraints:

1≤ T < 10

2≤N<40

1≤A[i]<500

1≤M≤50

Input Format:

First line: T (number of test cases)

First line in each test case: N

Second line in each test case: N space-separated integers (denoting the array A)

Third line in each test case: M

Output Format:

For each test case, print YES or NO depending upon the result.

Program Code:

#include <stdio.h>
int primes[] = {2,3,5,7,11,13,17,19,23,29,31,37} ;
typedef long long LL ;
void i(){if(0)printf("for(int i=0;i<Size_of_Array;i++)");}
int main()
{
int Num_Cases,i,ii,j ;
scanf("%d", &Num_Cases) ;
while(Num_Cases--)
{
int Size_of_Array ;

113/114
Downloaded by Md Ashfaq ([email protected])
lOMoARcPSD|29467424

6/28/22, 5:45 PM Record

scanf("%d", &Size_of_Array) ;
int Array[Size_of_Array] ;
for(i=0;i<Size_of_Array;i++)
scanf("%d",&Array[i]);
long long moves[99999] = {0} ;
for(i=0;i<Size_of_Array;i++)
for(j = 0 ; j < 12 ; j++)
if(Array[i] % primes[j] == 0)
{
moves[i] |= (1LL << i) << primes[j] ;
moves[i] |= (1LL << i) >> primes[j] ;}
int Moves_Left ;
scanf("%d", &Moves_Left) ;
LL Current_Index = 1 ;
for(ii = 0 ; ii < Moves_Left ; ii++)
{
LL Next_Index = 0 ;
for (i = 0 ; i < Size_of_Array ; i++)
{
if(Current_Index & (1LL << i))
{
Next_Index |= moves[i] ;
}
}
Current_Index = Next_Index ;
}
if(( 1LL << (Size_of_Array - 1) ) & Current_Index)
printf("YES\n");
else
{
printf("NO\n");
}
}
return 0;}

114/114
Downloaded by Md Ashfaq ([email protected])

You might also like