SEPTOCODE 21
This September, get ready to beat your brains out for the GDSC Club.
Starting from 20th September, all the contributors will be provided with simple programming questions,one per day, which can be written using any programming language of preference. The submissions will be accepted through Google after thorough checking.
2. CET-B
DAY-1
Problem Statement: In a certain game, a player's score is
decided by the formula a+b*c/a-b , where a, b and c denote
his score on levels 1 2 and 3. Given input as a, b and c, you have
to find the resulting score of the player.
Approach: Taking the all numbers input first you can just
calculate just by giving the expression in the code.
The system will automatically solve the expression and give the
output.
3. CET-B
DAY-2
Problem Statement: You are going to appear a fast-aptitude
exam, but only people with a calculating mind can clear it.
Given 2 years, write a program to calculate the total number of
days in both years.
Approach: Basically, you need to calculate whether the year is
leap year or not. If leap year the year will have 366 days else
365 days.
A number is a leap year if it is divisible by 4 and not divisible by
100 or its divisible by 400.
4. CET-B
DAY-3
Problem Statement: Given that your semester results are out
and you have received the credits of each subject. Frame a pro-
gram to input your credit of each subject in a line in form of a
string separated by commas.
Approach: After taking the input string, write a condition in
which the iterator goes to the new line if there is a comma (,)
else it will print the characters.
5. CET-B
DAY-4
Problem Statement: Zumba sports club has the following grading
policy: -
- Every player receives a point in the inclusive range from 0 to 100.
- If a player scores a point less than 35, he is disqualified
Joy is a coach at the sports club who likes to round each student's
point according to these rules: -
- If the difference between the point and the next multiple of 5 is less
than 3, round point up to the next multiple of 5.
- If the value of point is less than equal to 33, no rounding occurs as the
result will still be a disqualifying score.
Approach: First check that the number is less than or equal to 33 or not.
If condition satisfies no rounding occurs.
For finding the nearest multiple of 5 we can take a loop starting from i=1
and it will be added with the input number. Then you can check after
adding that the resulting number is divisible by 5 or not and if divisible
it’s the nearest multiple of 5. Then we can check by calculating the
difference between multiple of 5 and input number and if less than 3
then round off else not.
You can also find the modulus of the number with 5 and use the remain-
der value to find the net multiple of 5
6. CET-B
DAY-5
Problem Statement: Given an array of n integers. Find the
unique elements in the array and print them.
Unique elements are the elements which occur only once in
the array.
Approach: After taking the input string, take a nested loop
along with a counter variable. First array will check each
element and the other element will iterate through the total
array and check its second occurrence. If it occurs once more
then the counter variable increases.
If the counter variable equals to 1 then the element will be
printed.
8. CET-B
DAY-7
Problem Statement: Himansh loves billiards very much. One
day while playing a match , he found out a new trick in the
game. He interpreted each red balls as 0 and each white balls
as 1 .He started taking a note of the positions of white and red
balls in the table. If there are at least 6 balls of same color
placed one after another, then the situation is considered dan-
gerous else its out of danger.
Approach: After taking the input string, with the help of pre-
defined function taking a string containing six 0's and six 1's
compare and search if that string is found inside the main
input string .
If it is found then print "DANGEROUS" else "NOT DANGEROUS".
9. CET-B
DAY-8
Problem Statement: While checking the English papers of the
board exams ,the professor often gets frustated by the writing
format of words of the student in the examination paper. The words
were a net mix of upper case and the lower case letters in every
words. An idea struck into the mind of the professor to develop an
extension that would change the letters' register in every word so
that it either only consisted of lower case letters or, vice cersa, only
of uppercase ones. At that as little as possible letters should be
changed in the word. For example, the word HoUse must be
replaced with house, and the word ViP — with VIP.
Approach: After taking the input check how many upper case and
lower case letters are present by using a count variable.
If more number of lower case are present then convert the whole
string to lowercase else convert it to uppercase.
10. CET-B
DAY-9
Problem Statement: There are n students standing in a row for a
drill. They are given Red(R), Blue(B) and Green(G) colored tshirts.
The instructor wanted to remove some students from the row
such that two neighboring students don't have the same color
tshirts. Students in a row are considered neighboring if there are
no other students between them.
Approach: Create a loop which take iteration for every charac-
ter in an input string and check whether the character is equal to
it's adjescent character.
If it is so then increase the value of variable by one which was
initially declared as zero. Finally print the value of variable after
the loop over. Repeat the process for number of test cases.
11. CET-B
DAY-10
Problem Statement: A primary school teacher has taught the
students english alphabets. He now wants to test the
students. He has given his n students a task to write sentenc-
es with every alphabet occuring atleast once. He has taught
the students only lowercase alphabets. Given a string without
spaces findif every alphabet of english language has occured
atleast once. If the student has passed the test the teacher
gave them a pass mark.The sentences need not be meaning-
ful.
Approach: Create an integer array of size 26 where each place
represent the ascii character of each lower case alphabet.
Then just increment the values
in the appropriate places when that character appear in the
sentence. Loop through the characters of the string and sub-
tract the ascii from 97
to get values ranging from 1-26 then increment the value in
the array. Then check if any position is having value 0 then
print fail else pass.
12. CET-B
DAY-11
Problem Statement: There are n marbles in a row. A child has
to divide the marbles in halves untill he gets one marble. Find
the minimum number of steps required to get the single
marble. The halves can be unequal for odd numbers. After a
division from odd even pair the child has to choose the even
pair for futher division if the odd number is not 1.
Approach: First you need to enter the number of marbles in a
row. Then keep on dividing the number of marbles till there is
only one marble left. Count the minimum number of steps
required to get a single marble. If the number gets divided to
and even and odd number, continue the division with the even
one.
13. CET-B
DAY-12
Problem Statement: Given a square matrix, calculate the absolute
difference between the sums of its opposite rows and opposite
columns.
For example, the square matrix arr is shown below:
1 2 3
7 5 6
9 8 9
The sum of the rows = 1+2+3+9+8+9= 32. The sum of the columns =
1+7+9+3+6+9= 35. Their absolute difference is |32-35|=3.
Approach: ake the matrix as input.Then add all the elements of first row
with that of the last row and store in a variable. Add all the elements of
first
column with that of the last column and store in another variable. Print
the absolute difference between them.
14. CET-B
DAY-14
Problem Statement: There are n students in a class. They are given
numbers according to their height, like if two students have same
heights they are given same number. They are arranged in a pyra-
mid manner.
A pyramid manner is basically a sequence of numbers, with initially
an increasing order, then the peak, then a decreasing order, look-
ing like a pyramid.
Now, the class teacher needs to find the place where the tallest
student is standing.
Approach: Initially create two variable, the first variable store the
starting index and second variable store the ending index of input
array.
Then create a continuous loop which ends when both the variable
have same value. In each iteration find the middle index and com-
pare the
value at that position with upper value, if it is greater then second
variable should store the middle index else first variable should
store the upper middle index. Repeat this until the loop over. After
the loop over print the upper index of first variable.
16. CET-B
DAY-15
Problem Statement: There are n students appearing an online
exam. Each of them are given serial numbers starting from 1 to
n. No two students can have the same serial number. Each of
the students are asked to write their serial numbers on the top.
One of the student copied all the answers from another
student. He forgot to change his serial number and submitted
his answer sheet with the other students serial number. Find
the student who cheated. The teacher has recieved the
answer sheets in a random manner. If no students has cheated
print 0.
Approach: Create a continuous loop which occurs for t times,
t is the number of test cases. In each iteration take inupt array
and store in a variable.
Then create two loop to check which number is missing in the
range from 1 to n. finally print the missing number.
17. CET-B
DAY-16
Problem Statement: Mirsha is a computer science student in her
final year. During a certain event, she was faced with an interesting
problem.
Given a list of numbers, all except one numbers follows a certain
odd-even pattern. Basically this means, from the list, 1 number will
be odd, and rest even, or vice versa. Your task is to find the index of
the number which doesn't follow the pattern.
The indexing here starts from 1.
Approach: Initialize two variable k1 and k2 with integer value zero.
Then for each number in the array check if it is even or odd, for
even number
increase the value of k1 by and for odd number do same with k2.
Finally if k1 is greater then k2, then find the index of odd number
and print it
else find the index of even number and print it. Repeat the process
for all test cases.
18. CET-B
DAY-17
Problem Statement: A company decided to start a lucky draw for its
customers. The company gave its customers a lucky draw coupon with a
serial number printed on it.
The serial number ranges from 1 to n. The company devised a rule to
decide the lucky numbers:-
-Starting with the serial number, replace the serial number by the sum of
the squares of its digits.
-Repeat the process until the number equals 1 (where it will stay), or it
loops endlessly in a cycle which does not include 1.
-Those numbers for which this process ends in 1 are lucky.
The customers are asked to submit their tickets at the counter and their
serial numbers are checked their and lucky or unlucky is printed over their
tickets based on their serial number.
Approach: After taking the input first check if the number is greater than
9 or not. If smaller than 9 then its unlucky. Then calculate the sum of each
digits in the number and if it results to something other than 1 then the
number will be the sum and like this it checks in a loop.
19. CET-B
DAY-18
Problem Statement: A number of balls are kept in a basket each
named in the range [1,n]. some students picked few balls randomly
and changed their numbers to existing numbers in the box and
then added back. Now box has unique numbers + duplicate num-
bers. Find the original numbers on the balls whose numbers were
changed.
Approach: First input the number of test cases.
Input size of array(arr[]) and then input the array elements.
Then another array(reg[]) has been created to check which all
elements are present in the array from 1-n. The value of this array
will contain eiher 1 or 0 depicting which elements are present(val-
ue=1) and which are absent(value=0).
Once it's done, Then we once again traverse this reg[] array and
print the index value where the value is 0(depicting absent).
20. CET-B
DAY-19
Problem Statement: Given a number of balls placed on top of each other in
an order. It is kept in such a way that the number of balls goes on increasing in
a column and then decreasing which resembles a mountain pattern.Jonita has
been asked by her teacher to find whether the mountain has steep slope or
gentle slope. Help Jonita solving out this problem.
Approach: In this problem four loop is required. Using first loop store the
largest value from array in a variable. In the second loop find the index for
lagest value from the array and name it middle_index.
then divide the array into two part from middle_index. In the 3rd loop check if
any number repeat in first part of array and if so then print "gentle" and break
the loop, if not go to 4th loop.
In 4th loop check if any number repeat in second part of array if so print
"gentle" and break the loop, if not then just out from the loop and print
"steep".
21. CET-B
DAY-20
Problem Statement: Tony has been assigned a number n and
was asked to represent it as a sum of maximum possible
number of prime numbers.One can prove that such represen-
tation exists for any integer greater than 1. Help Tony to solve
this problem.
Approach: First check the number n is even or odd. For even
number print "n/2" then print "2" for n/2 times and for odd
number print interger value
of n/2 then print (n-3)/2 times "2" and one times "3". Repeat
this process for all test cases.
22. CET-B
DAY-21
Problem Statement: A baker wants to raise some yeast in a container for making bread.
Initially, the container is empty. Each morning, the baker can put any number of yeast into the
container. And each night, every yeast cell in the container will split into two yeast cells. You
hope to see exactly x yeast cells in the container at some moment.
What is the minimum number of yeast cells the baker needs to put into the container across
those days?
Approach: Initialize an integer variable k with zero. Check if the input integer is odd number
then increase k by one and the new value of n should be
(n-1)/2 , if number is even then just change n to n/2. All these process should be under an
continuous loop whose boolean value always be True,
if n reach to one then break the loop by incresing k by one. Finally print the value of k. Repeat
the process for all test cases.
Or,
FUNCTION TO SOLVE FOR N
Basically we need to form n(given number) as a sum of powers of 2
for example take 15
15 = (2^3) + (2^2) + (2^1) + (2^0)
and as it is clear there is no better way than expressing the number in binary
15 = 1111
to express 15 we need 4 powers of 2 (therefore 4 1's in the binary form of 15)
hence the answer to n is the number of 1's in the binary form of n
verify this on 5
5 = 0101 thus the answer is 2
Set a counter 'ctr' for counting no. of 1's
In the end:
for (int m = n;m>0; m/=2)
{
ctr += m%2;
}
return ctr;