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

Python Questions

Uploaded by

anish.sarawgi5
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
8 views

Python Questions

Uploaded by

anish.sarawgi5
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 6

Python Questions

1. Break Stick

Akash has a stick of length N. He can break the stick into 2 or more parts such that the
parity of length of each part is the same.

(Example of parity being same : a stick of length 11 can be broken into three sticks of
lengths {3, 3, 5} since each part is odd - that means they have same parity,
but if we break it into two sticks of lengths {5,6} since one is even and the other is odd -
It is considered that their parity is different.)

Problem: Can Akash obtain a stick of length exactly X by doing this by maintaining the
same parity?

Input Format:
1. The first line of input will contain a single integer T(No.of experimentations),
denoting the number of test cases. The description of the test cases follows.
a. Each test case consists of a single line of input, containing two space-
separated integers N(length of the stick), and X(Required size of the
stick).
Constraints: 1≤T≤1000 and 1≤X<N≤10^9

Output Format:
For each test case, output on a new line YES if Akash can obtain a stick of length
exactly X, and NO otherwise.

Sample Input:
T=3
N,X = 6 1
N,X = 3 2
N,X = 4 3

Sample Output:
YES
NO
YES
2. ATM Machine

There is an ATM machine. Initially, it contains a total of K units of money. N people


(numbered 1 through N) want to withdraw money, for each valid i, the i-th person wants
to withdraw A[i] units of money.

The people come in and try to withdraw money one by one, in the increasing order of
their indices. Whenever someone tries to withdraw money, if the machine has at least
the required amount of money, it will give out the required amount. Otherwise, it will
throw an error and not give out anything; in that case, this person will return home
directly without trying to do anything else.

For each person, determine whether they will get the required amount of money or not.

Input format:
1. The first line of the input contains a single integer T(No.of
experimentations) denoting the number of test cases. The description of T
test cases follows.
a. The first line of each test case contain two integers
separated by space contains N(No.of people in the queue)
and K(Total cash in the ATM)
b. The second line contains space-separated integers A[1],
A[2],...A[N].(A[i] = Cash required by the individuals)

Constraints: - 1≤T≤100, - 1≤N≤100, - 1≤A≤1,000,000 for each valid


- 1≤K≤1,000,000

Output format:
For each test case, print a single line containing a string with length N. For each
valid i, the i-th character of this string should be '1' if the i-th person will successfully
withdraw their money or '0' otherwise.

Sample Input:
T= 2
N, K = 5 10
A[i] = 3 5 3 2 1
N, K = 4 6
A[i] = 10 8 6 4

Sample Output:
o/p1 = 11010
o/p2 = 0010
3. Airline Restriction

Anna has 3 bags that she wants to take on a flight. They weigh A, B, and C kgs
respectively. She wants to check-in exactly two of these bags and carry the remaining
one bag with her.

The airline restrictions says that the total sum of the weights of the bags that are
checked-in cannot exceed D kgs and the weight of the bag which is carried cannot
exceed E kgs.

Find if Anna can take all the three bags.

Input Format:
1. The first line of the input contains a single integer T(no.of
experimentations) denoting the number of test cases. The description of T
test cases follows.
a. Each test case contains a single line of input, five space
separated integers A,B,C,D,E.

Constraints: - 1≤T≤36000, - 1≤A,B,C≤10, 15≤D≤20, 5≤E≤10

Output Format:
For each test case, output in a single line answers "YES" if Anna can take all the three
bags with her or "NO" if she cannot.

Sample Input:
T=3
A,B, C, D, E = 1 1 1 15 5
A,B, C, D, E = 8 7 6 15 5
A,B, C, D, E = 8 5 7 15 10

Sample Output:
o/p1 = YES
o/p2 = NO
o/p3 = YES
4. Pseudo Sorted Array

An array A of length N is said to be pseudo-sorted if it can be made non-decreasing after


performing the following operation at most once.

Given an array A, determine if it is pseudo-sorted or not.

Input Format:
1. The first line contains a single integer T(no.of experimentations) - the
number of test cases. Then the test cases follow.
a. The first line of each test case contains an integer N - the size of
the array A.
b. The second line of each test case contains N space-separated
integers A[1], A[2],...A[N] denoting the array A.

Constraints: 1≤T≤1000, 2≤N≤10^5, 1≤A[i]≤10^9

Output Format:
For each test case, output YES if the array A is pseudo-sorted, NO otherwise.

Sample Input:
T=3
A=5
A[i] = 3 5 7 8 9
A=4
A[i] = 1 3 2 3
A=3
A[i] = 3 2 1

Sample Output:
o/p1 : YES
o/p2 : YES
o/p3 : NO
5. Coins and Triangle

Matt belongs to a very rich family which owns many gold mines. Today, he brought N
gold coins and decided to form a triangle using these coins.

Matt has a unusual way of forming a triangle using gold coins, which is described as
follows:
He puts 1 coin in the 1st row, then puts 2 coins in the 2nd row, then puts 3 coins in the
3rd row and so on.

Matt is interested in forming a triangle with maximum possible height using at most N
coins. Can you tell him the maximum possible height of the triangle?

Input:
1. The first line of input contains a single integer T(No.of experimentations)
denoting the number of test cases.
a. The first and the only line of each test case contains an integer N
denoting the number of gold coins Matt has.
Constraints: 1 ≤ T ≤ 100 , 1 ≤ N ≤ 109

Output:
For each test case, output a single line containing an integer corresponding to the
maximum possible height of the triangle that Matt can get.

Sample Input:
T=3
N=3
N=5
N=7

Sample Output:
o/p1 : 2
o/p2: 2
o/p3 : 3
6. Remove Bad Elements

Aniket has an array A of length N.


In one operation, Aniket can remove any one element from the array.
Determine the minimum number of operations required to make all the elements the
same.

Input Format:
1. 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.
a. The first line of each test case contains a single integer —the length of
Array A.
b. Next line contains A space-separated integers A[1], A[2],...A[N] - denoting
the array A.
Constraints: 1≤T≤4000, 1≤A≤10^5, 1≤A[i]≤A

Output Format:
For each test case, output the minimum number of operations required to make all the
elements the same.

Sample Input:
T=3
A=3
A[i] = 3 3 3
A=6
A[i] = 1 3 2 1 2 2
A=4
A[i] = 1 2 1 2

Sample Output:
o/p1 = 0
o/p2 = 3
o/p3 = 2

You might also like