Skip to content

Commit e5dfdc4

Browse files
committed
Codechef: Easy Problems
1 parent ea1bb9d commit e5dfdc4

23 files changed

+1230
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
# In Ciel's restaurant, a waiter is training. Since the waiter isn't good at arithmetic,
2+
# sometimes he gives guests wrong change. Ciel gives him a simple problem. What is A-B (A minus B) ?
3+
#
4+
# Surprisingly, his answer is wrong. To be more precise, his answer has exactly one wrong digit.
5+
# Can you imagine this? Can you make the same mistake in this problem?
6+
#
7+
# Input
8+
# An input contains 2 integers A and B.
9+
#
10+
# Output
11+
# Print a wrong answer of A-B. Your answer must be a positive integer containing the same number of digits
12+
# as the correct answer, and exactly one digit must differ from the correct answer. Leading zeros are not
13+
# allowed. If there are multiple answers satisfying the above conditions, anyone will do.
14+
#
15+
# Constraints
16+
# 1 ≤ B < A ≤ 10000
17+
#
18+
# Sample Input
19+
# 5858 1234
20+
#
21+
# Sample Output
22+
# 1624
23+
#
24+
# Output details
25+
# The correct answer of 5858-1234 is 4624. So, for instance, 2624, 4324, 4623, 4604 and 4629 will be accepted,
26+
# but 0624, 624, 5858, 4624 and 04624 will be rejected.
27+
28+
# MY SOLUTION:
29+
30+
## This solution gives error in codechef as this consumes more memory
31+
32+
# A, B = input().split()
33+
# result = list(str(int(A) - int(B)))
34+
# result[random.randint(0, 3)] = str(random.randint(0, 9))
35+
# print(int(''.join(result)))
36+
37+
A, B = map(int, input().split())
38+
C = A - B
39+
if C % 10 == 9:
40+
C = C - 1
41+
else:
42+
C = C + 1
43+
print(C)
44+
45+
# Output For above solution:
46+
# 5858 1234
47+
# 4625
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
# There are N horses in the stable. The skill of the horse i is represented by an integer S[i].
2+
# The Chef needs to pick 2 horses for the race such that the difference in their skills is minimum.
3+
# This way, he would be able to host a very interesting race. Your task is to help him do this and
4+
# report the minimum difference that is possible between 2 horses in the race.
5+
#
6+
# Input:
7+
# First line of the input file contains a single integer T, the number of test cases.
8+
# Every test case starts with a line containing the integer N.
9+
# The next line contains N space separated integers where the i-th integer is S[i].
10+
#
11+
# Output:
12+
# For each test case, output a single line containing the minimum difference that is possible.
13+
#
14+
# Constraints:
15+
# 1 ≤ T ≤ 10
16+
# 2 ≤ N ≤ 5000
17+
# 1 ≤ S[i] ≤ 1000000000
18+
#
19+
# Example:
20+
#
21+
# Input:
22+
# 1
23+
# 5
24+
# 4 9 1 32 13
25+
#
26+
# Output:
27+
# 3
28+
29+
# MY SOLUTION:
30+
31+
testCase = int(input())
32+
for i in range(0, testCase):
33+
n = int(input())
34+
userInput = input().split()
35+
userInput = sorted([int(i) for i in userInput])
36+
mdif = (userInput[1] - userInput[0]) # Check difference
37+
for i in range(0, n-1):
38+
dif = userInput[i+1] - userInput[i]
39+
if dif < mdif:
40+
mdif = dif
41+
print(mdif)
42+
43+
# OUTPUT for above solution:
44+
# 1
45+
# 5
46+
# 4 9 1 32 13
47+
# 3
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
# Vlad enjoys listening to music. He lives in Sam's Town. A few days ago he had a birthday, so his parents
2+
# gave him a gift: MP3-player! Vlad was the happiest man in the world! Now he can listen his favorite songs
3+
# whenever he wants!
4+
# Vlad built up his own playlist. The playlist consists of N songs, each has a unique positive integer length.
5+
# Vlad likes all the songs from his playlist, but there is a song, which he likes more than the others.
6+
# It's named "Uncle Johny".
7+
# After creation of the playlist, Vlad decided to sort the songs in increasing order of their lengths.
8+
# For example, if the lengths of the songs in playlist was {1, 3, 5, 2, 4} after sorting it becomes
9+
# {1, 2, 3, 4, 5}. Before the sorting, "Uncle Johny" was on A-th position (1-indexing is assumed for the
10+
# playlist) in the playlist.
11+
# Vlad needs your help! He gives you all the information of his playlist.
12+
# Your task is to find the position of "Uncle Johny" in the sorted playlist.
13+
#
14+
# Input
15+
# The first line of the input contains an integer T denoting the number of test cases. The description of T test cases follows.
16+
# The first line of each test case contains one integer N denoting the number of songs in Vlad's playlist.
17+
# The second line contains N space-separated integers A1, A2, ..., AN denoting the lenghts of Vlad's songs.
18+
# The third line contains the only integer A - the position of "Uncle Johny" in the initial playlist.
19+
#
20+
# Output
21+
# For each test case, output a single line containing the position of "Uncle Johny" in the sorted playlist.
22+
#
23+
# Constraints
24+
# 1 ≤ T ≤ 1000
25+
# 1 ≤ A ≤ N ≤ 100
26+
# 1 ≤ ki ≤ 100
27+
#
28+
# Example
29+
# Input:
30+
# 3
31+
# 4
32+
# 1 3 4 2
33+
# 2
34+
# 5
35+
# 1 2 3 9 4
36+
# 5
37+
# 5
38+
# 1 2 3 9 4
39+
# 1
40+
#
41+
# Output:
42+
# 3
43+
# 4
44+
# 1
45+
46+
testCases = int(input())
47+
for i in range(0, testCases):
48+
N = input()
49+
A = list(map(int,input().split()))
50+
K = int(input())
51+
result = A[K - 1]
52+
A = sorted(A)
53+
for i in range(0, len(A)):
54+
if (A[i]== result):
55+
print (i + 1)
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
# In this game the player will use N coins numbered from 1 to N, and all the coins will be facing in
2+
# "Same direction" (Either Head or Tail),which will be decided by the player before starting of the game.
3+
#
4+
# The player needs to play N rounds.In the k-th round the player will flip the face of the all coins whose
5+
# number is less than or equal to k. That is, the face of coin i will be reversed, from Head to Tail, or,
6+
# from Tail to Head, for i ≤ k.
7+
#
8+
# Elephant needs to guess the total number of coins showing a particular face after playing N rounds.
9+
# Elephant really becomes quite fond of this game COIN FLIP, so Elephant plays G times. Please help the
10+
# Elephant to find out the answer.
11+
#
12+
# Input
13+
# The first line of input contains an integer T, denoting the number of test cases.
14+
# Then T test cases follow.
15+
#
16+
# The first line of each test contains an integer G, denoting the number of games played by Elephant.
17+
# Each of the following G lines denotes a single game, and contains 3 space separeted integers I, N, Q,
18+
# where I denotes the initial state of the coins, N denotes the number of coins and rounds, and Q, which
19+
# is either 1, or 2 as explained below.
20+
#
21+
# Here I=1 means all coins are showing Head in the start of the game, and I=2 means all coins are showing
22+
# Tail in the start of the game. Q=1 means Elephant needs to guess the total number of coins showing Head
23+
# in the end of the game, and Q=2 means Elephant needs to guess the total number of coins showing Tail in
24+
# the end of the game.
25+
#
26+
# Output
27+
# For each game, output one integer denoting the total number of coins showing the particular face in the
28+
# end of the game.
29+
#
30+
# Constraints
31+
# 1 ≤ T ≤ 10
32+
# 1 ≤ G ≤ 20000
33+
# 1 ≤ N ≤ 109
34+
# 1 ≤ I ≤ 2
35+
# 1 ≤ Q ≤ 2
36+
# Example
37+
#
38+
# Input:
39+
# 1
40+
# 2
41+
# 1 5 1
42+
# 1 5 2
43+
#
44+
# Output:
45+
# 2
46+
# 3
47+
48+
# MY SOLUTION:
49+
testCases = int(input())
50+
while testCases:
51+
testCases -= 1
52+
G = int(input())
53+
while G:
54+
G -= 1
55+
I, N, Q = map(int, input().split())
56+
if N % 2 == 0:
57+
print(N // 2)
58+
else:
59+
if I == Q:
60+
print(N // 2)
61+
else:
62+
print((N // 2) + 1)
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
# Input
2+
# The first line of the input contains a single integer T denoting the number of test cases to follow.
3+
# Description of each test case contains 2 lines. The first of these lines contain a single integer N,
4+
# the number of cars. The second line contains N space separated integers, denoting the maximum speed of
5+
# the cars in the order they entered the long straight segment.
6+
#
7+
# Output
8+
# For each test case, output a single line containing the number of cars which were moving at their maximum
9+
# speed on the segment.
10+
#
11+
# Example
12+
# Input:
13+
# 3
14+
# 1
15+
# 10
16+
# 3
17+
# 8 3 6
18+
# 5
19+
# 4 5 1 2 3
20+
#
21+
# Output:
22+
# 1
23+
# 2
24+
# 2
25+
#
26+
# Constraints
27+
# 1 ≤ T ≤ 100
28+
# 1 ≤ N ≤ 10,000
29+
# All speeds are distinct positive integers that fit in a 32 bit signed integer.
30+
31+
testCases = int(input())
32+
while testCases:
33+
testCases -= 1
34+
N = int(input())
35+
speeds = [int(i) for i in input().split()]
36+
maximum = speeds[0]
37+
count = 0
38+
for i in range(1, len(speeds)):
39+
if speeds[i] > maximum:
40+
count += 1
41+
else:
42+
maximum = speeds[i]
43+
44+
print(N - count)
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
# Lapindrome is defined as a string which when split in the middle, gives two halves having the same
2+
# characters and same frequency of each character. If there are odd number of characters in the string,
3+
# we ignore the middle character and check for lapindrome. For example gaga is a lapindrome, since the
4+
# two halves ga and ga have the same characters with same frequency. Also, abccab, rotor and xyzxy are a
5+
# few examples of lapindromes. Note that abbaab is NOT a lapindrome. The two halves contain the same
6+
# characters but their frequencies do not match.
7+
# Your task is simple. Given a string, you need to tell if it is a lapindrome.
8+
#
9+
# Input:
10+
# First line of input contains a single integer T, the number of test cases.
11+
# Each test is a single line containing a string S composed of only lowercase English alphabet.
12+
#
13+
# Output:
14+
# For each test case, output on a separate line: "YES" if the string is a lapindrome and "NO" if it is not.
15+
#
16+
# Constraints:
17+
# 1 ≤ T ≤ 100
18+
# 2 ≤ |S| ≤ 1000, where |S| denotes the length of S
19+
#
20+
# Example:
21+
# Input:
22+
# 6
23+
# gaga
24+
# abcde
25+
# rotor
26+
# xyzxy
27+
# abbaab
28+
# ababc
29+
#
30+
# Output:
31+
# YES
32+
# NO
33+
# YES
34+
# YES
35+
# NO
36+
# NO
37+
38+
testCases = int(input())
39+
while testCases:
40+
testCases -= 1
41+
string = input() # input is already in string format so no need to type-cast
42+
halve = len(string) // 2
43+
if len(string) % 2 == 0:
44+
if sorted(string[:halve]) == sorted(string[halve:]):
45+
print('YES')
46+
else:
47+
print('NO')
48+
else:
49+
if sorted(string[:halve]) == sorted(string[halve + 1:]):
50+
print('YES')
51+
else:
52+
print('NO')
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
# Customers in Strangeland are strange. A customer points at some kind of sweets and gives several banknotes
2+
# to the seller. This means that he wants to buy a positive number of sweets of that kind. He doesn't tell
3+
# the exact number of sweets he wants to buy. The only thing Ann knows is: an 'adequate' customer won't give
4+
# any extra banknotes. It means that if you throw away any banknote, the resulting amount of money won't be
5+
# enough to buy the wanted number of sweets.
6+
#
7+
# Ann has to determine the number of sweets the customer wants. Help Ann write a program which determines
8+
# this number or tells that it's impossible.
9+
#
10+
# Input
11+
# The first line of the input contains a single integer T, the number of test cases (no more than 20).
12+
# T test cases follow. Each test case consists of two lines. The first of these lines contains two integers
13+
# N and X (1 ≤ N, X ≤ 100) separated by a single space. N is the number of banknotes given by the customer.
14+
# X is the cost of a single sweet of the chosen kind. The second of these lines contains N space-separated
15+
# integers Ai (1 ≤ Ai ≤ 100), the values of the banknotes.
16+
#
17+
# Output
18+
# For each test case output exactly one line containing a single integer:
19+
#
20+
# -1 if the customer is inadequate and has given extra banknotes, or
21+
# K, the number of sweets the customer wants to buy. If there are several possible answers, output the
22+
# largest of them.
23+
#
24+
# Example
25+
# Input:
26+
# 3
27+
# 4 7
28+
# 10 4 8 5
29+
# 1 10
30+
# 12
31+
# 2 10
32+
# 20 50
33+
#
34+
# Output:
35+
# -1
36+
# 1
37+
# 7
38+
#
39+
# Explanation
40+
# In the first test case, the total amount of money received from the customer is 27. He can't buy more than
41+
# 3 sweets with this amount. If you throw away the banknote of value 5 (or of value 4), it will still be
42+
# possible to buy 3 sweets. Hence the customer is inadequate.
43+
#
44+
# In the second test case, it's clear that he wants to buy just one sweet (note that this number should be
45+
# positive).
46+
#
47+
# In the third test case, the total amount of money received is 70, which is enough for 7 sweets. The
48+
# customer might want to buy only 6 sweets, but we are interested in the largest possible answer.
49+
testCases = int(input())
50+
while testCases:
51+
testCases -= 1
52+
N, X = map(int, input().split())
53+
bankNotes = [int(i) for i in input().split()]
54+
bankNotes = sum(bankNotes)
55+
if (N * X) <= bankNotes:
56+
result = bankNotes // X
57+
print(int(result))
58+
else:
59+
print(int('-1'))

0 commit comments

Comments
 (0)