Download as pptx, pdf, or txt
Download as pptx, pdf, or txt
You are on page 1of 20

Competitive

Coding

C Language --- Arrays


Q1- Max element in Array

Find maximum element from the given array of integers.

Input Format
First line of input contains N - the size of the array and second line contains
the elements of the array.
Output Format
Print the maximum element of the given array.
Sample Input -1 Sample Output-1
5 15
-2 -19 8 15 4
Q2- Find Duplicate Element in Array

Find maximum element from the given array of integers.

Input Format
First line of input contains size of the array - N and second line contains the
elements of the array.
Output Format
Print the duplicate element from the given array.
Sample Input -1 Sample Output-1
5 10
5 4 10 9 21 10
Q3- Print Unique Elements of Array

Print unique elements of the array in the same order as they appear in the
input.
Note: Do not use any inbuilt functions/libraries for your main logic.
Input Format
First line of input contains a single integer N - the size of array and second
line contains array elements.
Output Format
Print unique elements of the array.
Sample Input -1 Sample Output-1
5 5 9 21
5 4 10 9 21 4 10
Q4- Gravity Flip
Little Chris is bored during his physics lessons (too easy), so he has built a
toy box to keep himself occupied. The box is special, since it has the ability
to change gravity.
There are n columns of toy cubes in the box arranged in a line. The i-th
column contains ai cubes. At first, the gravity in the box is pulling the cubes
downwards. When Chris switches the gravity, it begins to pull all the cubes to
the right side of the box. The figure shows the initial and final configurations
of the cubes in the box: the cubes that have changed their position are
highlighted with orange.

Given the initial configuration of the toy cubes in the box, find the amounts
of cubes in each of the n columns after the gravity switch!
Q4- Gravity Flip
Input Format
The first line of input contains an integer n (1 ≤ n ≤ 100), the number of the columns in the
box. The next line contains n space-separated integer numbers. The i-th number ai (1 ≤ ai ≤
100) denotes the number of cubes in the i-th column.
Output Format
Output n integer numbers separated by spaces, where the i-th number is the amount of
cubes in the i-th column after the gravity switch.
Sample Input -1 Sample Output-1
4 1223
3212
Sample Input -2 Sample Output-2
3 238
238
Note
The fi rst example case is shown on the fi gure. The top cube of the fi rst column falls to the
top of the last column; the top cube of the second column falls to the top of the third
column; the middle cube of the fi rst column falls to the top of the second column.
In the second example case the gravity switch does not change the heights of the columns.
Q5- Chef Team
Our Chef is hosting a cooking competition in which only two teams can compete at
the same time. Both sides should have an equal number of participants to make the
competition engaging and fair. Chef was given the duty of forming the two teams as
an organizer.There are various conditions for forming teams. He will be assigned a
number 'N' for each competition, and for each divisor 'D' of 'N' (including 1 and 'N'
itself), he will add a member:
1) If 'D' is even, go to the first team.
2) if 'D' is odd, go to the second team.
Chef needs your assistance with the event planning because he is quite busy. Your
job is to advise the chef whether or not he can make two teams with the same
amount of members.
For Example:
For ‘N’ = 10,
The divisors are:
1, 2, 5, 10.
The first team will have two members corresponding to even divisors 2 and 10.
The second team will have two members corresponding to odd divisors 1 and 5.
So, in this case, Chef can make two teams.
Q5- Chef Team
Input Format
First line will contain T, number of testcases. Then the testcases follow.
The first and only line of each test case contains an integer ‘N’ denoting the number given to Chef.
Output Format
For each test case print 1 if Chef can make two teams of equal members with the given ‘N’, else print
0.
Output for each test case will be printed in a separate line.
Sample Input -1 Sample Output-1
2
7 0
14 1
In test case 1:

N = 7, the divisors of 7 are 1,7.


The fi rst team will have 0 members since there are no even divisors.
The second team will have 2 members corresponding to divisors 1 and 7.
Since the teams have a diff erent number of members, the output will be 0.
In test case 2:

N=14, the divisors of 14 are 1,2,7,14.


The fi rst team will have 2 members corresponding to divisors 2 and 14.
The second team will have 2 members corresponding to divisors 1 and 7.
Since the teams have the same number of members, the output will be 1.
Q6- Solve The Case

Given a sorted array arr, remove the duplicates from arr such that each element
appears only once and display the new array.
Input Format
The first line contains T, the number of test cases. Then the T test cases follow.
Each test case contains 2 lines of input:
The first line contains n, the size of array arr.
The second line contains n space separated elements of array arr.
Output Format
For each test case, output in a single line, the new array. The elements of the
array should be separated by space. Your output format should match with that
given in the sample output.
Sample Input -1 Sample Output-1
2
3
112 12
10
0011122334 01234
Q7- Multiply the Array

Churchill was a kindhearted man, and he wanted to give a cakewalk problem


to the coders present, to warm them up.
Given an array of N integers, print the product of the numbers present in the
array.
Use appropriate variable types to avoid overflow errors
Input Format
The first line contains T, the number of test cases.
Each test cases consists of two lines, the first line containing N, the number
of elements, and the second line containing N integers.
Output Format
For each test case, print the required answer in a single line.
Sample Input -1 Sample Output-1
1
2
22 4
Q8- Compartment Weights
Good's Train is a train which carry weights like grains, vegetables, and many
other things in it.

You are given a task to add weights in each compartment of this good's train.
Initially train compartments may or may not be empty, and you will be given
q task, in each task you will be provided with 3 data stated as below.
start number of compartment
end number of compartment
The weights, that needs to be onboarded in each compartment in the given
range.
After you have completed all your q tasks, you will need to give the total
weights that is currently in this Good's Train.
Q8- Compartment Weights
Input Format
The first line of the input contains a single integer T. T denoting the number of test cases.
The description of T test cases is as follows.
The next line of the input contains a single integer N. N denotes the total number of
compartments in Good's Train(numbered from 1 to N).
The next line of the input contains N space-separated integers A1,A2,A3...An where
ith number denotes the initial weight of Good's that ith compartment have.
The next line of the input contains a single integer q. q denotes the total number of tasks
given to you.
Next q lines contains, 3 space-separated integers S,E,W denoting the start, end number of
compartments(both inclusive) and W denotes the weight to be onboarded in each
compartment.
Output Format
After completing all your q tasks, print the total weights that is currently in this Good's Train.
Sample Input -1 Sample Output-1
1
5
12345 90
2
1 3 10
2 4 15
Q9- Birthday of Anabelle
Vishal Wants to buy 2 gifts for his best friend whose name is Annabelle(her age is
20), So they both went for shopping in a store. But Annabelle gave, Vishal a
condition that she will accept this gifts only when the total price of the gifts is the
same as her age times 100.
The store contains, a list of items whose prices are also displayed, Now Vishal is
asking for your help to buy gifts, as he wants to impress Annabelle this time.
Note: Vishal cannot buy the same item more than once.Use appropriate variable
types to avoid overflow errors
Input Format
The first line of the input contains a single integer T. T denoting the number of test
cases. The description of T test cases is as follows.
The next line of the input contains a single integer N. N denotes the total number
of items in store.
The next line of the input contains N space-separated integers A1,A2,A3...An
where ith number denotes the price of ith element.
Output Format
For each test-case print "Accepted"(without quotes) if the gifts are accepted by
Annabelle, else print "Rejected"(without quotes)
Q9- Birthday of Anabelle

Sample Input -1 Sample Output-1


1
5
10 2 1000 50 1000 Accepted
Explanation:
As the given list of items have 2 items whose price sum up to the age times
100 of Annabelle i.e. 1000+1000 = (20 *100)
Q10- Count Pretty Numbers
Vasya likes the number 239. Therefore, he considers a number pretty if its last digit is
2, 3 or 9.
Vasya wants to watch the numbers between L and R (both inclusive), so he asked you to
determine how many pretty numbers are in this range. Can you help him?
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 and only line of each test case contains two space-separated integers L and R.
Output Format
For each test case, print a single line containing one integer — the number of pretty
numbers between L and R.
Sample Input -1 Sample Output-1
2
1 10 3
11 33 8
Example case 1: The pretty numbers between 1 and 10 are 2, 3 and 9.
Example case 2: The pretty numbers
between 11 and 33 are 12, 13, 19, 22, 23, 29, 32 and 33.
Q11- Cost of Groceries
Chef visited a grocery store for fresh supplies. There are N items in the store
where the ith item has a freshness value 𝐴𝑖 and cost 𝐵𝑖.
Chef has decided to purchase all the items having a freshness value greater than
equal to X. Find the total cost of the groceries Chef buys.
Input Format
The first line of input will contain a single integer T, denoting the number of test
cases.
Each test case consists of multiple lines of input.
The first line of each test case contains two space-separated integers N and X —
the number of items and the minimum freshness value an item should have.
The second line contains N space-separated integers, the array A, denoting the
freshness value of each item.
The third line contains 𝑁 space-separated integers, the array B, denoting the cost
of each item.
Output Format
For each test case, print a single line containing one integer — the number of
pretty numbers between L and R.
Q11- Cost of Groceries
Sample Input -1 Sample Output-1
4
2 20 90
15 67 6
10 90 0
31 50
123
123
3 100
10 90 50
30 7 93
4 50
12 78 50 40
40 30 20 10
Example case : Test case 1: Item 2 has freshness value greater than equal to X=20. Thus, Chef buys
item
2. The total cost is 90.
Test case 2: Items 1,2, and 3 have freshness value greater than equal to X=1. Thus, Chef buys all 3
items. The total cost is 1+2+3=6.
Test case 3: No item has freshness value greater than equal to X=100. Thus, Chef buys no items.
Test case 4: Items 2 and 3 have freshness value greater than equal to X=50. Thus, Chef buys items 2
and 3. The total cost is 30+20=50.
Q12- Running Compare

Alice and Bob recently got into running, and decided to compare how much
they ran on different days.
They each ran for N days — on the ith day, Alice ran 𝐴𝑖 meters and Bob
ran 𝐵𝑖 meters.
On each day,Alice is unhappy if Bob ran strictly more than twice her
distance, and happy otherwise.
Similarly, Bob is unhappy if Alice ran strictly more than twice his distance,
and happy otherwise.
For example, on a given day If Alice ran 200 meters and Bob ran 500, Alice
would be unhappy but Bob would be happy.
If Alice ran 500 meters and Bob ran 240, Alice would be happy and Bob
would be unhappy.
If Alice ran 300 meters and Bob ran 500, both Alice and Bob would be happy.
On how many of the N days were both Alice and Bob happy?
Q12- Running Compare

Input Format
The first line of input will contain a single integer T, denoting the number of
test cases.
Each test case consists of three lines of input.
The first line of each test case contains a single integer N — the number of
days.
The second line of each test case contains N space-separated integers 𝐴1, 𝐴2,
…, 𝐴𝑁 — the distances run by Alice on the N days.
The third line of each test case contains N space-separated integers 𝐵1, 𝐵2,
…,B 𝑁 — the distances run by Bob on the N days.
Output Format
For each test case, output on a new line the number of days when both Alice
and Bob were happy.
Q12- Running Compare

Sample Input -1 Sample Output-1


4
3 1
100 200 300 3
300 200 100 0
4 5
1000 1000 1000 1000
400 500 600 1200
4
800 399 1400 532
2053 2300 3400 23
5
800 850 900 950 1000
600 800 1000 1200 1400

You might also like