0% found this document useful (0 votes)
255 views

Problem Solving Set I

The document contains descriptions of 9 programming problems involving tasks like adding numbers, ATM withdrawals, sorting lists, finding remainders when dividing numbers, determining winners based on score differences in rounds of a game, and counting occurrences of a digit in integers. The problems cover a range of difficulty from basic math operations to more complex logic problems. Input and output formats are provided for each problem along with examples. Constraints on values are specified where applicable.
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
255 views

Problem Solving Set I

The document contains descriptions of 9 programming problems involving tasks like adding numbers, ATM withdrawals, sorting lists, finding remainders when dividing numbers, determining winners based on score differences in rounds of a game, and counting occurrences of a digit in integers. The problems cover a range of difficulty from basic math operations to more complex logic problems. Input and output formats are provided for each problem along with examples. Constraints on values are specified where applicable.
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 7

Problem solving Set I

1. Add Two Numbers


Shivam is the youngest programmer in the world, he is just 12 years old. Shivam is learning programming
and today he is writing his first program.

Program is very simple, Given two integers A and B, write a program to add these two numbers.

Input
The first line contains an integer T, total number of test cases. Then follow T lines, each line contains two
Integers A and B.

Output
Add A and B and display it.

Constraints
 1 ≤ T ≤ 1000
 1 ≤ A,B ≤ 10000

Example
Input
3
12
100 200
10 40

Output
3
300
50

2. ATM
Pooja would like to withdraw X $US from an ATM. The cash machine will only accept the transaction
if X is a multiple of 5, and Pooja's account balance has enough cash to perform the withdrawal transaction
(including bank charges). For each successful withdrawal the bank charges 0.50 $US. Calculate Pooja's
account balance after an attempted transaction.

Input
Positive integer 0 < X <= 2000 - the amount of cash which Pooja wishes to withdraw.
Nonnegative number 0<= Y <= 2000 with two digits of precision - Pooja's initial account balance.

Output
Output the account balance after the attempted transaction, given as a number with two digits of precision.
If there is not enough money in the account to complete the transaction, output the current bank balance.

Example - Successful Transaction


Input:
30 120.00

Output:
89.50

Example - Incorrect Withdrawal Amount (not multiple of 5)


Input:
42 120.00

Output:
120.00

Example - Insufficient Funds


Input:
300 120.00

Output:
120.00

3. Enormous Input Test


The purpose of this problem is to verify whether the method you are using to read input data is sufficiently
fast to handle problems branded with the enormous Input/Output warning. You are expected to be able to
process at least 2.5MB of input data per second at runtime.

Input
The input begins with two positive integers n k (n, k<=107). The next n lines of input contain one positive
integer ti, not greater than 109, each.

Output
Write a single integer to output, denoting how many integers ti are divisible by k.

Example
Input:
73
1
51
966369
7
9
999996
11

Output:
4

4. Small factorials
You are asked to calculate factorials of some small positive integers.

Input
An integer t, 1<=t<=100, denoting the number of test cases, followed by t lines, each containing a single
integer n, 1<=n<=100.

Output
For each integer n given at input, display a line with the value of n!

Example
Sample input:
4
1
2
5
3
Sample output:
1
2
120
6
5. Sum of Digits
You're given an integer N. Write a program to calculate the sum of all the digits of N.

Input
The first line contains an integer T, total number of testcases. Then follow T lines, each line contains an
integer N.

Output
Calculate the sum of digits of N.

Constraints
 1 ≤ T ≤ 1000
 1 ≤ N ≤ 1000000

Example
Input
3
12345
31203
2123
Output
15
9
8

6. Turbo Sort Problem Code


Given the list of numbers, you are to sort them in non decreasing order.

Input
t – the number of numbers in list, then t lines follow [t <= 10^6].
Each line contains one integer: N [0 <= N <= 10^6]

Output
Output given numbers in non decreasing order.

Example
Input:
5
5
3
6
7
1
Output:
1
3
5
6
7

7. Number Mirror
Problem Statement
Write a program that accepts a number, n, and outputs the same.

Input
The only line contains a single integer.

Output
Output the answer in a single line.

Constraints
 0 ≤ n ≤ 105

Sample Input
123

Sample Output
123

8. Find Remainder
Write a program to find the remainder when two given numbers are divided.

Input
The first line contains an integer T, total number of test cases. Then follow T lines, each line contains two
Integers A and B.

Output
Find remainder when A is divided by B.

Constraints
 1 ≤ T ≤ 1000
 1 ≤ A,B ≤ 10000

Example
Input
3
12
100 200
10 40

Output
1
100
10

9. The Lead Game


The game of billiards involves two players knocking 3 balls around on a green baize table. Well, there is
more to it, but for our purposes this is sufficient.
The game consists of several rounds and in each round both players obtain a score, based on how well they
played. Once all the rounds have been played, the total score of each player is determined by adding up the
scores in all the rounds and the player with the higher total score is declared the winner.
The Siruseri Sports Club organises an annual billiards game where the top two players of Siruseri play
against each other. The Manager of Siruseri Sports Club decided to add his own twist to the game by
changing the rules for determining the winner. In his version, at the end of each round, the cumulative
score for each player is calculated, and the leader and her current lead are found. Once all the rounds are
over the player who had the maximum lead at the end of any round in the game is declared the winner.
Consider the following score sheet for a game with 5 rounds:
Round Player 1 Player 2

1 140 82

2 89 134

3 90 110

4 112 106

5 88 90
The total scores of both players, the leader and the lead after each round for this game is given below:
Round Player 1 Player 2 Leader Lead

1 140 82 Player 1 58

2 229 216 Player 1 13

3 319 326 Player 2 7

4 431 432 Player 2 1

5 519 522 Player 2 3


Note that the above table contains the cumulative scores.
The winner of this game is Player 1 as he had the maximum lead (58 at the end of round 1) during the
game.
Your task is to help the Manager find the winner and the winning lead. You may assume that the scores
will be such that there will always be a single winner. That is, there are no ties.
Input
The first line of the input will contain a single integer N (N ≤ 10000) indicating the number of rounds in the
game. Lines 2,3,...,N+1 describe the scores of the two players in the N rounds. Line i+1 contains two
integer Si and Ti, the scores of the Player 1 and 2 respectively, in round i. You may assume that 1 ≤ Si ≤
1000 and 1 ≤ Ti ≤ 1000.
Output
Your output must consist of a single line containing two integers W and L, where W is 1 or 2 and indicates
the winner and L is the maximum lead attained by the winner.
Example
Input:
5
140 82
89 134
90 110
112 106
88 90
Output:
1 58

10. First and Last Digit Problem Code


If Give an integer N . Write a program to obtain the sum of the first and last digit of this number.

Input
The first line contains an integer T, total number of test cases. Then follow T lines, each line contains an
integer N.
Output
Display the sum of first and last digit of N.

Constraints
 1 ≤ T ≤ 1000
 1 ≤ N ≤ 1000000

Example
Input
3
1234
124894
242323

Output
5
5
5

11. Lucky Four Problem Code


Kostya likes the number 4 much. Of course! This number has such a lot of properties, like:
 Four is the smallest composite number;
 It is also the smallest Smith number;
 The smallest non-cyclic group has four elements;
 Four is the maximal degree of the equation that can be solved in radicals;
 There is four-color theorem that states that any map can be colored in no more than four colors in such
a way that no two adjacent regions are colored in the same color;
 Lagrange's four-square theorem states that every positive integer can be written as the sum of at most
four square numbers;
 Four is the maximum number of dimensions of a real division algebra;
 In bases 6 and 12, 4 is a 1-automorphic number;
 And there are a lot more cool stuff about this number!
Impressed by the power of this number, Kostya has begun to look for occurrences of four anywhere. He has
a list of T integers, for each of them he wants to calculate the number of occurrences of the digit 4 in the
decimal representation. He is too busy now, so please help him.

Input
The first line of input consists of a single integer T, denoting the number of integers in Kostya's list.
Then, there are T lines, each of them contain a single integer from the list.

Output
Output T lines. Each of these lines should contain the number of occurences of the digit 4 in the respective
integer from Kostya's list.

Constraints
 1 ≤ T ≤ 105
 (Subtask 1): 0 ≤ Numbers from the list ≤ 9 - 33 points.
 (Subtask 2): 0 ≤ Numbers from the list ≤ 109 - 67 points.

Example
Input:
5
447474
228
6664
40
81

Output:
4
0
1
1
0

You might also like