source
stringclasses
1 value
task_type
stringclasses
1 value
in_source_id
stringlengths
1
4
problem
stringlengths
488
6.07k
gold_standard_solution
stringlengths
19
30.1k
verification_info
dict
metadata
dict
problem_id
stringlengths
5
9
apps
verifiable_code
616
Solve the following coding problem using the programming language python: There are $M$ levels for a building numbered from $1$ to $M$ from top to bottom, each level having $N$ parking spots numbered from $1$ to $N$ from left to right. Some spots might have a car while other may be empty, the information of which is given in form of two dimensional character array $C$ ($C_{i, j}$ denote parking spot at $j$-th position on $i$-th level). There is a thief who wants to unlock all the cars. Now, he is skilled such that for the first time, he can directly reach in any parking spot in no time. Basically he can reach the first car to be stolen in 0 time. Now, he can move within the parking lot only in following manner, each taking 1 unit of time: - Move down a level. That is, if current position is $(i, j)$, then he reaches $(i+1, j)$ - If current position is $(i, j)$ and if - $i$ is odd, then he can move from $(i, j)$ to $(i, j+1)$ - $i$ is even, then he can move from $(i, j)$ to $(i, j-1)$ Note that he wants to unlock the cars in minimum time and the car is unlocked as soon as the thief reaches that parking spot.If the parking lot is empty, then the time taken is considered to be 0. Find the minimum time when all the cars would be unlocked. Note that once all cars are unlocked, the thief can escape instantly, so this time does not count. -----Input :----- - The first line of input contains a single integer $T$ (number of test cases). - First liine of each test case contains $M$ and $N$(number of levels and spots per each level) - Next $M$ line contains $N$ space separated characters $C_{i, 1}, C_{i, 2} \ldots C_{i, N}$ where $(1\leq i\leq M)$ and $C_{i, j}$ is either $'P'$ or $'N'$ (without quotes). If the spot contains $'P'$, then a car is parked there. else, it’s not parked. -----Output :----- For each test case print a single integer, the minimum time in which thief can unlock all cars. -----Constraints :----- - $1\leq T \leq100.$ - $1\leq M,N \leq300$ -----Subtasks :----- - Subtask 1 (20 points): $1\leq M \leq2.$ - Subtask 2 (80 points): Original Constraints -----Sample Input :----- 2 4 5 N P N N P N N P N N N P N N N P N N N N 3 3 N P P P P P P P N -----Sample Output :----- 10 6 -----Explanation:----- In the first case, He will select the spot $(1,2)$ and the path he takes will be $(1,2)→(1,3)→(1,4)→(1,5)→(2,5)→(2,4)→(2,3)→(2,2)→(3,2)→(4,2)→(4,1)$ So, he takes 10 steps to unlock all the cars. In the second case, He will select the spot $(1,2)$ and the path he takes will be $(1,2)→(1,3)→(2,3)→(2,2)→(2,1)→(3,1)→(3,2)$. So, he takes 6 steps. The input will be stdin and you should print your solution to stdout Now solve the problem and return the code.
```python from sys import stdin for _ in range(int(stdin.readline())): m, n = list(map(int, stdin.readline().split())) final = [] arr = [] val = 0 extra = 0 for j in range(m): ans = list(map(str, stdin.readline().split())) if ans.count('N') == n: val += 1 else: if val%2 == 0: arr.append(ans) extra += val else: arr.append(['N']*n) arr.append(ans) extra += (val-1) val = 0 for j in range(len(arr)): ans = arr[j] start = -1 for i in range(n): if ans[i] == 'P': start = i break if start != -1: for i in range(n-1, -1, -1): if ans[i] == 'P': end = i break if start != -1: if len(final) == 0: final.append([start, end]) else: if j%2 == 0: if final[-1][0] > start: final[-1][0] = start else: start = final[-1][0] else: if final[-1][1] < end: final[-1][1] = end else: end = final[-1][1] final.append([start, end]) else: if len(final) != 0: start, end = 0, n-1 if j%2 == 0: if final[-1][0] > start: final[-1][0] = start else: start = final[-1][0] else: if final[-1][1] < end: final[-1][1] = end else: end = final[-1][1] final.append([start, end]) if len(final) == 0: print(0) else: count = 0 for ele in final: count += (ele[1]-ele[0]+1) print(count-1+extra) ```
{ "language": "python", "test_cases": [ { "input": "2\n4 5\nN P N N P\nN N P N N\nN P N N N\nP N N N N\n3 3\nN P P\nP P P\nP P N\n", "output": "10\n6\n", "type": "stdin_stdout" } ] }
{ "difficulty": "interview", "problem_url": "https://www.codechef.com/CHPTRS01/problems/CARLOT" }
vfc_1306
apps
verifiable_code
617
Solve the following coding problem using the programming language python: We all know that Share market is place where drastic change occurs within moments. So we have one Stockholder, Isabella, who wants to maximize her profit by selling her shares. She has $N$ shares of a Doofenshmirtz Corporation which is represented by $N$ different lines where each line contains two space separated integers $a_i$ , $b_i$ corresponding to initial and final values of the share prize. Isabella can sell any number of shares. But, she will sell those shares only if the following condition is satisfied - - for any pair $(i,j)$ of shares that she choses to sell, $a_i \leq a_j$ and $b_i < b_j$ must be satisfied. You need to tell Isabella the maximum number of Shares she can sell. -----Input:----- - First line will contain $T$, number of test cases. - Each test case has the following format: - First line of each test case contain single integer $N$, the number of shares of Isabella. - Next $N$ lines of each test case contain two space separated integers $a_i$, $b_i$ (initial and final value of share prize respectively) for each $1 \leq i \leq N$. -----Output:----- For each test case output a single integer: the maximum number of shares that can be sold by Isabella. -----Constraints----- - $1 \leq T \leq 5$ - $1 \leq N \leq 10^5$ - $1 \leq a_i , b_i \leq 10^9 , for each $1$ \leq $i$ \leq $N -----Sample Input:----- $1$ $4$ $1$ $2$ $4$ $3$ $3$ $5$ $2$ $4$ -----Sample Output:----- $3$ -----Explanation:----- Here, Isabella decided to sell share 1, share 3 and share 4 as any two pair of chosen share hold the given condition. The input will be stdin and you should print your solution to stdout Now solve the problem and return the code.
```python def CeilIndex(A, l, r, key): while (r - l > 1): m = l + (r - l)//2 if (A[m] >= key): r = m else: l = m return r def LongestIncreasingSubsequenceLength(A, size): # Add boundary case, # when array size is one tailTable = [0 for i in range(size + 1)] len = 0 # always points empty slot tailTable[0] = A[0] len = 1 for i in range(1, size): if (A[i] < tailTable[0]): # new smallest value tailTable[0] = A[i] elif (A[i] > tailTable[len-1]): # A[i] wants to extend # largest subsequence tailTable[len] = A[i] len+= 1 else: # A[i] wants to be current # end candidate of an existing # subsequence. It will replace # ceil value in tailTable tailTable[CeilIndex(tailTable, -1, len-1, A[i])] = A[i] return len t=int(input()) for _ in range(t): n=int(input()) a=[] for i in range(n): a.append(list(map(int,input().split()))) a.sort() b=[0]*n for i in range(n): b[i]=a[i][1] print(LongestIncreasingSubsequenceLength(b, n)) ```
{ "language": "python", "test_cases": [ { "input": "1\n4\n1 2\n4 3\n3 5\n2 4\n", "output": "3\n", "type": "stdin_stdout" } ] }
{ "difficulty": "interview", "problem_url": "https://www.codechef.com/CACD2020/problems/STOCKMAX" }
vfc_1310
apps
verifiable_code
618
Solve the following coding problem using the programming language python: Dexter, being irritated by DD, gave her a lucrative game to play to keep her busy. There are $N$ bags numbered $1$ to $N$. The $i_{th}$ bag contains $A_i$ coins. The bags are placed in a circular order such that the $N_{th}$ bag is adjacent to the $1^{st}$ bag. DD can select $K$ consecutive adjacent bags and take all the coins in them. Help her find the maximum number of coins she can take by making the ideal choice. Note that the selected bags must be consecutive. Since they are placed in circular order, bag number $1$ and $N$ are considered to be consecutive. -----Input:----- - First-line will contain $T$, the number of test cases. Then the test cases follow. - First-line contains $N$ and $K$. - Second-line contains $N$ numbers $A_1, A_2,...,A_N$, -----Output:----- For each test case, output in a single line, the maximum money that can be collected by DD. -----Constraints----- - $1 \leq T \leq 100$ - $5 \leq N \leq 10^5$ - $1 \leq K < N$ - $1 \leq A_i \leq 10^5$ Sum of $N$ over all test cases is less than $10^6$ -----Sample Input:----- 1 5 3 8 6 9 4 10 -----Sample Output:----- 24 -----EXPLANATION:----- The ideal choice would be to take the last bag with $10$ coins and the first $2$ bags with $8$ and $6$ coins. The input will be stdin and you should print your solution to stdout Now solve the problem and return the code.
```python t=int(input()) for i in range(t): n,k=list(map(int,input().split(" "))) arr=list(map(int,input().strip().split(" ")))[:n] def maxCircularSum(arr, n, k): if (n < k): print("Invalid"); return; sum = 0; start = 0; end = k - 1; for i in range(k): sum += arr[i]; ans = sum; for i in range(k, n + k): sum += arr[i % n] - arr[(i - k) % n]; if (sum > ans): ans = sum; start = (i - k + 1) % n; end = i % n; print(ans); def __starting_point(): maxCircularSum(arr, n, k); __starting_point() ```
{ "language": "python", "test_cases": [ { "input": "1\n5 3\n8 6 9 4 10\n", "output": "24\n", "type": "stdin_stdout" } ] }
{ "difficulty": "interview", "problem_url": "https://www.codechef.com/ENCD2020/problems/ECAPR206" }
vfc_1314
apps
verifiable_code
619
Solve the following coding problem using the programming language python: In a regular table tennis match, the player who serves changes every time after 2 points are scored, regardless of which players scored them. Chef and Cook are playing a different match — they decided that the player who serves would change every time after $K$ points are scored instead (again regardless of which players scored them). When the game starts, it's Chef's turn to serve. You are given the current number of points scored by Chef and Cook ($P_1$ and $P_2$ respectively). Find out whether Chef or Cook has to serve next. -----Input----- - 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 three space-separated integers $P_1$, $P_2$ and $K$. -----Output----- For each test case, print a single line containing the string "CHEF" if it is Chef's turn or "COOK" if it is Cook's turn. -----Constraints----- - $1 \le T \le 10^5$ - $1 \le K \le 10^{9}$ - $0 \le P_1, P_2 \le 10^{9}$ -----Subtasks----- Subtask #1 (100 points): original constraints -----Example Input----- 3 1 3 2 0 3 2 34 55 2 -----Example Output----- CHEF COOK CHEF -----Explanation----- Example case 1: Chef serves for the first two points, Cook serves for the next two, so Chef has to serve again now. The input will be stdin and you should print your solution to stdout Now solve the problem and return the code.
```python n=int(input()) for i in range(n): l=list(map(int,input().split())) k=l[0]+l[1] k=k%(2*l[2]) if k>=0 and k<l[2]: print("CHEF") else: print("COOK") ```
{ "language": "python", "test_cases": [ { "input": "3\n1 3 2\n0 3 2\n34 55 2\n", "output": "CHEF\nCOOK\nCHEF\n", "type": "stdin_stdout" } ] }
{ "difficulty": "interview", "problem_url": "https://www.codechef.com/problems/CHSERVE" }
vfc_1318
apps
verifiable_code
620
Solve the following coding problem using the programming language python: Find the length of the longest contiguous segment in an array, in which if a given element $K$ is inserted, $K$ becomes the second largest element of that subarray. -----Input:----- - The first line will contain $T$, number of test cases. Then the test cases follow. - The first line of each test case contains two integers $N$ and $K$. - The next line contains N space-separated integers Ai denoting the elements of the array. -----Output:----- Print a single line corresponding to each test case — the length of the largest segment. -----Constraints----- - $1 \leq T \leq 10$ - $1 \leq N \leq 10^6$ - $1 \leq Ai, K \leq 10^9$ - Sum of N across all test cases doesn't exceed $10^6$ -----Sample Input:----- 2 5 3 2 4 2 4 2 8 5 9 3 5 7 8 11 17 2 -----Sample Output:----- 5 3 -----EXPLANATION:----- If 3 is inserted at anywhere in the array, it is the second largest element. Hence the maximum length is 5. If 5 is inserted anywhere between 1st and 4th element, it is the second largest element. The length of such subarray is 3. The input will be stdin and you should print your solution to stdout Now solve the problem and return the code.
```python for _ in range(int(input())): n,k=list(map(int,input().split())) a=list(map(int,input().split())) def check(mid): d,left={},0 for i in range(mid): if a[i]>k: if a[i] not in d: d[a[i]]=1 else: d[a[i]]+=1 if len(d)==1: return True for i in range(mid,n): if a[left]>k: d[a[left]]-=1 if d[a[left]]==0: del d[a[left]] if a[i]>k: if a[i] not in d: d[a[i]]=1 else: d[a[i]]+=1 if len(d)==1: return True left+=1 return False lo,hi=0,n while lo<=hi: mid=(lo+hi)//2 #print(mid,lo,hi) if check(mid): res=mid lo=mid+1 else: hi=mid-1 print(res) ```
{ "language": "python", "test_cases": [ { "input": "2\n5 3\n2 4 2 4 2\n8 5\n9 3 5 7 8 11 17 2\n", "output": "5\n3\n", "type": "stdin_stdout" } ] }
{ "difficulty": "interview", "problem_url": "https://www.codechef.com/problems/SLAEL" }
vfc_1322
apps
verifiable_code
621
Solve the following coding problem using the programming language python: Given n words w[1..n], which originate from the same stem (e.g. grace, graceful, disgraceful, gracefully), we are interested in the original stem. To simplify the problem, we define the stem as the longest consecutive substring that occurs in all the n words. If there are ties, we will choose the smallest one in the alphabetical (lexicographic) order. -----Input----- The first line contains an integer T denoting the total number of test cases. In each test cases, the first line contains an integer n denoting the number of words. In the second line, n words w[1..n] consisting of lower case characters are given as a single space-spearated list. -----Output----- For each test case, output the stem in a new line. -----Constraints----- - 1 <= T <= 10 - 1 <= n <= 10 - 1 <= |w[i]| <= 20 -----Example----- Input: 1 4 grace graceful disgraceful gracefully Output: grace -----Explanation----- The stem is grace. The input will be stdin and you should print your solution to stdout Now solve the problem and return the code.
```python t = eval(input()) for _ in range(t): n = eval(input()) a = input().strip().split() cb, cs = 0, "" for i in range(len(a[0])): for j in range(i+1,len(a[0])+1): al = True s = a[0][i:j] for k in a[1:]: if s not in k: al = False break if al: if j-i>=cb: cb = max(cb, j-i) if len(cs) < cb: cs = a[0][i:j] elif len(cs) == cb: cs = min(cs,a[0][i:j]) print(cs) ```
{ "language": "python", "test_cases": [ { "input": "1\n4\ngrace graceful disgraceful gracefully\n", "output": "grace\n", "type": "stdin_stdout" } ] }
{ "difficulty": "interview", "problem_url": "https://www.codechef.com/COOK63/problems/STEM" }
vfc_1326
apps
verifiable_code
623
Solve the following coding problem using the programming language python: 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 The input will be stdin and you should print your solution to stdout Now solve the problem and return the code.
```python t = int(input()) list_to_tri = [] for i in range(t): list_to_tri.append(int(input())) list_to_tri.sort() for i in list_to_tri: print(i) ```
{ "language": "python", "test_cases": [ { "input": "5\n5\n3\n6\n7\n1\n", "output": "1\n3\n5\n6\n7\n", "type": "stdin_stdout" } ] }
{ "difficulty": "interview", "problem_url": "https://www.codechef.com/problems/TSORT" }
vfc_1334
apps
verifiable_code
624
Solve the following coding problem using the programming language python: Once, a genius guy Cristo visited NASA where he met many scientists. A young intern Mark at NASA asked Cristo to observe the strange behaviour of two independent particles (say Alpha and Beta) moving in the free space.Cristo was astonished to see the movement of Alpha and Beta. However, he formulated a procedure to evaluate the distance covered by the particles in given time. The procedure calculates the distance covered by Alpha and Beta for a given time. Mark, however struggles to evaluate the procedure manually and asks you to help him. Cristo's Procedure :- alpha = 0 beta = 0 Procedure CristoSutra( Ti ) : if Ti <= 0 : alpha = alpha + 1 else if Ti == 1 : beta = beta + 1 else : CristoSutra(Ti-1) CristoSutra(Ti-2) CristoSutra(Ti-3) end procedure Note: Print the answer by taking mod from 109+7 . -----Constraints:----- - 1<=T<=105 - 1<=Ti<=105 -----Input Format:----- First line consists an integer t, number of Test cases.For each test case, there is an integer denoting time Ti. -----Output Format:----- For each test case, a single output line contains two space seperated numbers ,distance covered by alpha and beta in the given time. -----Subtasks:----- Subtask 1 (30 points) - 1<=T<=10 - 1<=Ti<=1000 Subtask 2 (70 points) original contraints Sample Input: 2 1 2 Sample Output: 0 1 2 1 The input will be stdin and you should print your solution to stdout Now solve the problem and return the code.
```python for _ in range(eval(input())): n=eval(input()) mod=1000000007 f1,f2=[0]*101000,[0]*101000 f1[1]=0 f1[2]=2 f1[3]=3 f2[1]=1 f2[2]=1 f2[3]=2; for i in range(4,100001): f1[i]=f1[i-1]%mod+f1[i-2]%mod+f1[i-3]%mod f2[i]=f2[i-1]%mod+f2[i-2]%mod+f2[i-3]%mod print(f1[n]%mod,f2[n]%mod) ```
{ "language": "python", "test_cases": [ { "input": "2\n1\n2\n", "output": "0 1\n2 1\n", "type": "stdin_stdout" } ] }
{ "difficulty": "interview", "problem_url": "https://www.codechef.com/ICOD2016/problems/ICODE16C" }
vfc_1338
apps
verifiable_code
625
Solve the following coding problem using the programming language python: Shaun is very much interested in Subarrays. Shaun wants to count the number of subarrays in his chosen array with sum being a multiple of $10^9$. Since, Shaun is interested in huge numbers.He chose his array such that it contains only $10^8$ and $9*10^8$ as its elements. Help shaun to count the number of required subarrays. -----Input:----- - First line will contain $T$, number of testcases. Then the testcases follow. - First line of each testcase contains one integer $N$,size of array $A$. - Second line of each testcase contains $N$ space separated array elements -----Output:----- For each testcase, output in a single line number of subarrays with sum being multiple of $10^9$. -----Constraints----- - $1 \leq T \leq 10$ - $1 \leq N \leq 10^5$ - $A[i]$=$10^8$ , $9*10^8$ -----Sample Input:----- 2 3 100000000 900000000 100000000 1 900000000 -----Sample Output:----- 2 0 The input will be stdin and you should print your solution to stdout Now solve the problem and return the code.
```python def subCount(arr, n, k): mod = [] for i in range(k + 1): mod.append(0) cumSum = 0 for i in range(n): cumSum = cumSum + arr[i] # as the sum can be negative, # taking modulo twice mod[((cumSum % k) + k) % k] = mod[((cumSum % k) + k) % k] + 1 result = 0 # Initialize result for i in range(k): if (mod[i] > 1): result = result + (mod[i] * (mod[i] - 1)) // 2 result = result + mod[0] return result t=int(input()) while t: t=t-1 n=int(input()) a=list(map(int,input().split())) for i in range(n): if a[i]==100000000: a[i]=1 elif a[i]==900000000: a[i]=9 s=10 print(subCount(a,n,s)) ```
{ "language": "python", "test_cases": [ { "input": "2\n3\n100000000 900000000 100000000\n1\n900000000\n", "output": "2\n0\n", "type": "stdin_stdout" } ] }
{ "difficulty": "interview", "problem_url": "https://www.codechef.com/COCA2020/problems/COCA2001" }
vfc_1342
apps
verifiable_code
626
Solve the following coding problem using the programming language python: The Chef has prepared the appetizers in the shapes of letters to spell a special message for the guests. There are n appetizers numbered from 0 to n-1 such that if the appetizers are arrayed in this order, they will display the message. The Chef plans to display them in this order on a table that can be viewed by all guests as they enter. The appetizers will only be served once all guests are seated. The appetizers are not necessarily finished in the same order as they are numbered. So, when an appetizer is finished the Chef will write the number on a piece of paper and place it beside the appetizer on a counter between the kitchen and the restaurant. A server will retrieve this appetizer and place it in the proper location according to the number written beside it. The Chef has a penchant for binary numbers. The number of appetizers created is a power of 2, say n = 2k. Furthermore, he has written the number of the appetizer in binary with exactly k bits. That is, binary numbers with fewer than k bits are padded on the left with zeros so they are written with exactly k bits. Unfortunately, this has unforseen complications. A binary number still "looks" binary when it is written upside down. For example, the binary number "0101" looks like "1010" when read upside down and the binary number "110" looks like "011" (the Chef uses simple vertical lines to denote a 1 bit). The Chef didn't realize that the servers would read the numbers upside down so he doesn't rotate the paper when he places it on the counter. Thus, when the server picks up an appetizer they place it the location indexed by the binary number when it is read upside down. You are given the message the chef intended to display and you are to display the message that will be displayed after the servers move all appetizers to their locations based on the binary numbers they read. -----Input----- The first line consists of a single integer T ≤ 25 indicating the number of test cases to follow. Each test case consists of a single line beginning with an integer 1 ≤ k ≤ 16 followed by a string of precisely 2k characters. The integer and the string are separated by a single space. The string has no spaces and is composed only of lower case letters from `a` to `z`. -----Output----- For each test case you are to output the scrambled message on a single line. -----Example----- Input: 2 2 chef 4 enjoyourapplepie Output: cehf eayejpuinpopolre The input will be stdin and you should print your solution to stdout Now solve the problem and return the code.
```python t=int(input()) def reversebinary(bits,n): bStr='' for i in range(bits): if n>0: bStr=bStr+str(n%2) else: bStr=bStr+'0' n=n>>1 return int(bStr,2) for i in range(t): k,msg=input().split() k=int(k) newmsg=[] for j in msg: newmsg.append(j) for j in range(len(msg)): newmsg[reversebinary(k,j)]=msg[j] print(''.join(newmsg)) ```
{ "language": "python", "test_cases": [ { "input": "2\n2 chef\n4 enjoyourapplepie\n\n\n", "output": "cehf\neayejpuinpopolre\n", "type": "stdin_stdout" } ] }
{ "difficulty": "interview", "problem_url": "https://www.codechef.com/COOK02/problems/ARRANGE" }
vfc_1346
apps
verifiable_code
627
Solve the following coding problem using the programming language python: Bharat was given a problem to solve, by his brother, Lord Ram. The problem was like, given integers, $N$ and $K$, Bharat has to find the number (possibilities) of non-increasing arrays of length $K$, where each element of the array is between $1$ and $N$ (both inclusive). He was confused, regarding this problem. So, help him solve the problem, so that, he can give the answer of the problem, to his brother, Lord Rama. Since, the number of possible sub-arrays can be large, Bharat has to answer the problem as "number of possible non-increasing arrays", modulo $10^9$ $+$ $7$. -----Input:----- - Two space-seperated integers, $N$ and $K$. -----Output:----- - Output in a single line, the number of possible non-increasing arrays, modulo $10^9$ $+$ $7$. -----Constraints:----- - $1 \leq N, K \leq 2000$ -----Sample Input:----- 2 5 -----Sample Output:----- 6 -----Explanation:----- - Possible Arrays, for the "Sample Case" are as follows: - {1, 1, 1, 1, 1} - {2, 1, 1, 1, 1} - {2, 2, 1, 1, 1} - {2, 2, 2, 1, 1} - {2, 2, 2, 2, 1} - {2, 2, 2, 2, 2} - Hence, the answer to the "Sample Case" is $6$ ($6$ % ($10^9$ $+$ $7$)). The input will be stdin and you should print your solution to stdout Now solve the problem and return the code.
```python import math p=7+10**9 n,k=list(map(int,input().split())) c=math.factorial(n+k-1)//((math.factorial(k))*(math.factorial(n-1))) print(c%p) ```
{ "language": "python", "test_cases": [ { "input": "2 5\n", "output": "6\n", "type": "stdin_stdout" } ] }
{ "difficulty": "interview", "problem_url": "https://www.codechef.com/PCR12020/problems/BHARAT" }
vfc_1350
apps
verifiable_code
628
Solve the following coding problem using the programming language python: Chef and his best friend Aleksa are into mathematical games these days. Today, they have some ( ≥ 0 ) black cells represented as B, and a white cell represented as W, lying randomly in a straight line. They have decided to play with these cells. In a move, a player chooses some ( > 0 ) black cells lying on any one side of the white cell and remove them. It should be noted that a player is not allowed to choose black cells from both side of the given white cell. Both the players alternate their moves, and play optimally. The player who is unable to move in his respective turn will lose the game. Aleksa, being a girl, has a habit of playing first. But Chef is fairly smart himself, and will not play the game if he is going to lose it. Therefore, he wants to know the winner beforehand. Can you tell him who is going to win the game for the given configuration of cells? -----Input----- First line of input contains a single integer T denoting the number of test cases. First and the only line of each test case contains a string S consisting of the characters 'B' and 'W', denoting black and white cells, respectively. -----Output----- For each test case, output "Chef" if chef wins the game for the given configuration. Else print "Aleksa". (quotes for clarity only). -----Constraints----- - 1 ≤ T ≤ 10 - 1 ≤ |S| ≤ 10000 - S contains exactly 1 white cell. -----Scoring----- - Subtask 1: 1 ≤ T ≤ 10, 1 ≤ |S| ≤ 10 : ( 30 pts ) - Subtask 2: 1 ≤ T ≤ 10, 1 ≤ |S| ≤ 100 : ( 30 pts ) - Subtask 3: 1 ≤ T ≤ 10, 1 ≤ |S| ≤ 10000 : ( 40 pts ) -----Example----- Input 3 W BW BWBB Output Chef Aleksa Aleksa ----- Explanation----- - Test 1 : Aleksa cannot make a move in her first turn as there is no black cell in the given configuration. - Test 2 : Aleksa can remove 1 black cell lying on the left of white cell in her turn. But then, Chef cannot make a move in his turn as there will be no black cells left. - Test 3 : Figure out yourself. The input will be stdin and you should print your solution to stdout Now solve the problem and return the code.
```python t =int(input()) #no. of test cases while t>0: t=t-1 str=input() size=len(str) pos=str.find('W') left=pos right=size-pos-1 arr = [[0 for i in range(right+1)] for j in range(left+1)] #arr[i,j] = 1 if with i black cells on left and j on right 1st player can         win, 0 otherwise. #Recursion: arr[i][j]= or(arr[x][y]) arr[0][0]=0 for i in range(left+1): for j in range(right+1): if i==j: arr[i][j]=0 else: arr[i][j]=1 if(arr[left][right]==1): print("Aleksa") else: print("Chef") ```
{ "language": "python", "test_cases": [ { "input": "3\nW\nBW\nBWBB\n", "output": "Chef\nAleksa\nAleksa\n", "type": "stdin_stdout" } ] }
{ "difficulty": "interview", "problem_url": "https://www.codechef.com/problems/BWCELL" }
vfc_1354
apps
verifiable_code
629
Solve the following coding problem using the programming language python: Naturally, the magical girl is very good at performing magic. She recently met her master wizard Devu, who gifted her R potions of red liquid, B potions of blue liquid, and G potions of green liquid. - The red liquid potions have liquid amounts given by r[1], ..., r[R] liters. - The green liquid potions have liquid amounts given by g[1], ..., g[G] liters. - The blue liquid potions have liquid amounts given by b[1], ..., b[B] liters. She want to play with the potions by applying magic tricks on them. In a single magic trick, she will choose a particular color. Then she will pick all the potions of the chosen color and decrease the amount of liquid in them to half (i.e. if initial amount of liquid is x, then the amount after decrement will be x / 2 where division is integer division, e.g. 3 / 2 = 1 and 4 / 2 = 2). Because she has to go out of station to meet her uncle Churu, a wannabe wizard, only M minutes are left for her. In a single minute, she can perform at most one magic trick. Hence, she can perform at most M magic tricks. She would like to minimize the maximum amount of liquid among all of Red, Green and Blue colored potions. Formally Let v be the maximum value of amount of liquid in any potion. We want to minimize the value of v. Please help her. -----Input----- First line of the input contains an integer T denoting the number of test cases. Then for each test case, we have four lines. The first line contains four space separated integers R, G, B, M. The next 3 lines will describe the amount of different color liquids (r, g, b), which are separated by space. -----Output----- For each test case, print a single integer denoting the answer of the problem. -----Constraints----- - 1 ≤ T ≤ 1000 - 1 ≤ R, G, B, M ≤ 100 - 1 ≤ r[i], g[i], b[i] ≤ 10^9 -----Example----- Input: 3 1 1 1 1 1 2 3 1 1 1 1 2 4 6 3 2 2 2 1 2 3 2 4 6 8 Output: 2 4 4 -----Explanation----- Example case 1. Magical girl can pick the blue potion and make its liquid amount half. So the potions will now have amounts 1 2 1. Maximum of these values is 2. Hence answer is 2. The input will be stdin and you should print your solution to stdout Now solve the problem and return the code.
```python import sys import math import heapq def half(n): return n//2 def main(arr,m): a,b,c=arr while m!=0: s=max(a,b,c) if s==a: a=half(a) elif s==b: b=half(b) else: c=half(c) m-=1 return max(a,b,c) for i in range(int(input())): r,g,b,m=list(map(int,input().split())) arr=[] for j in range(3): c=max(list(map(int,input().split()))) arr.append(c) print(main(arr,m)) ```
{ "language": "python", "test_cases": [ { "input": "3\n1 1 1 1\n1\n2\n3\n1 1 1 1\n2\n4\n6\n3 2 2 2\n1 2 3\n2 4\n6 8\n", "output": "2\n4\n4\n", "type": "stdin_stdout" } ] }
{ "difficulty": "interview", "problem_url": "https://www.codechef.com/problems/PRPOTION" }
vfc_1358
apps
verifiable_code
631
Solve the following coding problem using the programming language python: In Ciel's restaurant, a waiter is training. Since the waiter isn't good at arithmetic, sometimes he gives guests wrong change. Ciel gives him a simple problem. What is A-B (A minus B) ? Surprisingly, his answer is wrong. To be more precise, his answer has exactly one wrong digit. Can you imagine this? Can you make the same mistake in this problem? -----Input----- An input contains 2 integers A and B. -----Output----- Print a wrong answer of A-B. Your answer must be a positive integer containing the same number of digits as the correct answer, and exactly one digit must differ from the correct answer. Leading zeros are not allowed. If there are multiple answers satisfying the above conditions, anyone will do. -----Constraints----- 1 ≤ B < A ≤ 10000 -----Sample Input----- 5858 1234 -----Sample Output----- 1624 -----Output details----- The correct answer of 5858-1234 is 4624. So, for instance, 2624, 4324, 4623, 4604 and 4629 will be accepted, but 0624, 624, 5858, 4624 and 04624 will be rejected. -----Notes----- The problem setter is also not good at arithmetic. The input will be stdin and you should print your solution to stdout Now solve the problem and return the code.
```python a, b = [int(x) for x in input().split()] r = list(str(a-b)) if r[0] == "1": r[0] = "2" else: r[0]="1" print("".join(r)) ```
{ "language": "python", "test_cases": [ { "input": "5858 1234\n", "output": "1624\nOutput details\nThe correct answer of 5858-1234 is 4624.\nSo, for instance, 2624, 4324, 4623, 4604 and 4629 will be accepted, but 0624, 624, 5858, 4624 and 04624 will be rejected.\nNotes\nThe problem setter is also not good at arithmetic.\n", "type": "stdin_stdout" } ] }
{ "difficulty": "interview", "problem_url": "https://www.codechef.com/COOK17/problems/CIELAB" }
vfc_1366
apps
verifiable_code
632
Solve the following coding problem using the programming language python: Sheldon is a little geek living in Texas. While his friends like to play outside, little Sheldon likes to play around with ICs and lasers in his house. He decides to build N clap activated toggle machines each with one power inlet and one outlet. Each machine works when its power source inlet is receiving power. When the machine is in 'ON' state and is receiving power at its inlet, it makes power available at its power outlet to which a bulb or another ToGgLe machine could be attached. Suppose Sheldon attached 2 such machines to one another with the power inlet of the first machine attached to a power source at his house and the outlet of the second machine to a bulb. Initially both machines are in 'OFF' state and power source to first machine is off too. Now the power source is switched on. The first machine receives power but being in the 'OFF' state it does not transmit any power. Now on clapping the first ToGgLe machine toggles to 'ON' and the second machine receives power. On clapping once more the first toggles to 'OFF' and the second toggles to 'ON'. But since the second ToGgLe machine receives no power the bulb does not light up yet. On clapping once more, the first machine which is still receiving power from the source toggles to 'ON' and the second which was already 'ON' does not toggle since it was not receiving power. So both the machine are in 'ON' state and the bulb lights up and little Sheldon is happy. But when Sheldon goes out for a while, his evil twin sister attaches N such ToGgLe machines (after making sure they were all in 'OFF' state) and attaches the first to a power source (the power source is initially switched off) and the last ToGgLe machine to a bulb. Sheldon is horrified to find that his careful arrangement has been disturbed. Coders, help the poor boy by finding out if clapping k times for the N ToGgLe machines (all in 'OFF' state with the first one connected to a switched off power source and last one to a bulb) would light the bulb. Hurry before Sheldon has a nervous breakdown! -----Input----- First line has number of test cases, T. Following T lines have N, k separated by a single space where N is the number of ToGgLe machines and k is the number of times Sheldon clapped. -----Output----- T lines with cach line of the form: "ON" (just the word on without the double quotes) if the bulb is 'ON' for the test case numbered n and "OFF" (just the word off without the double quotes) if the bulb is 'OFF' for the test case numbered n. -----Example----- Input: 4 4 0 4 47 1 0 1 1 Output: OFF ON OFF ON The input will be stdin and you should print your solution to stdout Now solve the problem and return the code.
```python n=int(input()) while n>0: i=1 a,b=(int(i) for i in input().split()) if (b+1)%(i<<a)==0: print("ON") else: print("OFF") n=n-1 ```
{ "language": "python", "test_cases": [ { "input": "4\n4 0\n4 47\n1 0\n1 1\n", "output": "OFF\nON\nOFF\nON\n", "type": "stdin_stdout" } ] }
{ "difficulty": "interview", "problem_url": "https://www.codechef.com/BTCD2012/problems/T05" }
vfc_1370
apps
verifiable_code
633
Solve the following coding problem using the programming language python: Well known investigative reporter Kim "Sherlock'' Bumjun needs your help! Today, his mission is to sabotage the operations of the evil JSA. If the JSA is allowed to succeed, they will use the combined power of the WQS binary search and the UFDS to take over the world! But Kim doesn't know where the base is located. He knows that the base is on the highest peak of the Himalayan Mountains. He also knows the heights of each of the $N$ mountains. Can you help Kim find the height of the mountain where the base is located? -----Input:----- - First line will contain $T$, number of testcases. Then the testcases follow. - The first line in each testcase contains one integer, $N$. - The following $N$ lines of each test case each contain one integer: the height of a new mountain. -----Output:----- For each testcase, output one line with one integer: the height of the tallest mountain for that test case. -----Constraints----- - $1 \leq T \leq 10$ - $1 \leq N \leq 100000$ - $0 \leq$ height of each mountain $\leq 10^9$ -----Subtasks:----- - 100 points: No additional constraints. -----Sample Input:----- 1 5 4 7 6 3 1 -----Sample Output:----- 7 The input will be stdin and you should print your solution to stdout Now solve the problem and return the code.
```python n=int(input()) def do(): t=int(input()) x=[] for i in range(t): x.append(int(input())) print(max(x)) return for i in range(n): do() ```
{ "language": "python", "test_cases": [ { "input": "1\n5\n4\n7\n6\n3\n1\n", "output": "7\n", "type": "stdin_stdout" } ] }
{ "difficulty": "interview", "problem_url": "https://www.codechef.com/problems/UWCOI20A" }
vfc_1374
apps
verifiable_code
634
Solve the following coding problem using the programming language python: Lyra Belacqua is a very gifted girl. She is one of a very small set of people capable of reading an alethiometer, more commonly known as The Golden Compass. It has one specific use: to tell the truth. The name in fact, is derived from "Aletheia" meaning truth, and "-ometer", meaning "measuring device". The alethiometer had four needles, out of which the user would direct three of them to lie over symbols on the face of the device to ask a question. The fourth needle then swung into action and pointed to various symbols one after another, thus telling the answer. For this problem, consider the alethiometer consisting of symbols : digits '0'-'9' and letters 'A'-'Z'. Learned scholars were debating the age of the Universe, and they requested Lyra to find out the age from the alethiometer. Having asked the question, the fourth needle started spouting out symbols, which Lyra quickly recorded. In that long string of characters, she knows that some substring corresponds to the age of the Universe. She also knows that the alethiometer could have wrongly pointed out atmost one digit (0-9) as a letter (A-Z). She then wonders what is the maximum possible age of the Universe. Given the set of symbols the alethiometer pointed out, help her find the maximum age of the Universe, which could correspond to a substring of the original string with atmost one letter changed. Note: We consider a substring to be a contiguous part of the string S Also, the alethiometer wrongly reports only a letter. All the digits remain as they are. -----Input----- Each input consists of a single string S which is what Lyra recorded from the fourth needle's pointing. -----Output----- Output one number, the maximum possible answer. -----Constraints----- - 1 ≤ |S| ≤ 1,000 - S will only contain digits 0-9 and uppercase Latin letters. -----Example----- Input1: 06454 Input2: C0D3C43F Output1: 6454 Output2: 3943 -----Explanation----- In the first example, there is no choice as to what the number can be. It has to be 6,454. In the second example, there are a total of 41 possible strings (one for the original, and 10 for changing each letter). You can verify that the maximum number as a substring is got by making the string "C0D3943F". The input will be stdin and you should print your solution to stdout Now solve the problem and return the code.
```python string=input() max_no=0 for i in range(len(string)): var_occur=0 check_no=str() j=i while(j<len(string) and var_occur<2 ): if(string[j].isalpha()): if(var_occur==0): check_no+='9' var_occur+=1 else: var_occur+=1 else: check_no+=string[j] j+=1 #print(check_no) max_no=max(max_no,int(check_no)) print(max_no) ```
{ "language": "python", "test_cases": [ { "input": "06454\nInput2:\nC0D3C43F\n", "output": "6454\nOutput2:\n3943\n", "type": "stdin_stdout" } ] }
{ "difficulty": "interview", "problem_url": "https://www.codechef.com/problems/ALETHIO" }
vfc_1378
apps
verifiable_code
635
Solve the following coding problem using the programming language python: Chef is given a sequence of prime numbers $A_1, A_2, \ldots, A_N$. This sequence has exactly $2^N$ subsequences. A subsequence of $A$ is good if it does not contain any two identical numbers; in particular, the empty sequence is good. Chef has to find the number of good subsequences which contain at most $K$ numbers. Since he does not know much about subsequences, help him find the answer. This number could be very large, so compute it modulo $1,000,000,007$. -----Input----- - The first line of the input contains two space-separated integers $N$ and $K$. - The second line contains $N$ space-separated integers $A_1, A_2, \ldots, A_N$. -----Output----- Print a single line containing one integer ― the number of good subsequences with size at most $K$, modulo $1,000,000,007$. -----Constraints----- - $1 \le K \le N \le 10^5$ - $2 \le A_i \le 8,000$ for each valid $i$ -----Subtasks----- Subtask #1 (40 points): $A_1, A_2, \ldots, A_N$ are pairwise distinct Subtask #2 (60 points): original constraints -----Example Input----- 5 3 2 2 3 3 5 -----Example Output----- 18 -----Explanation----- There is $1$ good subsequence with length $0$, $5$ good subsequences with length $1$, $8$ good subsequences with length $2$ and $4$ good subsequences with length $3$. The input will be stdin and you should print your solution to stdout Now solve the problem and return the code.
```python # cook your dish here from collections import Counter def solve(arr, n, k): ans = 0 dict1 = {} mod = 1000000007 for i in range(n): if arr[i] in dict1: dict1[arr[i]] += 1 else: dict1[arr[i]] = 1 l1 = [0]+list(dict1.keys()) v = min(k, len(l1)) dp = [[0 for _ in range(v+1)]for _ in range(len(l1))] dp[0][0] = 1 for i in range(1, len(l1)): dp[i][0] = 1 for j in range(1, v+1): dp[i][j] = dp[i-1][j] + dp[i-1][j-1]*dict1[l1[i]] for i in range(v+1): ans += dp[len(l1)-1][i] ans = ans%mod return ans n, k = map(int, input().strip().split()) arr = list(map(int, input().strip().split())) print(solve(arr, n, k)) ```
{ "language": "python", "test_cases": [ { "input": "5 3\n2 2 3 3 5\n", "output": "18\n", "type": "stdin_stdout" } ] }
{ "difficulty": "interview", "problem_url": "https://www.codechef.com/problems/GDSUB" }
vfc_1382
apps
verifiable_code
636
Solve the following coding problem using the programming language python: In this problem you are given a sequence of $N$ positive integers $S[1],S[2],\dots,S[N]$. In addition you are given an integer $T$, and your aim is to find the number of quadruples $(i,j,k,l)$, such that $1 \le i < j < k < l \le N$, and $S[i] + S[j] + S[k] + S[l] = T$. That is, the number of ways of picking four numbers from the sequence summing up to $T$. -----Constraints:----- For all test cases, - $1 \le N \le 5000$ - $1 \le T \le 10^6$ - $1 \le S[i] \le 10^9$, for all $i$. Subtask $1:10\%$ It is guaranteed that $N \le 50$. Subtask $2:50\%$ It is guaranteed that $N \le 300$. Subtask $3:40\%$ No additional guarantees. -----Input Format:----- There is only one line of input with $N+2$ space separated integers. The first two integers are $N$ and $T$. The next $N$ integers are $S[1],S[2],\dots,S[N]$. -----Output Format:----- A single integer which is the number of valid quadruples. -----Sample Input 1:----- 6 20 3 1 1 2 5 10 -----Sample Output 1:----- 1 -----Explanation 1:----- The quadruple $(1,4,5,6)$ is valid because $S[1]+S[4]+S[5]+S[6]=3+2+5+10=20$. You can check that no other quadruple is valid and hence the answer is $1$. -----Sample Input 2:----- 6 13 1 2 3 4 5 4 -----Sample Output 2:----- 3 -----Explanation 2:----- You can verify that the only quadruples that are valid are $(2,3,4,6),(1,3,4,5)$ and $(1,3,5,6)$. Thus, the answer is $3$. -----Note:----- As the answer might be large, please use 64 bit integers (long long int in C/C++ and long in Java) instead of 32 bit int. The input will be stdin and you should print your solution to stdout Now solve the problem and return the code.
```python # cook your dish here from itertools import combinations a = list(map(int, input().split())) n = a[0] t = a[1] q = list(combinations(a[2:], 4)) total = 0 for i in q: if sum(i) == t: total += 1 print(total) ```
{ "language": "python", "test_cases": [ { "input": "6 20 3 1 1 2 5 10\n", "output": "1\n", "type": "stdin_stdout" } ] }
{ "difficulty": "interview", "problem_url": "https://www.codechef.com/ZCOPRAC/problems/ZCO17001" }
vfc_1386
apps
verifiable_code
637
Solve the following coding problem using the programming language python: A balanced parenthesis string is defined as follows: - The empty string is balanced - If P is balanced, (P) is also - If P and Q are balanced, PQ is also balanced You are given two even integers n$n$ and k$k$. Find any balanced paranthesis string of length n$n$ that doesn't contain a balanced substring of length k$k$, or claim that no such string exists. -----Input----- - First line will contain T$T$, number of testcases. Then the testcases follow. - Each testcase contains of a single line containing n$n$ and k$k$. -----Output----- For every testcase, print on a new line, any balanced paranthesis string of length n$n$ that doesn't contain a balanced substring of length k$k$. If there doesn't exist any such string, print −1$-1$ instead. -----Constraints----- - 1≤T≤50000$1 \leq T \leq 50000$ - 2≤k≤n≤105$2 \leq k \leq n \leq 10^5$ - Sum of n$n$ over all testcases doesn't exceed 105$10^5$. - n$n$ and k$k$ are both even integers. -----Example Input----- 2 4 2 8 6 -----Example Output----- -1 (())(()) -----Explanation----- In the first testcase, the only balanced strings of length 4$4$ are (()) and ()(), both of which contain () as a substring. In the second testcase, (())(()) is a balanced string that doesn't contain any balanced substring of length 6$6$. The input will be stdin and you should print your solution to stdout Now solve the problem and return the code.
```python import sys def input(): return sys.stdin.readline().strip() for i in range(int(input())): n, k = map(int, input().split()) arr = [] if k == 2 or k == 4 or n % 2 != 0 or n == k: arr.append('-1') elif k % 2 != 0: for i in range(int(n / 2)): arr.append('(') for i in range(int(n / 2)): arr.append(')') elif int(n / (k - 2)) == 1: if (n - 2) % 4 == 0: for i in range(int((n - 2) / 4)): arr.append('(') for i in range(int((n - 2) / 4)): arr.append(')') arr.append('()') for i in range(int((n - 2) / 4)): arr.append('(') for i in range(int((n - 2) / 4)): arr.append(')') else: for i in range(int((n - 4) / 4)): arr.append('(') for i in range(int((n - 4) / 4)): arr.append(')') arr.append('(())') for i in range(int((n - 4) / 4)): arr.append('(') for i in range(int((n - 4) / 4)): arr.append(')') else: for i in range(int((n % (k - 2)) / 2)): arr.append('(') for i in range(int(n / (k - 2))): for j in range(int((k - 2) / 2)): arr.append('(') for j in range(int((k - 2) / 2)): arr.append(')') for i in range(int((n % (k - 2)) / 2)): arr.append(')') print("".join(arr)) ```
{ "language": "python", "test_cases": [ { "input": "2\n4 2\n8 6\n", "output": "-1\n(())(())\n", "type": "stdin_stdout" } ] }
{ "difficulty": "interview", "problem_url": "https://www.codechef.com/problems/UNBAL" }
vfc_1390
apps
verifiable_code
638
Solve the following coding problem using the programming language python: You will be given m strings. For each of those strings, you need to count the total number of appearances of that string as substrings in all possible strings of length n containing only lower case English letters. A string may appear in a string multiple times. Also, these appearances may overlap. All these must be counted separately. For example, aa appears thrice in the string aaacaa: aaacaa, aaacaa and aaacaa. -----Input----- - The first line contains one integer, T, the number of test cases. The description of each test case follows: - The first line of each test case will contain two integers n and m. - The ith of the next m lines will have one string in each line. All the strings will consist only of lower case English letters. -----Output----- - For each test case, print "Case x:" (without quotes. x is the test case number, 1-indexed) in the first line. - Then print m lines. The ith line should contain the number of appearances of the ith string in all possible strings of length n. As the numbers can be very large, print the answers modulo 109+7. -----Constraints----- - 1 ≤ T ≤ 100 - 1 ≤ n ≤ 100000 - 1 ≤ m ≤ 1000 - 1 ≤ Length of every string in input - 1 ≤ Total length of all strings in one test case ≤ 5 * 105 - 1 ≤ Total length of all strings in one test file ≤ 5 * 106 -----Example----- Input: 3 2 1 aa 2 1 d 12 3 cdmn qweewef qs Output: Case 1: 1 Case 2: 52 Case 3: 443568031 71288256 41317270 -----Explanation:----- Testcase 1: aa is the only string of length 2 which contains aa as a substring. And it occurs only once. Hence the answer is 1. The input will be stdin and you should print your solution to stdout Now solve the problem and return the code.
```python for _ in range(int(input())): n,m=map(int,input().split()) print("Case "+str(_+1)+":") for i in range(m): s=input() ls=len(s) if ls>n: print("0") else: k=(n-ls+1) print((k*pow(26,n-ls,1000000007))%1000000007) ```
{ "language": "python", "test_cases": [ { "input": "3\n2 1\naa\n2 1\nd\n12 3\ncdmn\nqweewef\nqs\n\n\n", "output": "Case 1:\n1\nCase 2:\n52\nCase 3:\n443568031\n71288256\n41317270\n", "type": "stdin_stdout" } ] }
{ "difficulty": "interview", "problem_url": "https://www.codechef.com/problems/ZUBAPCNT" }
vfc_1394
apps
verifiable_code
639
Solve the following coding problem using the programming language python: For a string $S$ let the unique set of characters that occur in it one or more times be $C$. Consider a permutation of the elements of $C$ as $(c_1, c_2, c_3 ... )$. Let $f(c)$ be the number of times $c$ occurs in $S$. If any such permutation of the elements of $C$ satisfies $f(c_i) = f(c_{i-1}) + f(c_{i-2})$ for all $i \ge 3$, the string is said to be a dynamic string. Mr Bancroft is given the task to check if the string is dynamic, but he is busy playing with sandpaper. Would you help him in such a state? Note that if the number of distinct characters in the string is less than 3, i.e. if $|C| < 3$, then the string is always dynamic. -----Input:----- - First line will contain $T$, number of testcases. Then the testcases follow. - Each testcase contains of a single line of input, a string $S$. -----Output:----- For each testcase, output in a single line "Dynamic" if the given string is dynamic, otherwise print "Not". (Note that the judge is case sensitive) -----Constraints----- - $1 \leq T \leq 10$ - $1 \leq |S| \leq 10^5$ - $S$ contains only lower case alphabets: $a$, $b$, …, $z$ -----Sample Input:----- 3 aaaabccc aabbcc ppppmmnnoooopp -----Sample Output:----- Dynamic Not Dynamic -----Explanation:----- - Testase 1: For the given string, $C = \{a, b, c\}$ and $f(a)=4, f(b)=1, f(c)=3$. $f(a) = f(c) + f(b)$ so the permutation $(b, c, a)$ satisfies the requirement. - Testcase 2: Here too $C = \{a, b, c\}$ but no permutation satisfies the requirement of a dynamic string. - Testcase 3: Here $C = \{m, n, o, p\}$ and $(m, n, o, p)$ is a permutation that makes it a dynamic string. The input will be stdin and you should print your solution to stdout Now solve the problem and return the code.
```python # cook your dish here t=int(input()) for _ in range(t): st=input() s=set(st) a=[] f1=f2=0 for i in s: a.append(st.count(i)) a.sort() if len(a)>=3: for i in range(2,len(a)): if a[i]!=a[i-1]+a[i-2]: f1=1 break x=a[0] a[0]=a[1] a[1]=x for i in range(2,len(a)): if a[i]!=a[i-1]+a[i-2]: f2=1 break if f1==1 and f2==1: print("Not") else: print("Dynamic") else: print("Dynamic") ```
{ "language": "python", "test_cases": [ { "input": "3\naaaabccc\naabbcc\nppppmmnnoooopp\n", "output": "Dynamic\nNot\nDynamic\n", "type": "stdin_stdout" } ] }
{ "difficulty": "interview", "problem_url": "https://www.codechef.com/problems/CLFIBD" }
vfc_1398
apps
verifiable_code
640
Solve the following coding problem using the programming language python: Chef made two laddus with sweetness X and Y respectively. Cheffina comes and sees the chef created two laddus with different sweetness (might be same). Cheffina has the magical power to make the sweetness of laddus equal. Cheffina requires 1 unit of power to increase the sweetness of laddu by its original value i.e. 1 unit to convert Z to 2Z and 2 unit to convert Z to 3Z and so on… How many units of power does cheffina want to make the sweetness equal? -----Input:----- - First-line will contain $T$, the number of test cases. Then the test cases follow. - Each test case contains a single line of input, two integers $X, Y$. -----Output:----- For each test case, output in a single line answer as power required. -----Constraints----- - $1 \leq T \leq 10^5$ - $1 \leq X,Y \leq 10^5$ -----Sample Input:----- 2 2 2 4 6 -----Sample Output:----- 0 3 -----EXPLANATION:----- For 1) Sweetness are same so no need to use power. For 2) 1st laddu 2 Unit power = 4 -> 12 2nd Laddu 1 Unit power = 6 -> 12 After using total 3 unit power sweetness of both laddus are same. The input will be stdin and you should print your solution to stdout Now solve the problem and return the code.
```python def gcd(a,b): if b==0:return a else:return gcd(b,a%b) def lcm(a,b): m=a*b g=gcd(a,b) return int(m/g) for _ in range(int(input())): x,y=[int(x) for x in input().split()] l=lcm(x,y) s=int(l/x) t=int(l/y) print(s+t-2) ```
{ "language": "python", "test_cases": [ { "input": "2\n2 2\n4 6\n", "output": "0\n3\n", "type": "stdin_stdout" } ] }
{ "difficulty": "interview", "problem_url": "https://www.codechef.com/PBK22020/problems/ITGUY22" }
vfc_1402
apps
verifiable_code
641
Solve the following coding problem using the programming language python: A key feature of the Siruseri railway network is that it has exactly one route between any pair of stations. The government has chosen three contractors to run the canteens at the stations on the railway network. To ensure that there are no disputes between the contractors it has been decided that if two stations, say $A$ and $B$, are assigned to a particular contractor then all the stations that lie on the route from $A$ to $B$ will also be awarded to the same contractor. The government would like the assignment of stations to the contractors to be as equitable as possible. The government has data on the number of passengers who pass through each station each year. They would like to assign stations so that the maximum number of passengers passing through any contractor's collection of stations is minimized. For instance, suppose the railway network is as follows, where the volume of passenger traffic is indicated by the side of each station. One possible assignment would to award stations $1$ and $3$ to one contractor (there by giving him a traffic of $35$ passengers), station $2$ to the second contractor (traffic of $20$) and stations $4, 5$ and $6$ to the third contractor (traffic of $100$). In this assignment, the maximum traffic for any one contractor is 100. On the other hand if we assigned stations $1, 2$ and $3$ to one contractor, station $4$ and $6$ to the second contractor and station $5$ to the third contractor the maximum traffic for any one contractor is $70$. You can check that you cannot do better. (The assignment $1$, $2$ and $3$ to one contractor, $4$ to the second contractor, and $5$ and $6$ to the third contractor has a lower value for the maximum traffic ($55$) but it is not a valid assignment as the route from $5$ to $6$ passes through $4$.) -----Input:----- The first line of the input contains one integer $N$ indicating the number of railways stations in the network. The stations are numbered $1,2,..., N$. This is followed by $N$ lines of input, lines $2,3,...,N+1$, indicating the volume of traffic at each station. The volume of traffic at station $i$, $1 \leq i \leq N$, is given by a single integer in line $i+1$. The next $N-1$ lines of input, lines $N+2, N+3, ..., 2 \cdot N$, describe the railway network. Each of these lines contains two integers, denoting a pair of stations that are neighbours. -----Output:----- The output should be a single integer, corresponding to the minimum possible value of the maximum traffic of any contractor among all valid assignment of the stations to the three contractors. -----Constraints:----- - $1 \leq N \leq 3000$. -----Sample Input----- 6 10 20 25 40 30 30 4 5 1 3 3 4 2 3 6 4 -----Sample Output----- 70 The input will be stdin and you should print your solution to stdout Now solve the problem and return the code.
```python n=int(input()) cost=[] d={} val_desc=[0]*n visited=set() visited.add(0) dfstack=[] desc = [[False for i in range(n)] for i in range(n)] for i in range(n): cost.append(int(input())) d[i]=[] for i in range(n-1): j,k=list(map(int,input().split())) d[j-1].append(k-1) d[k-1].append(j-1) def dfs(u): val_desc[u]+=cost[u] dfstack.append(u) for i in dfstack: desc[u][i]=True for i in d[u]: if i not in visited: visited.add(i) dfs(i) val_desc[u]+=val_desc[i] dfstack.pop(-1) dfs(0) mp=10**9 coco=sum(cost) for i in range(n): for j in range(i+1,n): vali=val_desc[i] valj=val_desc[j] if desc[i][j]: valj-=val_desc[i] if desc[j][i]: vali-=val_desc[j] p=max(vali,valj,coco-vali-valj) mp=min(mp,p) #print(desc) #print(val_desc) #print print(mp) ```
{ "language": "python", "test_cases": [ { "input": "6\n10\n20\n25\n40\n30\n30\n4 5\n1 3\n3 4\n2 3\n6 4\n", "output": "70\n", "type": "stdin_stdout" } ] }
{ "difficulty": "interview", "problem_url": "https://www.codechef.com/IARCSJUD/problems/SPLIT3" }
vfc_1406
apps
verifiable_code
643
Solve the following coding problem using the programming language python: Ted$Ted$ loves prime numbers. One day he is playing a game called legendary$legendary$ with his girlfriend Robin$Robin$. Ted$Ted$ writes a number N$N$ on a table and the number is in the form of : N = P1A1 * P2A2 * ……….. * PnAn Ted$Ted$ asks Robin$Robin$ to find the sum of all the numbers which are less than or equal to N$N$ and also contains all the primes whose minimum power in the number is given by an array B$B$. As Robin$Robin$ is bad with maths she asks your help to answer this question. -----Input:----- - The first line of the input contains a single integer T$T$ denoting the number of test cases. The description of T$T$ test cases follows. - The first line of each test case contains a single integer n$n$, represents a number of distinct prime numbers. - The second line of each test case contains n$n$ space separated distinct prime numbers which represents an array P$P$. - The third line of each test case contains n$n$ space separated integers which represents an array A$A$. - The fourth line of each test case contains n$n$ space separated integers which represents an array B$B$ -----Output:----- For each test case, output the Answer Modulo 109 + 7 in a single line. -----Constraints----- - 1≤T≤3$1 \leq T \leq 3$ - 1≤n≤105$1 \leq n \leq 10^5$ - 2≤Pi≤106$2 \leq P_{i} \leq 10^6$ - 1≤Ai≤109$1 \leq A_{i} \leq 10^9$ - 0≤Bi≤Ai$0 \leq B_{i} \leq Ai$ -----Sample Input:----- 1 3 2 3 5 2 1 2 1 1 1 -----Sample Output:----- 540 -----EXPLANATION:----- 22 * 31 * 52= 300 which is N over here. The four numbers less than or equal to 300 are 30, 60, 150 and 300. 30 = 21 * 31 * 51 , 60 = 22 * 31 * 51, 150 = 21 * 31 * 52 and 300 = 22 * 31 * 52. In the 4 numbers, the minimum powers for 2, 3 and 5 are 1, 1 and 1 or more than them in every case. The input will be stdin and you should print your solution to stdout Now solve the problem and return the code.
```python d = 10**9 + 7 t = int(input()) while t: t-=1 n =int(input()) p =list(map(int, input().strip().split())) a =list(map(int, input().strip().split())) b =list(map(int, input().strip().split())) ans = 1 for i in range(n): c = a[i] - b[i] + 1 tmp = (( pow(p[i],b[i],d) * ((pow(p[i],c,d) - 1 + d)%d) * pow(p[i]-1 , d-2, d)%d)) ans *= tmp ans = ans%d print(ans) ```
{ "language": "python", "test_cases": [ { "input": "1\n3\n2 3 5\n2 1 2\n1 1 1\n", "output": "540\n", "type": "stdin_stdout" } ] }
{ "difficulty": "interview", "problem_url": "https://www.codechef.com/AARA2018/problems/ARMBH4" }
vfc_1414
apps
verifiable_code
644
Solve the following coding problem using the programming language python: There are $N$ friends in a group. Each of them have $A_{i}$ candies. Can they share all of these candies among themselves such that each one of them have equal no. of candies. -----Input:----- - First line will contain $T$, number of testcases. Then the testcases follow. - First line of each testcase contains of a single line of input, an integer $N$ denoting no. of friends in the group. - Next line contains $N$ space separated integers $A_{i}$ denoting the no. candies $i^{th}$ friend has. -----Output:----- For each testcase, output $"Yes"$ if it is possible to share equally else $"No"$ (without " "). -----Constraints----- - $1 \leq T \leq 10$ - $1 \leq N \leq 100$ - $0 \leq A_{i} \leq 1000$ -----Sample Input:----- 1 3 1 2 3 -----Sample Output:----- Yes -----EXPLANATION:----- Each of them have $2$ candies after sharing. The input will be stdin and you should print your solution to stdout Now solve the problem and return the code.
```python # cook your dish here for _ in range(int(input())): friends = int(input()) candies = list(map(int,input().split())) if (sum(candies) % friends == 0): print("Yes") else: print("No") ```
{ "language": "python", "test_cases": [ { "input": "1\n3\n1 2 3\n", "output": "Yes\n", "type": "stdin_stdout" } ] }
{ "difficulty": "interview", "problem_url": "https://www.codechef.com/ENJU2020/problems/ECJN202" }
vfc_1418
apps
verifiable_code
645
Solve the following coding problem using the programming language python: Chef has $K$ chocolates and he wants to distribute them to $N$ people (numbered $1$ through $N$). These people are standing in a line in such a way that for each $i$ ($1 \le i \le N-1$), person $i$ and person $i+1$ are adjacent. First, consider some way to distribute chocolates such that for each valid $i$, the number of chocolates the $i$-th person would receive from Chef is $A_i$ and the sum $S_1 = \sum_{i=1}^{N-1} \left|A_i - A_{i+1}\right|$ is minimum possible. Of course, each person must receive a non-negative integer number of chocolates. Then, Chef wants to create a new sequence $B_1, B_2, \ldots, B_N$ by rearranging (permuting) the elements of the sequence $A_1, A_2, \ldots, A_N$. For each valid $i$, the number of chocolates the $i$-th person actually receives from Chef is $B_i$. Chef wants to distribute the chocolates (choose $B_1, B_2, \ldots, B_N$ by permuting the sequence $A$ and give $B_i$ chocolates to the $i$-th person for each valid $i$) in such a way that $S_2 = \sum_{i=1}^{N-1} \left|B_i – B_{i+1}\right|$ is maximum possible. You need to find the maximum value of $S_2$. It is guaranteed that $S_2$ does not depend on the exact choice of the sequence $A_1, A_2, \ldots, A_N$, as long as it is a sequence that minimises $S_1$. -----Input----- - 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 line of each test case contains a single integer $N$. - The second line contains a single integer $K$. -----Output----- For each test case, print a single line containing one integer — the maximum value of the sum $S_2$. -----Constraints----- - $1 \le T \le 10$ - $2 \le N \le 10^5$ - $2 \le K \le 10^{100,000}$ -----Subtasks----- Subtask #1 (30 points): $2 \le N, K \le 1,000$ Subtask #2 (70 points): original constraints -----Example Input----- 1 3 2 -----Example Output----- 2 -----Explanation----- Example case 1: To minimise $S_1$, Chef could give $1$ chocolate to person $1$ and $1$ chocolate to person $2$, so $S_1 = |1-1| + |1-0| = 1$. To maximise $S_2$, Chef can give $1$ chocolate to person $1$ and $1$ chocolate to person $3$, since the sequence $B = (1, 0, 1)$ is a permutation of $A = (1, 1, 0)$. Then, $S_2 = |1-0| + |0-1| = 2$. The input will be stdin and you should print your solution to stdout Now solve the problem and return the code.
```python t = int(input()) for _ in range(t): n = int(input()) k = int(input()) num = int(k/n) x = max(n*(1+num) - k, 0) diff = abs(x - (n-x)) if diff == 0: number = 2*x - 1 else: number = min(x, n-x)*2 print(number) ```
{ "language": "python", "test_cases": [ { "input": "1\n3\n2\n", "output": "2\n", "type": "stdin_stdout" } ] }
{ "difficulty": "interview", "problem_url": "https://www.codechef.com/problems/MMAX" }
vfc_1422
apps
verifiable_code
646
Solve the following coding problem using the programming language python: Given a string $s$. You can perform the following operation on given string any number of time. Delete two successive elements of the string if they are same. After performing the above operation you have to return the least possible length of the string. -----Input:----- - First line will contain $T$, number of testcases. Then the testcases follow. - Each testcase contains of a single line of input, a string $s$. -----Output:----- For each testcase, output in a single line answer- minimum length of string possible after performing given operations. -----Constraints----- - $1 \leq T \leq 1000$ - $2 \leq length of string \leq 10^5$ $s$ contains only lowercase letters. -----Sample Input:----- 3 abccd abbac aaaa -----Sample Output:----- 3 1 0 -----EXPLANATION:----- - In first case, $"abd"$ will be final string. - in second case, $"c"$ will be final string The input will be stdin and you should print your solution to stdout Now solve the problem and return the code.
```python from sys import stdin,stdout import math,bisect from datetime import date from collections import Counter,deque,defaultdict L=lambda:list(map(int, stdin.readline().strip().split())) M=lambda:list(map(int, stdin.readline().strip().split())) I=lambda:int(stdin.readline().strip()) S=lambda:stdin.readline().strip() C=lambda:stdin.readline().strip().split() def pr(a):return("".join(list(map(str,a)))) #_________________________________________________# def solve(): s = list(S()) a=[s[0]] for i in range(1,len(s)): if a and a[-1]==s[i]: a.pop() else: a.append(s[i]) print(len(a)) for _ in range(I()): solve() ```
{ "language": "python", "test_cases": [ { "input": "3\nabccd\nabbac\naaaa\n", "output": "3\n1\n0\n", "type": "stdin_stdout" } ] }
{ "difficulty": "interview", "problem_url": "https://www.codechef.com/CDGO2021/problems/MINLEN" }
vfc_1426
apps
verifiable_code
647
Solve the following coding problem using the programming language python: Ashley wrote a random number generator code. Due to some reasons, the code only generates random positive integers which are not evenly divisible by 10. She gives $N$ and $S$ as input to the random number generator. The code generates a random number with number of digits equal to $N$ and sum of digits equal to $S$. The code returns -1 if no number can be generated. Print "-1" in such cases (without quotes). Else print the minimum possible product of digits of the random number generated. -----Input:----- - First line will contain a single integer $T$, the number of testcases. - Each testcase consists of two space separated integers, $N$ and $S$. -----Output:----- For each testcase, output the answer on a new line. -----Constraints----- - $1 \leq T \leq 10^5$ - $1 \leq N \leq 18$ - $1 \leq S \leq 5 * N$ -----Sample Input:----- 2 1 5 2 2 -----Sample Output:----- 5 1 -----EXPLANATION:----- In first testcase, the only possible number of length 1 having digit sum 5 is 5. And it's product of digits is 5. In second testcase, only possible two digit number as a generator output is 11(as 20 is divisible by 10, it is never generated) and product of it's digits is 1. The input will be stdin and you should print your solution to stdout Now solve the problem and return the code.
```python """ Author : thekushalghosh Team : CodeDiggers """ import sys,math input = sys.stdin.readline ############ ---- USER DEFINED INPUT FUNCTIONS ---- ############ def inp(): return(int(input())) def inlt(): return(list(map(int,input().split()))) def insr(): s = input() return(s[:len(s) - 1]) def invr(): return(map(int,input().split())) ################################################################ ############ ---- THE ACTUAL CODE STARTS BELOW ---- ############ t = 1 t = inp() for tt in range(t): n,s = invr() if n == 2 and s > 1: print(s - 1) elif n > 2 and s > 1: print(0) elif n == 1: print(s) else: print(-1) ```
{ "language": "python", "test_cases": [ { "input": "2\n1 5\n2 2\n", "output": "5\n1\n", "type": "stdin_stdout" } ] }
{ "difficulty": "interview", "problem_url": "https://www.codechef.com/NCC2020/problems/NCC005" }
vfc_1430
apps
verifiable_code
648
Solve the following coding problem using the programming language python: Chef is going to organize a hill jumping competition and he is going to be one of the judges in it. In this competition there are N hills in a row, and the initial height of i-th hill is Ai. Participants are required to demonstrate their jumping skills by doing what the judges tell them. Judges will give each participant a card which has two numbers, i and k, which means that the participant should start at the i-th hill and jump k times, where one jump should be from the current hill to the nearest hill to the right which is strictly higher (in height) than the current one. If there is no such hill or its distance (i.e. difference between their indices) is more than 100 then the participant should remain in his current hill. Please help Chef by creating a program to use it during the competitions. It should read the initial heights of the hill and should support two kinds of operations: Type 1: Given a two numbers: i and k, your program should output the index of the hill the participant is expected to finish if he starts from the i-th hill (as explained above). Type 2: Given three numbers: L, R, X, the heights of all the hills between L and R, both end points inclusive, should be increased by X (if X is negative then their height is decreased). -----Input----- - First line contains two integers N and Q, denoting the number of hills and number of operations respectively. - Second line contains N space-separated integers A1, A2, ..., AN denoting the initial heights of the hills. - Each of the next Q lines describes an operation. If the first integer is equal to 1, it means that the operation is of Type 1, and it will be followed by two integers i and k. Otherwise the first number will be equal to 2, and it means that the operation is of Type 2, and so it will be followed by three integers L, R and X. -----Output----- For each operation of Type 1, output the index of the hill in which the participant will finish. -----Constraints----- - 1 ≤ N, Q ≤ 100,000 - 1 ≤ Ai ≤ 1,000,000 - 1 ≤ L ≤ R ≤ N - -1,000,000 ≤ X ≤ 1,000,000 - 1 ≤ i, k ≤ N -----Subtasks----- - Subtask 1 (20 points) : 1 ≤ N, Q ≤ 1,000 - Subtask 2 (80 points) : Original constraints -----Example----- Input: 5 3 1 2 3 4 5 1 1 2 2 3 4 -1 1 1 2 Output: 3 4 -----Explanation----- The initial heights are (1, 2, 3, 4, 5). The first operation is of Type 1 and starts from Hill 1 and wants to jump twice. The first jump will be to Hill 2, and the second jump will be to Hill 3. Hence the output for this is 3. The second operation changes the heights to (1, 2, 2, 3, 5). The last operation starts from Hill 1. The first jump is to Hill 2. But the next jump will skip Hill 3 (because it's height is not strictly greater than the current hill's height), and will go to Hill 4. Hence the output is 4. The input will be stdin and you should print your solution to stdout Now solve the problem and return the code.
```python n,q=list(map(int,input().split())) final=[] height=list(map(int,input().split())) for k in range(0,q): b=input().split() if int(b[0])==1: step=int(b[1])-1 for k in range(0,int(b[2])): temp = 0 j=1 while j in range(1,101) and temp==0 and step+j<n: if height[step+j]>height[step]: step=step+j temp=1 j+=1 final.append(step+1) elif int(b[0])==2: for k in range(int(b[1])-1,int(b[2])): height[k]=height[k]+int(b[3]) for l in range(0,len(final)): print(final[l]) ```
{ "language": "python", "test_cases": [ { "input": "5 3\n1 2 3 4 5\n1 1 2\n2 3 4 -1\n1 1 2\n", "output": "3\n4\n", "type": "stdin_stdout" } ] }
{ "difficulty": "interview", "problem_url": "https://www.codechef.com/AUG17/problems/HILLJUMP" }
vfc_1434
apps
verifiable_code
649
Solve the following coding problem using the programming language python: Mandarin chinese , Russian and Vietnamese as well. You are given a grid with $n$ rows and $m$ columns. Each cell of this grid can either be empty or it contains one particle. It can never contain more than one particle. Let's denote the cell in the $i$-th row and $j$-th column by $(i, j)$, with the top left corner being $(0, 0)$. From a cell $(i, j)$, a particle could move in one of the following four directions: - to the left, i.e. to the cell $(i, j - 1)$ - to the right, i.e. to the cell $(i, j + 1)$ - up, i.e. to the cell $(i - 1, j)$ - down, i.e. to the cell $(i + 1, j)$ It is not possible for a particle to move to a cell that already contains a particle or to a cell that does not exist (leave the grid). It is possible to apply a force in each of these directions. When a force is applied in a given direction, all particles will simultaneously start moving in this direction as long as it is still possible for them to move. You are given a sequence of forces. Each subsequent force is applied only after all particles have stopped moving. Determine which cells of the grid contain particles after all forces from this sequence are applied in the given order. -----Input----- - 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 line of each test case contains two space-separated integers $n$ and $m$. - $n$ lines describing the initial grid follow. For each $i$ ($1 \le i \le n$), the $i$-th of these lines contains a binary string with length $m$ describing the $i$-th row of the grid. For each $j$ ($1 \le j \le m$), if the $j$-th character of this string is '1', then the cell $(i, j)$ contains a particle, and if it is '0', then the cell $(i, j)$ is empty. - The last line contains a single string $S$ describing the sequence of applied forces. Each character of this string corresponds to applying a force in some direction; forces applied in the directions left, right, up, down correspond to characters 'L', 'R', 'U', 'D' respectively. -----Output----- For each test case, print $n$ lines each containing a binary string of length $m$, describing the resulting grid (after all the forces are applied) in the same format as the input grid. -----Constraints----- - $1 \le T \le 200$ - $1 \le n, m \le 100$ - $1 \le |S| \le 2 \cdot 10^4$ -----Subtasks----- Subtaks #1 (30 points): - $1 \le T \le 10$ - $1 \le n, m \le 10$ - $1 \le |S| \le 100$ Subtask #2 (70 points): Original constraints -----Example Input----- 3 4 4 1010 0010 1001 0100 LRDU 4 3 000 010 001 101 LRL 3 2 01 10 00 D -----Example Output----- 0011 0011 0001 0001 000 100 100 110 00 00 11 -----Explanation----- Example case 1: The initial grid is: 1010 0010 1001 0100 After applying the first force (in the direction "L", i.e. to the left), the grid is: 1100 1000 1100 1000 After applying the second force (in the direction "R"), the grid is: 0011 0001 0011 0001 After applying the third force (in the direction "D"), the grid is: 0001 0001 0011 0011 After applying the fourth force (in the direction "U"), the final grid is: 0011 0011 0001 0001 The input will be stdin and you should print your solution to stdout Now solve the problem and return the code.
```python def main(): for _ in range(int(input())): rows,column = map(int,input().split()) arr = [] for i in range(rows): arr.append(list(input())) string = input() last = string[-1] operation = Find(string,last) for i in string[0]+operation: if i == "L": arr = Left(arr) if i == "R": arr = Right(arr) if i == "U": arr = Transpose(arr) arr = Left(arr) arr = Transpose(arr) if i == "D": arr = Transpose(arr) arr = Right(arr) arr = Transpose(arr) for i in arr: print(i) def Left(arr): for i in range(len(arr)): ans = arr[i].count("1") arr[i] = "1"*ans + (len(arr[i]) - ans)*"0" return arr def Right(arr): for i in range(len(arr)): ans = arr[i].count("1") arr[i] = (len(arr[i]) - ans)*"0"+"1"*ans return arr def Transpose(arr): ansss = [] ans = list(map(list, zip(*arr))) for i in ans: ass = i hello = "" for j in ass: hello += j ansss.append(hello) return ansss def Find(string,last): for i in string[-2::-1]: if last == "L": if i in ["D","U"]: last = i + last break if last == "R": if i in ["D","U"]: last = i + last break if last == "D": if i in ["L","R"]: last = i + last break if last == "U": if i in ["L","R"]: last = i + last break return last def __starting_point(): main() __starting_point() ```
{ "language": "python", "test_cases": [ { "input": "3\n4 4\n1010\n0010\n1001\n0100\nLRDU\n4 3\n000\n010\n001\n101\nLRL\n3 2\n01\n10\n00\nD\n", "output": "0011\n0011\n0001\n0001\n000\n100\n100\n110\n00\n00\n11\n", "type": "stdin_stdout" } ] }
{ "difficulty": "interview", "problem_url": "https://www.codechef.com/problems/FRCPRT" }
vfc_1438
apps
verifiable_code
650
Solve the following coding problem using the programming language python: Chef is the event manager of his college. He has been assigned the task to manage the upcoming tech fest. There are $K$ rooms where the event can take place, and at a particular time only one event can be organized in a room for a particular time interval. Each event coordinator has their strictly preferred room $P_i$, and if the room is already occupied he simply cancels that event.Chef wants to maximize the total number of events,and so he allows or disallows certain events in order to achieve the task . Chef is busy handling his events so the chef needs your help . Given a list of $N$ events with their start time $S_i$,end time $E_i$ and preferred room $P_i$,you need to calculate the maximum number of events that can take place. Note that the $i$th event wants to occupy the $p_i$ room from [$s_i$, $f_i$) . -----Input:----- The first line contains an integer $T$ denoting the number of test cases . Each of the next $T$ lines contains two integers $N$ and $K$ , the number of events and the number of rooms respectively . Each of the next $N$ lines contains three integers $s_i$ ,$e_i$ and $p_i$,the start time ,end time and the preferred room of ith event. -----Output:----- Print the maximum number of events that can take place. -----Constraints----- - $1 \leq T \leq 100$ - $1 \leq N \leq 10^3$ - $1 \leq K \leq 10^5$ - $1 \leq Si < Ei \leq 10^9$ - $1 \leq Pi \leq K$ -----Sample Input:----- 1 4 2 1 10 1 10 20 2 15 50 2 20 30 2 -----Sample Output:----- 3 -----EXPLANATION:----- Chef can allow events 1st ,2nd and 4th,to get the maximum 3. The input will be stdin and you should print your solution to stdout Now solve the problem and return the code.
```python import sys # import math from math import gcd # import re # from heapq import * # from collections import defaultdict as dd # from collections import OrderedDict as odict # from collections import Counter as cc # from collections import deque # sys.setrecursionlimit(10**5)#thsis is must # mod = 10**9+7; md = 998244353 # m = 2**32 input = lambda: sys.stdin.readline().strip() inp = lambda: list(map(int,sys.stdin.readline().strip().split())) # def C(n,r,mod): # if r>n: # return 0 # num = den = 1 # for i in range(r): # num = (num*(n-i))%mod # den = (den*(i+1))%mod # return (num*pow(den,mod-2,mod))%mod # M = 1000000+1 # pfc = [i for i in range(M+1)] # def pfcs(M): # for i in range(2,M+1): # if pfc[i]==i: # for j in range(i+i,M+1,i): # if pfc[j]==j: # pfc[j] = i # return #______________________________________________________ for _ in range(int(input())): n,k = map(int,input().split()) d = [[] for i in range(k+1)] for i in range(n): l,r,p = map(int,input().split()) d[p].append([l,r]) ans = 0 for i in d: if len(i)==0: continue ans+=1 t = sorted(i,key = lambda x:(x[1],x[0])) final = t[0][1] for j in range(1,len(t)): if t[j][0]>=final: ans+=1 final = t[j][1] print(ans) ```
{ "language": "python", "test_cases": [ { "input": "1\n4 2\n1 10 1\n10 20 2\n15 50 2\n20 30 2\n", "output": "3\n", "type": "stdin_stdout" } ] }
{ "difficulty": "interview", "problem_url": "https://www.codechef.com/ENNO2020/problems/ENCNOV4" }
vfc_1442
apps
verifiable_code
651
Solve the following coding problem using the programming language python: In these quarantine days, Chef and Chefina are getting bored. So, Chef came up with a game for her. He gets a pack of cards with numbers written on them. Chef then asks her to remove cards from the pack in the following manner: Chefina can choose any 3 cards at a time, having unique values, and remove the smallest and largest of them, and put back the middle one. For example, say Chefina chooses 3 cards that have numbers $x$, $y$, $z$ on them, such that $x <= y <= z$. Then she can throw away cards with number $x$ and $z$, but has to put the card with number $y$ on it back into the pack. Chefina can repeat this process any number of times. As soon as the pack contains cards with unique numbers, the game ends. If Chefina can determine the count of cards that will remain in the end, and tell it to Chef beforehand, she wins the game. Chefina asks for your help to win this game. Given the number written on the cards, help her find the count of cards in the pack when she wins. $Note:$ You need to maximize the array length or the number of unique elements -----Input:----- - The first line of the input consists of a single integer $T$, denoting the number of test cases. Description of $T$ test cases follow. - The first line of each test case consists of a single integer $N$, denoting the number of cards in the pack - The next line consists of $N$ space separated numbers $A1$, $A2$ … $An$. For each valid $i (1 <= i <= N)$, the $i$-th card has the number $Ai$ written on it. -----Output:----- - For each test case, print the count of the cards that remain in the end. -----Constraints----- - $1 \leq T \leq 500$ - $1 \leq N \leq 10^6$ - $1 \leq Ai \leq N$ -----Subtasks----- - 30 points : $1 \leq T \leq 20$; $ 1 \leq N \leq 5*10^5$ - 70 points : Original constraints -----Sample Input:----- 2 5 1 2 2 3 5 9 1 2 2 3 3 5 8 8 9 -----Sample Output:----- 3 5 -----EXPLANATION:----- Test case 1: Chefina chooses the cards with number: 2, 3, 5, throws away 2 & 5, and puts back 3. So, the pack now contains cards with numbers: 1, 2, 3. Since the pack contains cards with unique numbers only, these are the 3 final cards. Test case 2: Chefina chooses the cards with number: 2, 3, 8, throws away 2 & 8, and puts back 3. Now the pack contains cards with numbers: 1, 2, 3, 3, 5, 8, 9. Next, she chooses cards with number: 3, 5, 8 throws away 3 & 8, and puts back 5. Now the pack contains cards with number: 1, 2, 3, 5, 9. Since the pack contains cards with unique numbers only, these are the 5 final cards. Note: There might be multiple options to choose the 3 cards from the pack in any turn The input will be stdin and you should print your solution to stdout Now solve the problem and return the code.
```python # cook your dish here try: for i in range(int(input())): n=int(input()) l=[int(j) for j in input().split()][:n] d={} for j in l: d[j]=d.get(j,0)+1 a=len(d) c=0 for j in list(d.keys()): while(d[j]>=3): d[j]=(d[j]//3)+(d[j]%3) if(d[j]==2): c=c+1 if(c&1): s=0 for j in list(d.values()): s=s+j print(s-c-1) else: s=0 for j in list(d.values()): s=s+j print(s-c) except: pass ```
{ "language": "python", "test_cases": [ { "input": "2\n5\n1 2 2 3 5\n9\n1 2 2 3 3 5 8 8 9\n", "output": "3\n5\n", "type": "stdin_stdout" } ] }
{ "difficulty": "interview", "problem_url": "https://www.codechef.com/BTCH2020/problems/UNQCARD" }
vfc_1446
apps
verifiable_code
652
Solve the following coding problem using the programming language python: Shubham recently learned the lexicographical order in strings. Now, he has two strings s1 and s2 of the equal size and Shubham wants to compare those two strings lexicographically. Help Shubham with the strings comparison. Note: Letters are case insensitive. -----Input----- First line contains a integer T denoting the number of test cases. Each test case contains two strings of equal size in two separate lines. -----Output----- For each test case, If s1 < s2, print "first". If s1 > s2, print "second". If s1=s2, print "equal". in separate lines. -----Constraints----- - 1 ≤ T ≤ 10^2 - 1 ≤ Length of the string ≤ 500 -----Example----- Input: 2 abc acb AB ba Output: first first The input will be stdin and you should print your solution to stdout Now solve the problem and return the code.
```python t=eval(input()) while t: t=t-1 s1=input().lower() s2=input().lower() res="equal" for i in range(len(s1)): if(s1[i]!=s2[i]): res="first" if s1[i]<s2[i] else "second" break print(res) ```
{ "language": "python", "test_cases": [ { "input": "2\nabc\nacb\nAB\nba\n", "output": "first\nfirst\n", "type": "stdin_stdout" } ] }
{ "difficulty": "interview", "problem_url": "https://www.codechef.com/CDFXOQ16/problems/CDFX01" }
vfc_1450
apps
verifiable_code
653
Solve the following coding problem using the programming language python: You have a laser with P amount of energy in it. You are playing a game where you have to use the laser to destroy some crystals, each with some health of their own. Initially, you have 0 points. Each crystal has an associated health. The health of the ith crystal is given by health[i]. You can perform one of the two actions: - At the cost of health[i] energy, you can destroy the ith crystal and gain 1 point. You can only perform this action if your laser has atleast health[i] energy in it. - At the cost of 1 point, you can destroy the ith crystal and refuel the laser's energy by an amount equal to health[i]. This action can only be performed if you have atleast one point. Note: Each crystal can only be destroyed once. Determine the maximum number of points you can obtain by destroying any number of crystals and performing either action as you wish. -----Input:----- - First line will contain n, number of crystals. - Second line will contain space separated integers, health of each crystal. - Third line will contain an integer P, initial energy of the laser. -----Output:----- Print the largest number of points we can have after destroying any number of crystals. -----Constraints----- - health.length <= 1000 - 0 <= health[i] < 10000 - 0 <= P < 10000 -----Subtasks----- - 40 points : 1 <= health.length <= 100 - 60 points : health.length > 100 -----Sample Input 1:----- 1 200 100 -----Sample Output 1:----- 0 -----Explanation:----- The laser initially has only 100 energy. Since the only crystal requires 200 energy to destroy, we cannot perform any action here, and the game ends. The number of points is zero. -----Sample Input 2:----- 2 100 200 150 -----Sample Output 2:----- 1 -----Explanation:----- The laser has 150 energy. We can consume 100 energy and destroy the first crystal and gain 1 point. The remaining energy is 50, which is not enough to destroy the other crystal. We end the game here as we have the maximum possible points attainable. The input will be stdin and you should print your solution to stdout Now solve the problem and return the code.
```python def game(n,l,p): if(len(l)==0): return 0 l.sort() if(len(l)>=1 and p<l[0]): return 0 l.sort() c=0 ma=set() ma.add(0) while(len(l)): if(p>=l[0]): p-=l[0] c+=1 ma.add(c) l=l[1:] else: if(c>0): c-=1 ma.add(c) p+=l[-1] l=l[:-1] else: return max(ma) return max(ma) n=int(input()) l=list(map(int,input().split())) p=int(input()) print(game(n,l,p)) ```
{ "language": "python", "test_cases": [ { "input": "1\n200\n100\n", "output": "0\n", "type": "stdin_stdout" } ] }
{ "difficulty": "interview", "problem_url": "https://www.codechef.com/COFJ2020/problems/JUN1" }
vfc_1454
apps
verifiable_code
654
Solve the following coding problem using the programming language python: Three numbers A, B and C are the inputs. Write a program to find second largest among them. -----Input----- The first line contains an integer T, the total number of testcases. Then T lines follow, each line contains three integers A, B and C. -----Output----- For each test case, display the second largest among A, B and C, in a new line. -----Constraints----- - 1 ≤ T ≤ 1000 - 1 ≤ A,B,C ≤ 1000000 -----Example----- Input 3 120 11 400 10213 312 10 10 3 450 Output 120 312 10 The input will be stdin and you should print your solution to stdout Now solve the problem and return the code.
```python # cook your dish here x=int(input()) for i in range(x): s=list(map(int,input().split())) s.sort() print(s[1]) ```
{ "language": "python", "test_cases": [ { "input": "3\n120 11 400\n10213 312 10\n10 3 450\n", "output": "120\n312\n10\n", "type": "stdin_stdout" } ] }
{ "difficulty": "interview", "problem_url": "https://www.codechef.com/problems/FLOW017" }
vfc_1458
apps
verifiable_code
655
Solve the following coding problem using the programming language python: Chef had a sequence of positive integers with length $N + K$. He managed to calculate the arithmetic average of all elements of this sequence (let's denote it by $V$), but then, his little brother deleted $K$ elements from it. All deleted elements had the same value. Chef still knows the remaining $N$ elements — a sequence $A_1, A_2, \ldots, A_N$. Help him with restoring the original sequence by finding the value of the deleted elements or deciding that there is some mistake and the described scenario is impossible. Note that the if it is possible for the deleted elements to have the same value, then it can be proven that it is unique. Also note that this value must be a positive integer. -----Input----- - 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 line of each test case contains three space-separated integers $N$, $K$ and $V$. - The second line contains $N$ space-separated integers $A_1, A_2, \ldots, A_N$. -----Output----- For each test case, print a single line containing one integer — the value of the deleted elements, or $-1$ if there is a mistake. -----Constraints----- - $1 \le T \le 100$ - $1 \le N, K \le 100$ - $1 \le V \le 10^5$ - $1 \le A_i \le 10^5$ for each valid $i$ -----Subtasks----- Subtask #1 (100 points): original constraints -----Example Input----- 3 3 3 4 2 7 3 3 1 4 7 6 5 3 3 4 2 8 3 -----Example Output----- 4 -1 -1 The input will be stdin and you should print your solution to stdout Now solve the problem and return the code.
```python def __starting_point(): t=int(input()) for _ in range(t): n,k,v=map(int,input().split()) li=list(map(int,input().split())) sumn=0 for i in range(n): sumn=sumn+li[i] sumk=v*(n+k)-sumn e=int(sumk/k) r=sumk%k if e<=0: print(-1) elif r!=0: print(-1) else: print(e) __starting_point() ```
{ "language": "python", "test_cases": [ { "input": "3\n3 3 4\n2 7 3\n3 1 4\n7 6 5\n3 3 4\n2 8 3\n", "output": "4\n-1\n-1\n", "type": "stdin_stdout" } ] }
{ "difficulty": "interview", "problem_url": "https://www.codechef.com/problems/AVG" }
vfc_1462
apps
verifiable_code
656
Solve the following coding problem using the programming language python: Write a program to obtain a number $N$ and increment its value by 1 if the number is divisible by 4 $otherwise$ decrement its value by 1. -----Input:----- - First line will contain a number $N$. -----Output:----- Output a single line, the new value of the number. -----Constraints----- - $0 \leq N \leq 1000$ -----Sample Input:----- 5 -----Sample Output:----- 4 -----EXPLANATION:----- Since 5 is not divisible by 4 hence, its value is decreased by 1. The input will be stdin and you should print your solution to stdout Now solve the problem and return the code.
```python # cook your dish here n = int(input()) if(n%4==0): print(n+1) else: print(n-1) ```
{ "language": "python", "test_cases": [ { "input": "5\n", "output": "4\n", "type": "stdin_stdout" } ] }
{ "difficulty": "interview", "problem_url": "https://www.codechef.com/problems/DECINC" }
vfc_1466
apps
verifiable_code
657
Solve the following coding problem using the programming language python: You will be given a two-dimensional array with row consisting values 0 or 1. A move consists of choosing any column or row, and toggling all the 0’s as 1’s and 1’s as 0’s. After making the required moves, every row represents a binary number and the score of the matrix will be sum of all the numbers represented as binary numbers in each row. Find the highest possible score. $Example:$ Input: 0 0 1 1 1 0 1 0 1 1 0 0 Output: 39 Explanation: Toggled to 1 1 1 1 1 0 0 1 1 1 1 1 0b1111 + 0b1001 + 0b1111 = 15 + 9 + 15 = 39 -----Input:----- - First line will contains $m$, $n$ for the size of the 2-D array. - Contains $m$ lines of $n$ space-separated values each. -----Output:----- Single integer which is the maximum score obtained by the sum of binary numbers. -----Constraints----- - $1 \leq m, n \leq 20$ - $A[i][j] = 1$ or $0$ -----Sample Input:----- 3 4 0 0 1 1 1 0 1 0 1 1 0 0 -----Sample Output:----- 39 The input will be stdin and you should print your solution to stdout Now solve the problem and return the code.
```python def matrixScore(A): """ :type A: List[List[int]] :rtype: int """ m,n = len(A),len(A[0]) # 行变换 for i in range(m): if A[i][0] == 1: continue for j in range(n): A[i][j] = 1 - A[i][j] # 列变换 res = 0 for rows in zip(*A): # 始终使1的个数是更大的 cnt1 = max(rows.count(1), rows.count(0)) res += cnt1 * 2**(n-1) n -= 1 return res m, n = [int(s) for s in input().split(" ")] arr = [[int(s) for s in input().split(" ")] for i in range(m)] ans = matrixScore(arr) print(ans) ```
{ "language": "python", "test_cases": [ { "input": "3 4\n0 0 1 1\n1 0 1 0\n1 1 0 0\n", "output": "39\n", "type": "stdin_stdout" } ] }
{ "difficulty": "interview", "problem_url": "https://www.codechef.com/COFDEC20/problems/COFDQ2" }
vfc_1470
apps
verifiable_code
658
Solve the following coding problem using the programming language python: A sequence of integers ($a_1, a_2, \ldots, a_k$) is said to be UpDown, if these inequalities hold true: - $a_1 \le a_2$ - $a_2 \ge a_3$ - $a_3 \le a_4$ and so on. That is, every even-indexed element should be at least as large as its adjacent elements. And every odd-indexed element should be at most as large as its adjacent elements. Formally, $a_{2i} \ge a_{2i+1}$ and $a_{2i+1} \le a_{2i+2}$, for all valid positions. A subsegment is a consecutive portion of a sequence. That is, a subsegment of ($b_1, b_2, \ldots, b_k$) will be of the form ($b_i, b_{i+1}, \ldots, b_j$), for some $i$ and $j$. You are given a sequence ($s_1, s_2, \ldots, s_n$). You can insert at most one integer anywhere in this sequence. It could be any integer. After inserting an integer (or choosing not to), suppose you have the new sequence ($t_1, t_2, \ldots, t_m$). Note that $m$ will either be $n$+1 or $n$. You want to maximize the length of the longest subsegment of ($t_1, t_2, \ldots, t_m$) which is UpDown, and output the length of that. -----Input----- - The first line contains a single integer, $T$, which is the number of testcases. The description of each testcase follows. - The first line of every testcase contains a single integer, $n$, which is the number of integers in the original sequence. - The second line contains $n$ integers: $s_1, s_2, \ldots, s_n$, which forms the original sequence. -----Output----- For each testcase output a single line containing one integer, which should be the length of the longest UpDown subsegment that you can get after inserting at most one integer. -----Constraints----- - $1 \le T \le 2$ - $1 \le n \le 10^6$ - $1 \le s_i \le 10^9$ -----Subtasks----- Subtask #1 (20 points): $1 \le n \le 100$ Subtask #2 (10 points): $1 \le n \le 10000$ Subtask #3 (70 points): Original constraints -----Sample Input----- 2 7 100 1 10 3 20 25 24 5 3 3 2 4 1 -----Sample Output----- 7 6 -----Explanation----- Testcase 1: The original sequence is (100, 1, 10, 3, 20, 25, 24). Suppose you insert the element 5 between the elements 20 and 25, you will get the new sequence (100, 1, 10, 3, 20, 5, 25, 24). The longest UpDown subsegment of this sequence is (1, 10, 3, 20, 5, 25, 24), whose length is 7. You can check that you cannot do any better, and hence the answer is 7. Testcase 2: The original sequence is (3, 3, 2, 4, 1). Suppose you insert the element 4 at the end, you will get the new sequence (3, 3, 2, 4, 1, 4). This entire sequence is UpDown, and so the longest UpDown subsegment of this sequence is (3, 3, 2, 4, 1, 4), whose length is 6. You can check that you cannot do any better, and hence the answer is 6. The input will be stdin and you should print your solution to stdout Now solve the problem and return the code.
```python # cook your dish here t=int(input()) for _ in range(t): n=int(input()) array=list(map(int, input().split())) list_sub=[] idx=0 counter=0 for i in range(n-1): if counter%2==0 and array[i]<=array[i+1]: counter+=1 elif counter%2==1 and array[i]>=array[i+1]: counter+=1 else: list_sub.append((idx,i)) if counter%2==1: idx=i counter=1 else: idx=i+1 counter=0 list_sub.append((idx, n-1)) massimo=0 if len(list_sub)==1: massimo=list_sub[0][1]-list_sub[0][0]+2 for i in range(len(list_sub)-1): if list_sub[i][1]==list_sub[i+1][0]: massimo=max(massimo, list_sub[i][1]-list_sub[i][0]+2+list_sub[i+1][1]-list_sub[i+1][0]) else: massimo=max(massimo, list_sub[i][1]-list_sub[i][0]+3+list_sub[i+1][1]-list_sub[i+1][0]) print(massimo) ```
{ "language": "python", "test_cases": [ { "input": "2\n7\n100 1 10 3 20 25 24\n5\n3 3 2 4 1\n", "output": "7\n6\n", "type": "stdin_stdout" } ] }
{ "difficulty": "interview", "problem_url": "https://www.codechef.com/ZCOPRAC/problems/UPDOWSEQ" }
vfc_1474
apps
verifiable_code
659
Solve the following coding problem using the programming language python: Binod is a youtuber and he is busy in the fame of social media so he asked you to help him solve a problem. You have been given an array of $positive$ $integers$ $a_{1},a_{2},a_{3},...,a_{i},...,a_{n}$ of size n.You have to find the smallest length of the subarray such that the length of the subarray must be $strictly$ greater than k and it's sum also must be $strictly$ greater than s. -----Input Format :------ - The first line of input contains three space-separated integers n, k and s - The second line contains n space-separated integers,describing the array a -----Output Format:----- - Print a single integer :- The smallest length of subarray if exists, Otherwise print "-1" (without quotes) -----Constraints:------ - $1 \leq n, k \leq 10^{6}$ - $1 \leq a_{1},a_{2},a_{3},...,a_{i},...,a_{n}\leq 10^{9}$ - $1 \leq s \leq 10^{15}$ Subtask #1 (30 points): - $1 \leq n, k \leq 10^{3}$ Subtask #2 (70 points): $Original$ $Constraints$ -----Sample Test Cases:------ -----Example 1:----- 5 1 5 1 2 3 4 5 -----Output :----- 2 -----Explanation----- $\textbf{There are two possibles answers} :$ - Index starts at 3 and ends at 4 have a sum of 7 which is strictly greater than 5 and has a length of subarray greater than 1. - Index starts at 4 and ends at 5 have a sum of 9 which is strictly greater than 5 and has a length of subarray greater than 1. Any of the possible scenarios gives the same answer. -----Example 2:----- 3 2 1 9 9 1 -----Output :----- 3 -----Explanation :----- - Each value in array index satisfies the condition sum greater than 1 but to satisfy the condition of length greater than 2 choose the subarray of length 3 The input will be stdin and you should print your solution to stdout Now solve the problem and return the code.
```python #binarr def binarr(a, k, s): a.sort(reverse=True) arr = [0]*k for i in range(k): arr[i] = a[i] if sum(arr) <= s: return binarr(a, k+1, s) return len(arr) try: n, k, s = list(map(int, input().split())) a = list(map(int, input().split())) print(binarr(a, k+1, s)) except Exception: pass ```
{ "language": "python", "test_cases": [ { "input": "1:\n5 1 5\n1 2 3 4 5\n", "output": "2\n", "type": "stdin_stdout" } ] }
{ "difficulty": "interview", "problem_url": "https://www.codechef.com/COVO2020/problems/BINARR" }
vfc_1478
apps
verifiable_code
660
Solve the following coding problem using the programming language python: The chef was busy in solving algebra, he found some interesting results, that there are many numbers which can be formed by the sum of the factorial of the digits, he wrote all those interesting numbers in the diary(in increasing order) and went to sleep. Cheffina came and stole his diary, in morning chef found that his diary is missing. Now the chef wants your help to find those numbers, Chef asks you whether N is that interesting number or not. If N is an interesting number then print 1. Else print 0. -----Input:----- - First-line will contain $T$, the number of test cases. Then the test cases follow. - Each test case contains a single line of input, $N$. -----Output:----- For each test case, output in a single line answer 1 or 0. -----Constraints----- - $1 \leq T \leq 10^6$ - $0 \leq N \leq 10^9$ -----Sample Input:----- 2 2 10 -----Sample Output:----- 1 0 -----EXPLANATION:----- For 1) Factorial of 2 is 2, hence it is an interesting number. For 2) conversion for 10 is 1! + 0! = 2, which is not equal to 10, hence not an interesting number. The input will be stdin and you should print your solution to stdout Now solve the problem and return the code.
```python t = int(input()) for i in range(t): n = int(input()) if n == 1 or n == 2 or n == 145 or n == 40585: print(1) else: print(0) ```
{ "language": "python", "test_cases": [ { "input": "2\n2\n10\n", "output": "1\n0\n", "type": "stdin_stdout" } ] }
{ "difficulty": "interview", "problem_url": "https://www.codechef.com/PBK22020/problems/ITGUY26" }
vfc_1482
apps
verifiable_code
661
Solve the following coding problem using the programming language python: Raju has created a program to find the square root of a number. But his program can store only integers. Being a newbie, he didn't know about rounding the numbers. Hence his program returns the absolute value of the result if possible. For example, sqrt(3) = 1.73205080757……. His program will return 1 Given a number $N$, and it's integral square root $S$, His instructor will consider the answer correct if Difference between $N$ and the square of $S$ is within less than or equal to $X$% of $N$. -----Input:----- - First line contains $T$ no. of test cases and $X$ separated by space - For every test case, a line contains an integer $N$ -----Output:----- For every test case, print yes if his programs return square root and (N-(S^2)) <= 0.01XN . For everything else, print no on a new line -----Constraints----- 10 points: - $1 \leq T \leq 10$ - $0\leq N \leq 10$ 20 points: - $1 \leq T \leq 30000$ - $-10^9 \leq N \leq 10^9$ 70 points: - $1 \leq T \leq 10^6$ - $-10^9 \leq N \leq 10^9$ -----Sample Input:----- 2 20 5 3 -----Sample Output:----- yes no -----EXPLANATION:----- In #1, sqrt(5) = 2.2360679775. Taking integral value, S = 2. S2 = 4. Difference=1 which is within 20% of 5 In #1, sqrt(3) = 1.73205080757. Taking integral value, S = 1. S2 = 1. Difference=2 which is not within 20% of 3 The input will be stdin and you should print your solution to stdout Now solve the problem and return the code.
```python try: from math import sqrt t,x=list(map(int,input().split())) for _ in range(t): n=int(input()) if(n<0): print("no") else: diff=(x/100)*n ans=int(sqrt(n)) ans1=ans**2 if(n-ans1<=diff): print("yes") else: print("no") except: pass ```
{ "language": "python", "test_cases": [ { "input": "2 20\n5\n3\n", "output": "yes\nno\n", "type": "stdin_stdout" } ] }
{ "difficulty": "interview", "problem_url": "https://www.codechef.com/COMT2020/problems/ROOTSQR" }
vfc_1486
apps
verifiable_code
662
Solve the following coding problem using the programming language python: Kabir wants to impress Tara by showing her his problem solving skills. He has decided to give the correct answer to the next question which will be asked by his Algorithms teacher. The question asked is: Find the sum of alternate consecutive d$d$ odd numbers from the range L$L$ to R$R$ inclusive. if d$d$ is 3 and L$L$ is 10 and R$R$ is 34, then the odd numbers between 10 and 34 are 11,13,15,17,19,21,23,25,27,29,31,33$11,13,15,17,19,21,23,25,27,29,31,33$, and the d$d$ alternate odd numbers are 11,13,15,23,25,27$11,13,15,23,25,27$. You are a friend of Kabir, help him solve the question. Note:$Note:$ Number of odd number between L$L$ and R$R$ (both inclusive) is a multiple of d$d$. -----Input:----- - First line will contain T$T$, number of test cases. - First line of each test case contains one integer d$d$ . - Second line of each test case contains two space separated integer L$L$ and R$R$. -----Output:----- For each test case, print the sum modulo 1000000007. -----Constraints:----- - 1≤T≤106$1 \leq T \leq 10^6$ - 1≤d≤103$1 \leq d \leq 10^3$ - 1≤L<R≤106$1 \leq L < R \leq 10^6$ -----Sample Input:----- 1 3 10 33 -----Sample Output:----- 114 -----EXPLANATION:----- Sum of alternate odd numbers i.e, 11,13,15,23,25,27 is 114 The input will be stdin and you should print your solution to stdout Now solve the problem and return the code.
```python # cook your dish here for t in range(int(input().strip())): d = int(input().strip()) L, R = map(int, input().strip().split(" ")) if L % 2 == 0: L += 1 sum = (((((R - L + 2)//2)//d)+1)//2) - 1 sum = (sum * 2 * d * (sum + 1) * d) + (sum+1) *d * (L + d -1) print(sum%1000000007) ```
{ "language": "python", "test_cases": [ { "input": "1\n3\n10 33\n", "output": "114\n", "type": "stdin_stdout" } ] }
{ "difficulty": "interview", "problem_url": "https://www.codechef.com/HECS2020/problems/CC002" }
vfc_1490
apps
verifiable_code
663
Solve the following coding problem using the programming language python: You are given a string $S$ and an integer $L$. A operation is described as :- "You are allowed to pick any substring from first $L$ charcaters of $S$, and place it at the end of the string $S$. A string $A$ is a substring of an string $B$ if $A$ can be obtained from $B$ by deletion of several (possibly, zero or all) characters from the beginning and several (possibly, zero or all) elements from the end. Find the lexographically smallest string after performing this opertaion any number of times (possibly zero). For example $S$ = "codechef" and $L=4$. Then, we can take substring "ode" from S[0-3] and place it at end of the string $S$ = "cchefode". -----Input:----- - First line will contain $T$, number of testcases. - Then each of the N lines contain an integer $L$ and a string $S$. -----Output:----- For each testcase, output in a single line answer lexographically smallest string. -----Constraints----- - $1 \leq T \leq 10^4$ - $2 \leq |S| \leq 10^3$ - $1 \leq L \leq N $ -----Sample Input:----- 2 1 rga 2 cab -----Sample Output:----- arg abc -----EXPLANATION:----- In the first testcase: substring 'r' is picked and placed at the end of the string. rga -> gar Then performing same operation gives :- gar -> arg The input will be stdin and you should print your solution to stdout Now solve the problem and return the code.
```python def least_rotation(S: str) -> int: """Booth's algorithm.""" f = [-1] * len(S) # Failure function k = 0 # Least rotation of string found so far for j in range(1, len(S)): sj = S[j] i = f[j - k - 1] while i != -1 and sj != S[k + i + 1]: if sj < S[k + i + 1]: k = j - i - 1 i = f[i] if sj != S[k + i + 1]: # if sj != S[k+i+1], then i == -1 if sj < S[k]: # k+i+1 = k k = j f[j - k] = -1 else: f[j - k] = i + 1 return k for _ in range(int(input())): l, s = input().split() if int(l) == 1: l = len(s) s += s k = least_rotation(s) print(s[k:k+l]) else: print(''.join(sorted(s))) ```
{ "language": "python", "test_cases": [ { "input": "2\n1 rga\n2 cab\n", "output": "arg\nabc\n", "type": "stdin_stdout" } ] }
{ "difficulty": "interview", "problem_url": "https://www.codechef.com/BIT32020/problems/BIT3B" }
vfc_1494
apps
verifiable_code
664
Solve the following coding problem using the programming language python: This year $p$ footballers and $q$ cricketers have been invited to participate in IPL (Indian Programming League) as guests. You have to accommodate them in $r$ rooms such that- - No room may remain empty. - A room may contain either only footballers or only cricketers, not both. - No cricketers are allowed to stay alone in a room. Find the number of ways to place the players. Note though, that all the rooms are identical. But each of the cricketers and footballers are unique. Since the number of ways can be very large, print the answer modulo $998,244,353$. -----Input----- - 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 three space-separated integers $p$, $q$ and $r$ denoting the number of footballers, cricketers and rooms. -----Output----- For each test case, output the number of ways to place the players modulo $998,244,353$. -----Constraints----- - $1 \le T \le 100$ - $1 \le p, q, r \le 100$ -----Example Input----- 4 2 1 4 2 4 4 2 5 4 2 8 4 -----Example Output----- 0 3 10 609 -----Explanation----- Example case 2: Three possible ways are: - {Footballer 1}, {Footballer 2}, {Cricketer 1, Cricketer 2}, {Cricketer 3, Cricketer 4} - {Footballer 1}, {Footballer 2}, {Cricketer 1, Cricketer 3}, {Cricketer 2, Cricketer 4} - {Footballer 1}, {Footballer 2}, {Cricketer 1, Cricketer 4}, {Cricketer 2, Cricketer 3} Please note that the rooms are identical. The input will be stdin and you should print your solution to stdout Now solve the problem and return the code.
```python # cook your dish here MOD = 998244353 fball = [ [0]*101 for _ in range(101) ] cric = [ [0]*101 for _ in range(101) ] def calSNum(n, r): if n == r or r == 1: fball[r][n] = 1 return if n > 0 and r > 0 and n > r: fball[r][n] = (fball[r-1][n-1]%MOD + (r*fball[r][n-1])%MOD )%MOD return fball[r][n] = 0 def calASNum(n, r): if n == 0 and r == 0 : cric[r][n] = 0 return if n >= 2 and r == 1: cric[r][n] = 1 return if r > 0 and n > 0 and n >= 2*r: cric[r][n] = ((r*cric[r][n-1])%MOD + ((n-1)*cric[r-1][n-2])%MOD )%MOD return cric[r][n] = 0 def preCompute(): for r in range(1,101): for n in range(1, 101): calSNum(n, r) calASNum(n, r) def main(): preCompute() for _ in range(int(input())): f, c, r = list(map(int, input().split())) ans = 0 if f + (c//2) >= r: minv = min(f, r) for i in range(1, minv+1): if r-i <= c//2: ans = (ans + (fball[i][f] * cric[r-i][c])%MOD )%MOD print(ans) def __starting_point(): main() __starting_point() ```
{ "language": "python", "test_cases": [ { "input": "4\n2 1 4\n2 4 4\n2 5 4\n2 8 4\n", "output": "0\n3\n10\n609\n", "type": "stdin_stdout" } ] }
{ "difficulty": "interview", "problem_url": "https://www.codechef.com/problems/FCIPL" }
vfc_1498
apps
verifiable_code
665
Solve the following coding problem using the programming language python: Chef organised a chess tournament, which spanned over $M$ months. There were $N$ players, and player $i$ was rated $R_i$ before the start of the tournament. To see the progress of the players, he noted their rating changes at the end of each month. After the tournament, FIDE asked Chef to find the number of players whose peak rating and peak ranking did not occur in the same month. In other words, Chef was asked to find the ratings and ranking of each player after each of the $M$ months. Then, using this data, he should find the number of players, such that the month in which they achieved their highest rating over all the months, was different from the month in which they achieved their best rank (based on ratings), over all the months. Note that we do not consider the initial rating/ranking, but only the rating and rankings after each of the $M$ months. For a particular player, if there are multiple peak rating or peak ranking months, Chef was to consider the earliest of them. If multiple players had the same rating at the end of some month, they were to be given the same rank. For example, if there were $5$ players, and their ratings at the end of some month were $(2600$, $2590$, $2600$, $2600$ and $2590)$, players $1$, $3$ and $4$ were to be given the first rank, while players $2$ and $5$ should be given the fourth rank. As Chef hates statistics, he asks you, his friend, to help him find this. Can you help Chef? -----Input:----- - The first line contains an integer $T$, the number of test cases. - The first line of each test case contains two space-separated integers $N$ and $M$, the number of players and the number of months that the tournament spanned over. - The second line of each test case contains $N$ space-separated integers, $R_1, R_2, \ldots, R_N$ denoting the initial ratings of the players, i.e., their ratings before the start of the tournament. - The next $N$ lines each contain $M$ space-separated integers. The $j^{th}$ integer of the $i^{th}$ line, $C_{i,j}$ denotes the rating change of the $i^{th}$ player after the $j^{th}$ month. -----Output:----- For each test case, print the number of players whose peak ratings did not occur in the same month as their peak ranking, in a new line. -----Constraints----- - $1 \le T \le 10$ - $1 \le N,M \le 500$ - $1800 \le R_i \le 2800$ - $-20 \le C_{i,j} \le 20$ -----Subtasks----- - 30 points : $1 \leq N,M \leq 50$ - 70 points : Original constraints. -----Sample Input:----- 2 3 3 2500 2500 2520 10 -5 -20 10 15 20 -15 17 13 2 3 2125 2098 -20 10 -10 10 10 -20 -----Sample Output:----- 2 2 -----Explanation:----- Test case 1: - The ratings for player $1$ after each month are: $(2510$, $2505$ and $2485)$, while his rankings are first, third and third, respectively. Thus, his best rating and best ranking occur after the same month, i.e., after the first month. - The ratings for player $2$ after each month are: $(2510$, $2525$ and $2545)$, while his rankings are first, first and first, respectively. His best rating occurs after the third month, while his best ranking occurs after the first month (we consider the first month even though his peak ranking is over all the months, because we consider only the earliest month where he attains the peak ranking). - The ratings for player $3$ after each month are: $(2505$, $2522$ and $2535)$, while his rankings are third, second and second, respectively. His best rating occurs after the third month, while his best ranking occurs after the second month. So there are two players ($2$ and $3$), whose peak ratings did not occur in the same month as their peak ranking, and hence the answer is 2. Test case 2: - The ratings for player $1$ after each month are: $(2105$, $2115$ and $2105)$, while his rankings are second, second and first, respectively. Best rating is after second month, but best ranking is after third month. - The ratings for player $2$ after each month are: $(2108$, $2118$ and $2098)$, while his rankings are first, first and second, respectively. Best rating is after second month, but best ranking is after first month. So there are two players ($1$ and $2$), whose peak ratings did not occur in the same month as their peak ranking, and hence the answer is 2. The input will be stdin and you should print your solution to stdout Now solve the problem and return the code.
```python # cook your dish here t=int(input()) for _ in range(t): n,m=list(map(int,input().split())) r=list(map(int,input().split())) rating=[[r[i]]*(m) for i in range(n)] ranking=[[0]*m for i in range(n)] for i in range(n): diff=list(map(int,input().split())) for j in range(m): rating[i][j]+=diff[j] if j+1<m: rating[i][j+1]=rating[i][j] for i in range(m): rate=[[j,rating[j][i]] for j in range(n)] rate=sorted(rate,key=lambda x: x[1],reverse=True) c=1 gap=0 for j in range(n): if j>0 and rate[j-1][1]==rate[j][1]: gap+=1 if j>0 and rate[j-1][1]!=rate[j][1]: c+=1+gap gap=0 ranking[rate[j][0]][i]=c count=0 for i in range(n): rate=rating[i].copy() i1=rate.index(max(rate)) rank=ranking[i].copy() i2=rank.index(min(rank)) if i1!=i2: count+=1 print(count) ```
{ "language": "python", "test_cases": [ { "input": "2\n3 3\n2500 2500 2520\n10 -5 -20\n10 15 20\n-15 17 13\n2 3\n2125 2098\n-20 10 -10\n10 10 -20\n", "output": "2\n2\n", "type": "stdin_stdout" } ] }
{ "difficulty": "interview", "problem_url": "https://www.codechef.com/problems/ELOMAX" }
vfc_1502
apps
verifiable_code
666
Solve the following coding problem using the programming language python: The chef is trying to solve some pattern problems, Chef wants your help to code it. Chef has one number K to form a new pattern. Help the chef to code this pattern problem. -----Input:----- - First-line will contain $T$, the number of test cases. Then the test cases follow. - Each test case contains a single line of input, one integer $K$. -----Output:----- For each test case, output as the pattern. -----Constraints----- - $1 \leq T \leq 100$ - $1 \leq K \leq 100$ -----Sample Input:----- 4 1 2 3 4 -----Sample Output:----- 1 12 34 123 456 789 1234 5678 9101112 13141516 -----EXPLANATION:----- No need, else pattern can be decode easily. The input will be stdin and you should print your solution to stdout Now solve the problem and return the code.
```python # cook your dish here t = int(input()) for _ in range(t): s = '' n = int(input()) if n==1: print(1) continue for i in range(1, n+1): s = s + str(i) print(s) p = 1 for i in range(n-1): s = '' for j in range(n): s = s + str(p + n) p = p+1 print(s) ```
{ "language": "python", "test_cases": [ { "input": "4\n1\n2\n3\n4\n", "output": "1\n12\n34\n123\n456\n789\n1234\n5678\n9101112\n13141516\n", "type": "stdin_stdout" } ] }
{ "difficulty": "interview", "problem_url": "https://www.codechef.com/PTRN2021/problems/ITGUY49" }
vfc_1506
apps
verifiable_code
667
Solve the following coding problem using the programming language python: Diana is planning to make a very long journey. Her journey consists of $N$ bus routes, numbered from $1 to N$ in the order she must take them. The buses themselves are very fast but do not run often. The $i-th$ bus route only runs every $Xi$ days. More specifically, she can only take the $i-th$ bus on day $Xi, 2Xi, 3Xi$, and so on. Since the buses are very fast, she can take multiple buses on the same day. Diana must finish her journey by day D, but she would like to start the journey as late as possible. What is the latest day she could take the first bus, and still finish her journey by day $D$? It is guaranteed that it is possible for Diana to finish her journey by day $D$. -----Input:----- The first line of the input gives the number of test cases, $T$. $T$ test cases follow. Each test case begins with a line containing the two integers N and D. Then, another line follows containing $N$ integers, the $i-th$ one is $Xi$. -----Output:----- For each test case, output one line containing an integer $Y$, where $Y$ is the latest day she could take the first bus, and still finish her journey by day $D$. -----Constraints----- - $1 \leq T \leq 100$ - $1 \leq Xi \leq D.$ - $1 \leq N \leq 1000.$ It is guaranteed that it is possible for Diana to finish her journey by day $D$. -----Sample Input:----- 3 3 10 3 7 2 4 100 11 10 5 50 1 1 1 -----Sample Output:----- 6 99 1 -----EXPLANATION:----- In Sample $Case 1$, there are $N = 3$ bus routes and Bucket must arrive by day $D = 10$. She could: - Take the 1st bus on day 6 $(X1 = 3)$, - Take the 2nd bus on day 7 $(X2 = 7)$ and - Take the 3rd bus on day 8 $(X3 = 2)$. In Sample $Case 2$, there are $N = 4$ bus routes and Bucket must arrive by day $D = 100$. She could: - Take the 1st bus on day 99 $(X1 = 11)$, - Take the 2nd bus on day 100$ (X2 = 10)$, - Take the 3rd bus on day 100 $(X3 = 5)$ and - Take the 4th bus on day 100 $(X4 = 50)$, In Sample Case 3, there is $N = 1$ bus route, and Bucket must arrive by day $D = 1$. She could: - Take the 1st bus on day 1 $(X1 = 1)$. The input will be stdin and you should print your solution to stdout Now solve the problem and return the code.
```python t = int(input()) for _ in range(t): nd = list(map(int, input().split())) n = nd[0] d = nd[1] cutOff = [] x = d buses = list(map(int, input().split())) for i in range(len(buses)-1,-1,-1): x = x - x%buses[i] print(x) ```
{ "language": "python", "test_cases": [ { "input": "3\n3 10\n3 7 2\n4 100\n11 10 5 50\n1 1\n1\n", "output": "6\n99\n1\n", "type": "stdin_stdout" } ] }
{ "difficulty": "interview", "problem_url": "https://www.codechef.com/COX22020/problems/CCODEX2" }
vfc_1510
apps
verifiable_code
668
Solve the following coding problem using the programming language python: You are given an array A with size N (indexed from 0) and an integer K. Let's define another array B with size N · K as the array that's formed by concatenating K copies of array A. For example, if A = {1, 2} and K = 3, then B = {1, 2, 1, 2, 1, 2}. You have to find the maximum subarray sum of the array B. Fomally, you should compute the maximum value of Bi + Bi+1 + Bi+2 + ... + Bj, where 0 ≤ i ≤ j < N · K. -----Input----- - 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 line of each test case contains two space-separated integers N and K. - The second line contains N space-separated integers A0, A1, ..., AN-1. -----Output----- For each test case, print a single line containing the maximum subarray sum of B. -----Constraints----- - 1 ≤ T ≤ 10 - 1 ≤ N ≤ 105 - 1 ≤ K ≤ 105 - -106 ≤ Ai ≤ 106 for each valid i -----Subtasks----- Subtask #1 (18 points): N · K ≤ 105 Subtask #2 (82 points): original constraints -----Example----- Input: 2 2 3 1 2 3 2 1 -2 1 Output: 9 2 -----Explanation----- Example case 1: B = {1, 2, 1, 2, 1, 2} and the subarray with maximum sum is the whole {1, 2, 1, 2, 1, 2}. Hence, the answer is 9. Example case 2: B = {1, -2, 1, 1, -2, 1} and the subarray with maximum sum is {1, 1}. Hence, the answer is 2. The input will be stdin and you should print your solution to stdout Now solve the problem and return the code.
```python def max_sum(arr): # Finds the maximum sum of sub-arrays of arr max_till_now = -1000000 #minimum possible number current_sum = 0 for i in range(len(arr)): if current_sum < 0: # If sum of previous elements is negative, then ignore them. Start fresh # with `current_sum = 0` current_sum = 0 current_sum += arr[i] # Update max if max_till_now < current_sum: max_till_now = current_sum return max_till_now def solve(A, k): if k == 1: return max_sum(A) # Find sum of elements of A sum_A = 0 for i in range(len(A)): sum_A += A[i] Max_Suffix_Sum = -1000000 current = 0 for i in range(len(A)): current += A[-i-1] if current > Max_Suffix_Sum: Max_Suffix_Sum = current Max_Prefix_Sum = -1000000 current = 0 for i in range(len(A)): current += A[i] if current > Max_Prefix_Sum: Max_Prefix_Sum = current if sum_A <= 0: # Check two cases: # Case 1 : Check the max_sum of A case_1_max_sum = max_sum(A) # Case 2 : Check the max_sum of A + A case_2_max_sum = Max_Suffix_Sum + Max_Prefix_Sum # Return the maximum of the two cases return max([case_1_max_sum, case_2_max_sum]) else: # if sum_A > 0 #Check two cases: # Case 1 : Check the max_sum of A case_1_max_sum = max_sum(A) # Case 2 # Max sum = Max_Suffix_Sum + (k - 2)*sum_A + Max_Prefix_Sum case_2_max_sum = Max_Suffix_Sum + (k - 2)*sum_A + Max_Prefix_Sum # Return the maximum of the two cases return max([case_1_max_sum, case_2_max_sum]) # Main T = int(input()) # No of test cases for i in range(T): [N, k] = list(map(int, input().split(" "))) A = list(map(int, input().split(" "))) answer = solve(A,k) print(answer) ```
{ "language": "python", "test_cases": [ { "input": "2\n2 3\n1 2\n3 2\n1 -2 1\n\n\n", "output": "9\n2\n", "type": "stdin_stdout" } ] }
{ "difficulty": "interview", "problem_url": "https://www.codechef.com/problems/KCON" }
vfc_1514
apps
verifiable_code
669
Solve the following coding problem using the programming language python: Nadaca is a country with N$N$ cities. These cities are numbered 1$1$ through N$N$ and connected by M$M$ bidirectional roads. Each city can be reached from every other city using these roads. Initially, Ryan is in city 1$1$. At each of the following K$K$ seconds, he may move from his current city to an adjacent city (a city connected by a road to his current city) or stay at his current city. Ryan also has Q$Q$ conditions (a1,b1),(a2,b2),…,(aQ,bQ)$(a_1, b_1), (a_2, b_2), \ldots, (a_Q, b_Q)$ meaning that during this K$K$-second trip, for each valid i$i$, he wants to be in city ai$a_i$ after exactly bi$b_i$ seconds. Since you are very good with directions, Ryan asked you to tell him how many different trips he could make while satisfying all conditions. Compute this number modulo 109+7$10^9 + 7$. A trip is a sequence of Ryan's current cities after 1,2,…,K$1, 2, \ldots, K$ seconds. -----Input----- - The first line of the input contains a single integer T$T$ denoting the number of test cases. The description of T$T$ test cases follows. - The first line of each test case contains three space-separated integers N$N$, M$M$ and K$K$. - Each of the next M$M$ lines contains two space-separated integers u$u$ and v$v$ denoting a road between cities u$u$ and v$v$. - The next line contains a single integer Q$Q$. - Q$Q$ lines follow. For each i$i$ (1≤i≤Q$1 \le i \le Q$), the i$i$-th of these lines contains two space-separated integers ai$a_i$ and bi$b_i$. -----Output----- For each test case, print a single line containing one integer — the number of trips Ryan can make, modulo 109+7$10^9+7$. -----Constraints----- - 1≤T≤50$1 \le T \le 50$ - 1≤N,M,K,Q≤9,000$1 \le N, M, K, Q \le 9,000$ - 1≤ui,vi≤N$1 \le u_i, v_i \le N$ for each valid i$i$ - ui≠vi$u_i \neq v_i$ for each valid i$i$ - there is at most one road between each pair of cities - each city is reachable from every other city - 1≤ai≤N$1 \le a_i \le N$ for each valid i$i$ - 0≤bi≤K$0 \le b_i \le K$ for each valid i$i$ - the sum of N$N$ over all test cases does not exceed 9,000$9,000$ - the sum of K$K$ over all test cases does not exceed 9,000$9,000$ - the sum of M$M$ over all test cases does not exceed 9,000$9,000$ - the sum of Q$Q$ over all test cases does not exceed 9,000$9,000$ -----Subtasks----- Subtask #1 (20 points): - the sum of N$N$ over all test cases does not exceed 400$400$ - the sum of K$K$ over all test cases does not exceed 400$400$ - the sum of M$M$ over all test cases does not exceed 400$400$ - the sum of Q$Q$ over all test cases does not exceed 400$400$ Subtask #2 (80 points): original constraints -----Example Input----- 3 4 3 3 1 2 1 3 1 4 0 4 3 3 1 2 1 3 1 4 1 2 2 4 3 3 1 2 1 3 1 4 1 2 1 -----Example Output----- 28 4 6 The input will be stdin and you should print your solution to stdout Now solve the problem and return the code.
```python T = int(input()) for _ in range(T): N, M, K = [int(x) for x in input().split()] UV = [[int(x) for x in input().split()] for _ in range(M)] Q = int(input()) AB = [[int(x) for x in input().split()] for _ in range(Q)] X = [[i] for i in range(N)] for u, v in UV: X[u - 1] += [v - 1] X[v - 1] += [u - 1] A = [[1 if i > 0 or j == 0 else 0 for j in range(N)] for i in range(K + 1)] for a, b in AB: A[b] = [1 if i == a - 1 else 0 for i in range(N)] if A[0][0] == 1: for k in range(K - 1, -1, -1): for i in range(N): if A[k][i] != 0: A[k][i] = sum(A[k + 1][j] for j in X[i]) print(A[0][0]) ```
{ "language": "python", "test_cases": [ { "input": "3\n4 3 3\n1 2\n1 3\n1 4\n0\n4 3 3\n1 2\n1 3\n1 4\n1\n2 2\n4 3 3\n1 2\n1 3\n1 4\n1\n2 1\n", "output": "28\n4\n6\n", "type": "stdin_stdout" } ] }
{ "difficulty": "interview", "problem_url": "https://www.codechef.com/problems/WNDR" }
vfc_1518
apps
verifiable_code
670
Solve the following coding problem using the programming language python: Sereja has an array A of N positive integers : A[1], A[2], A[3], ... , A[N]. In a single operation on the array, he performs the following two steps : - Pick two indices i, j s.t. A[i] > A[j] - A[i] -= A[j] Sereja can apply these operations any number of times (possibly zero), such that the sum of resulting elements of the array is as small as possible. Help Sereja find this minimum sum. -----Input----- First line of input contains an integer T - the number of test cases. T test cases follow. First line of each test case contains the integer N. The next line contains N integers — A[1], A[2], A[3], ... , A[N]. -----Output----- For each test case, output a single line with the answer. -----Constraints----- - 1 ≤ T ≤ 10 - 1 ≤ N ≤ 105 - 1 ≤ A[i] ≤ 109 -----Example----- Input: 2 1 1 3 2 4 6 Output: 1 6 -----Explanation----- Example case 2. In this case, one possible way in which Sereja can perform the operations could be as follows. - Pick i = 2, j = 1. A[2] -= A[1]. Now the resulting array would be [2, 2, 6]. - Pick i = 3, j = 2. A[3] -= A[2]. Now the resulting array would be [2, 2, 4]. - Pick i = 3, j = 2. A[3] -= A[2]. Now the resulting array would be [2, 2, 2]. As the resulting array is [2 2 2], so the sum is 6. The input will be stdin and you should print your solution to stdout Now solve the problem and return the code.
```python def gcd(a,b): if b==0: return a else: return gcd(b,a%b) def main(): t=int(input()) while t!=0: t=t-1 n=int(input()) if n==1: print(input()) else: a=list(map(int,input().split(" "))) p=a[0] for i in range(1,n): p=gcd(p,a[i]) if p==1: break print(n*p) def __starting_point(): main() __starting_point() ```
{ "language": "python", "test_cases": [ { "input": "2\n1\n1\n3\n2 4 6\n", "output": "1\n6\n", "type": "stdin_stdout" } ] }
{ "difficulty": "interview", "problem_url": "https://www.codechef.com/COOK64/problems/SEAARASU" }
vfc_1522
apps
verifiable_code
671
Solve the following coding problem using the programming language python: Chef is going to start playing Fantasy Football League (FFL) this season. In FFL, each team consists of exactly $15$ players: $2$ goalkeepers, $5$ defenders, $5$ midfielders and $3$ forwards. Chef has already bought $13$ players; he is only missing one defender and one forward. There are $N$ available players (numbered $1$ through $N$). For each valid $i$, the $i$-th player is either a defender or a forward and has a price $P_i$. The sum of prices of all players in a team must not exceed $100$ dollars and the players Chef bought already cost him $S$ dollars. Can you help Chef determine if he can complete the team by buying one defender and one forward in such a way that he does not exceed the total price limit? -----Input----- - 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 line of each test case contains two space-separated integers $N$ and $S$. - The second line contains $N$ space-separated integers $P_1, P_2, \ldots, P_N$. - The last line contains $N$ space-separated integers. For each valid $i$, the $i$-th of these integers is $0$ if the $i$-th player is a defender or $1$ if the $i$-th player is a forward. -----Output----- For each test case, print a single line containing the string "yes" if it is possible to build a complete team or "no" otherwise (without quotes). -----Constraints----- - $1 \le T \le 100$ - $1 \le N \le 100$ - $13 \le S \le 100$ - $1 \le P_i \le 100$ for each valid $i$ -----Subtasks----- Subtask #1 (100 points): original constraints -----Example Input----- 2 4 90 3 8 6 5 0 1 1 0 4 90 5 7 6 5 0 1 1 0 -----Example Output----- yes no -----Explanation----- Example case 1: If Chef buys the $1$-st and $3$-rd player, the total price of his team is $90 + 9 = 99$, which is perfectly fine. There is no other valid way to pick two players. Example case 2: Chef cannot buy two players in such a way that all conditions are satisfied. The input will be stdin and you should print your solution to stdout Now solve the problem and return the code.
```python for i in range(int(input())): n,s =map(int,input().split()) l1=list(map(int,input().split())) l2=list(map(int,input().split())) m=[] n=[] for i in range(len(l1)): if l2[i]==0: m.append(l1[i]) else: n.append(l1[i]) if len(m)>0 and len(n)>0: if 100-s>=(min(m)+min(n)): print("yes") else: print("no") else: print("no") ```
{ "language": "python", "test_cases": [ { "input": "2\n4 90\n3 8 6 5\n0 1 1 0\n4 90\n5 7 6 5\n0 1 1 0\n", "output": "yes\nno\n", "type": "stdin_stdout" } ] }
{ "difficulty": "interview", "problem_url": "https://www.codechef.com/problems/FFL" }
vfc_1526
apps
verifiable_code
672
Solve the following coding problem using the programming language python: In my town ,there live a coder named Chef . He is a cool programmer . One day , he participate in a programming contest ,the contest give him only one problem . If he can’t solve the problem ,the problem setter will kill him . But the round allow you to help Chef. Can you save the life of Chef from problem setter ? :p You are given two point of a straightline in X and Y axis and they are A(x1 , y1) and B(x2 ,y2) . Problem setter will give you another point C(x3 , y3) . If C exist in AB straightline ,then print “YES” . Otherwise ,print “NO” in first line and print the minimum distance from C to AB straightline in second line . Please , save the life of Chef . Note : It is not possible that A and B point is similar . -----Input:----- The first line of the input contains a single integer t (1≤t≤100) — the number of test cases . Each test case starts with four integers( x1, y1 , x2 , y2 ) in first line . Next line contains a single number q ,the number of queries . Each query contains two integers ( x3 ,y3 ) -----Output:----- Print , q number of “YES” or “NO” (as it mentioned above) in each test case .For every test case , print “Test case : i ” ( 1<= i <=T ) -----Constraints----- -1000 <= x1 , y1 , x2 , y2 , x3 , y3 <= 1000 -----Sample Input:----- 2 3 5 6 5 2 4 5 6 8 3 4 7 10 1 7 4 -----Sample Output:----- Test case : 1 YES NO 3.000000 Test case : 2 NO 3.328201 The input will be stdin and you should print your solution to stdout Now solve the problem and return the code.
```python # cook your dish here from math import sqrt for i in range(int(input())): x1,y1,x2,y2=list(map(float,input().split())) m=(y2-y1)/(x2-x1) c=y2-m*x2 print('Test case : ',i+1) q=int(input()) for i in range(q): x3,y3=list(map(float,input().split())) if(y3-m*x3-c==0): print("YES") else: d=(abs(y3-m*x3-c))/sqrt(1+m*m) print("NO") print("%.6f" % d) ```
{ "language": "python", "test_cases": [ { "input": "2\n3 5 6 5\n2\n4 5\n6 8\n3 4 7 10\n1\n7 4\n", "output": "Test case : 1\nYES\nNO\n3.000000\nTest case : 2\nNO\n3.328201\n", "type": "stdin_stdout" } ] }
{ "difficulty": "interview", "problem_url": "https://www.codechef.com/ABCC2020/problems/POINT" }
vfc_1530
apps
verifiable_code
673
Solve the following coding problem using the programming language python: Its Christmas time and Santa has started his ride to deliver gifts to children waiting for him in a 1-dimentional city. All houses in this city are on a number line numbered as 1, 2, 3… and so on. Santa wants to deliver to houses from n to m, but he found that all the kids living at positions that are divisible by a, a+d, a+2d, a+3d or a+4d are naughty and he does not want to deliver them any gifts. Santa wants to know how many gifts he has to carry before leaving to the city given that there is only one kid in a house. Help him out! Formally, Given $m, n, a, d \in \mathbb{N}$ where $n < m$, find the number of $x \in \{n, n+1, ..., m-1, m\}$ such that $x$ is not divisible by $a$, $a+d$, $a+2d$, $a+3d$ or $a+4d$ -----Input----- The first line is the number $t$, corresponding to number of test cases\ This is followed by $t$ lines of the format: $n$ $m$ $a$ $d$ -----Output----- For each test case, print a single number that is the number of gifts Santa should pack. -----Constraints----- - $1 < m, n, a \leq 2^{32}$ - $1 < d \leq 2^{10}$ -----Sample Input:----- 1 2 20 2 1 -----Sample Output:----- 5 -----Explanation:----- In the range {2, 3, 4, …, 19, 20}, only {7, 11, 13, 17, 19} are not divisible by 2, 3, 4, 5, or 6 The input will be stdin and you should print your solution to stdout Now solve the problem and return the code.
```python from math import gcd from math import ceil from itertools import combinations as c t=int(input()) for _ in range(t): n,m,a,d=list(map(int,input().split())) l=[] for i in range(5): l.append(a+i*d) ans=m-n+1 for i in range(1,6): x=list(c(l,i)) for j in x: e=j[0] for v in j: e=(e*v)//gcd(e,v) #print(e) if i%2: ans-=m//e-(n-1)//e else: ans+=m//e-(n-1)//e print(ans) ```
{ "language": "python", "test_cases": [ { "input": "1\n2 20 2 1\n", "output": "5\n", "type": "stdin_stdout" } ] }
{ "difficulty": "interview", "problem_url": "https://www.codechef.com/NQST2020/problems/XMASGIFT" }
vfc_1534
apps
verifiable_code
674
Solve the following coding problem using the programming language python: Chef bought an electronic board and pen. He wants to use them to record his clients' signatures. The board is a grid with $N$ rows (numbered $1$ through $N$) and $M$ columns (numbered $1$ through $M$) of pixels. Initially, all pixels are white. A client uses the electronic pen to sign on the board; whenever the pen touches a pixel, this pixel becomes black. Note that a signature may be non-continuous (a client may lift the pen while signing). Chef stores a typical signature of his current client as a matrix of characters $A_{i, j}$, where for each valid $i$ and $j$, $A_{i, j}$ is either '1' (if the cell in the $i$-th row and $j$-th column is black) or '0' (if this cell is white). The client just signed on the board; this signature is stored in the same form as a matrix $B_{i, j}$. Chef wants to know how close this signature is to this client's typical signature. Two signatures are considered the same if it is possible to choose (possibly negative) integers $dr$ and $dc$ such that for each $1 \le i \le N$ and $1 \le j \le M$, $A_{i, j} = B_{i + dr, j + dc}$. Here, if $B_{i + dr, j + dc}$ does not correspond to a valid cell, it is considered to be '0'. To compare the signatures, the colours of zero or more cells must be flipped in such a way that the signatures become the same (each flipped cell may be in any matrix). The error in the client's current signature is the minimum number of cells whose colours must be flipped. Find the error in the signature. -----Input----- - 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 line of each test case contains two space-separated integers $N$ and $M$. - $N$ lines follow. For each valid $i$, the $i$-th of these lines contains a string with length $M$ describing the $i$-th row of the matrix $A$. - $N$ more lines follow. For each valid $i$, the $i$-th of these lines contains a string with length $M$ describing the $i$-th row of the matrix $B$. -----Output----- For each test case, print a single line containing one integer — the error in the current signature. -----Constraints----- - $1 \le T \le 50$ - $2 \le N, M \le 25$ -----Example Input----- 5 3 3 100 010 000 000 010 001 4 4 0000 0110 0000 0011 1100 0000 1100 0000 3 3 100 000 001 000 010 000 3 3 000 010 000 100 000 001 3 3 111 000 000 001 001 001 -----Example Output----- 0 2 1 0 2 The input will be stdin and you should print your solution to stdout Now solve the problem and return the code.
```python import numpy as np for _ in range(int(input())): ans = np.float('inf') n, m = (int(x) for x in input().split()) sig = np.zeros((n,m)) img = np.zeros((3*n,3*m)) for row in range(n): sig[row,:] = np.array([int(x) for x in input()]) for row in range(n): img[row+n,m:2*m] = np.array([int(x) for x in input()]) for i in range(2*n): for j in range(2*m): ans = min(ans, np.abs(np.sum(img[i:n+i, j:m+j] != sig))) print(ans) ```
{ "language": "python", "test_cases": [ { "input": "5\n3 3\n100\n010\n000\n000\n010\n001\n4 4\n0000\n0110\n0000\n0011\n1100\n0000\n1100\n0000\n3 3\n100\n000\n001\n000\n010\n000\n3 3\n000\n010\n000\n100\n000\n001\n3 3\n111\n000\n000\n001\n001\n001\n", "output": "0\n2\n1\n0\n2\n", "type": "stdin_stdout" } ] }
{ "difficulty": "interview", "problem_url": "https://www.codechef.com/problems/SIGNTURE" }
vfc_1538
apps
verifiable_code
676
Solve the following coding problem using the programming language python: There is an event in DUCS where boys get a chance to show off their skills to impress girls. The boy who impresses the maximum number of girls will be honoured with the title “Charming Boy of the year”. There are $N$ girls in the department. Each girl gives the name of a boy who impressed her the most. You need to find the name of a boy who will be honoured with the title. If there are more than one possible winners, then the one with the lexicographically smallest name is given the title. It is guaranteed that each boy participating in the event has a unique name. -----Input----- - 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 line of each test case contains an integer $N$ denoting the number of girls. - The second line contains $N$ space-separated strings $S_1, S_2, \ldots, S_N$, denoting the respective names given by the girls. -----Output----- For each test case, print a single line containing a string — the name of the boy who impressed the maximum number of girls. In case of a tie, print the lexicographically smallest name. -----Constraints----- - $1 \leq T \leq 100$ - $1 \leq N \leq 10^5$ - $1 \leq |S_i| \leq 10$, for each valid $i$ $(|S_i|$ is the length of the string $S_i)$ - For each valid $i$, $S_i$ contains only lowercase English alphabets - Sum of $N$ over all the test cases is $\leq 10^6$ -----Subtasks----- - 30 points: $1 \leq N \leq 100$ - 70 points: original constraints -----Sample Input----- 2 10 john berry berry thomas thomas john john berry thomas john 4 ramesh suresh suresh ramesh -----Sample Output----- john ramesh The input will be stdin and you should print your solution to stdout Now solve the problem and return the code.
```python from collections import Counter for _ in range(int(input())): n=int(input()) l=[i for i in input().split()] ll=[] c=Counter(l) cc=[] m=0 for l,count in c.most_common(len(l)-1): if m==0: ll.append(l) cc.append(count) if m==count: ll.append(l) cc.append(count) if count<m: break m=count k=set(cc) leng=len(list(k)) if leng==1: sor=sorted(ll) print(sor[0]) else: print(ll[0]) ```
{ "language": "python", "test_cases": [ { "input": "2\n10\njohn berry berry thomas thomas john john berry thomas john\n4\nramesh suresh suresh ramesh\n", "output": "john\nramesh\n", "type": "stdin_stdout" } ] }
{ "difficulty": "interview", "problem_url": "https://www.codechef.com/DCC2020/problems/DCC202" }
vfc_1546
apps
verifiable_code
677
Solve the following coding problem using the programming language python: Computation of the date either previous or forthcoming dates is quiet easy. But it is quiet difficult to calculate the day from a particular given date. You are required to find a day from a particular date given to you. -----Input----- It consists of a single line entry consisting of date in format dd mm yyyy. i.e. the input line consists of the three numbers written in order followed by spaces. Eg. Input for 18-12-1990 is be written as 18 12 1990 -----Output----- It consists of single line output showing the day for that particular date. -----Example----- Input: 14 3 2012 Output: Wednesday The input will be stdin and you should print your solution to stdout Now solve the problem and return the code.
```python import sys import datetime a,b,c = list(map(int,sys.stdin.readline().split())) d = datetime.date(c,b,a) print(d.strftime("%A")) ```
{ "language": "python", "test_cases": [ { "input": "14 3 2012\n", "output": "Wednesday\n", "type": "stdin_stdout" } ] }
{ "difficulty": "interview", "problem_url": "https://www.codechef.com/TCTR2012/problems/NOPC10" }
vfc_1550
apps
verifiable_code
678
Solve the following coding problem using the programming language python: Snackdown 2019 is coming! People have started to spread the word and tell other people about the contest. There are $N$ people numbered $1$ through $N$. Initially, only person $1$ knows about Snackdown. On each day, everyone who already knows about Snackdown tells other people about it. For each valid $i$, person $i$ can tell up to $A_i$ people per day. People spread the information among the people who don't know about Snackdown in the ascending order of their indices; you may assume that no two people try to tell someone about Snackdown at the same moment. Each person is only allowed to start telling other people about Snackdown since the day after he/she gets to know about it (person $1$ can start telling other people already on day $1$). How many days does it take for all people to know about Snackdown? -----Input----- - 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 line of each test case contains a single integer $N$. - The second line contains $N$ space-separated integers $A_1, A_2, \dots, A_N$. -----Output----- For each test case, print a single line containing one integer — the number of days. -----Constraints----- - $1 \le T \le 1,000$ - $2 \le N \le 10^5$ - the sum of $N$ for all test cases does not exceed $10^6$ - $0 \le A_i \le N$ for each valid $i$ - $1 \le A_1$ -----Example Input----- 2 7 2 1 1 5 5 5 5 5 5 1 3 2 1 -----Example Output----- 2 1 -----Explanation----- Example case 1: On day $1$, person $1$ tells people $2$ and $3$ about Snackdown. On day $2$, the first three people know about Snackdown, so they can tell $2+1+1 = 4$ people about it in a single day. That means the last four people get to know about Snackdown on day $2$, so the total number of days is $2$. Example case 2: On each day, person $1$ can tell up to $5$ people about Snackdown, so on the first day, he simply tells all people about it and the total number of days is $1$. The input will be stdin and you should print your solution to stdout Now solve the problem and return the code.
```python # cook your dish here test_case = int(input()) while test_case : n_people = int(input()) array = list(map(int, input().strip().split())) sums =[0 for i in range(n_people)] sums[0] = array[0] for i in range(1, n_people) : sums[i] = sums[i-1] + array[i] # print(sums) k = 1 count = 0 i = 0 while(k < n_people) : k = k + sums[i] # print(k) i = i + sums[i] count = count + 1 print(count) test_case -= 1 # 2 1 1 5 5 5 5 # [2, 3, 4, 9, 14, 19, 24] # 0 1 2 3 4 5 6 ```
{ "language": "python", "test_cases": [ { "input": "2\n7\n2 1 1 5 5 5 5\n5\n5 1 3 2 1\n", "output": "2\n1\n", "type": "stdin_stdout" } ] }
{ "difficulty": "interview", "problem_url": "https://www.codechef.com/problems/SPREAD2" }
vfc_1554
apps
verifiable_code
679
Solve the following coding problem using the programming language python: Harry is a bright student. To prepare thoroughly for exams, he completes all the exercises in his book! Now that the exams are approaching fast, he is doing book exercises day and night. He writes down and keeps updating the remaining number of exercises on the back cover of each book. Harry has a lot of books messed on the floor. Therefore, he wants to pile up the books that still have some remaining exercises into a single pile. He will grab the books one-by-one and add the books that still have remaining exercises to the top of the pile. Whenever he wants to do a book exercise, he will pick the book with the minimum number of remaining exercises from the pile. In order to pick the book, he has to remove all the books above it. Therefore, if there are more than one books with the minimum number of remaining exercises, he will take the one which requires the least number of books to remove. The removed books are returned to the messy floor. After he picks the book, he will do all the remaining exercises and trash the book. Since number of books is rather large, he needs your help to tell him the number of books he must remove, for picking the book with the minimum number of exercises. Note that more than one book can have the same name. -----Input----- The first line contains a single integer N denoting the number of actions. Then N lines follow. Each line starts with an integer. If the integer is -1, that means Harry wants to do a book exercise. Otherwise, the integer is number of the remaining exercises in the book he grabs next. This is followed by a string denoting the name of the book. -----Output----- For each -1 in the input, output a single line containing the number of books Harry must remove, followed by the name of the book that Harry must pick. -----Constraints----- 1 < N ≤ 1,000,000 0 ≤ (the number of remaining exercises of each book) < 100,000 The name of each book consists of between 1 and 15 characters 'a' - 'z'. Whenever he wants to do a book exercise, there is at least one book in the pile. -----Example----- Input: 6 9 english 6 mathematics 8 geography -1 3 graphics -1 Output: 1 mathematics 0 graphics The input will be stdin and you should print your solution to stdout Now solve the problem and return the code.
```python t=eval(input()) a=[] b=[] top=-1 for __ in range(0,t): x=input().split() if(x[0]!="-1" and x[0]!="0"): add=int(x[0]) if top!=-1 and add>a[top][0] : b[top]+=1 else: a.append((add,x[1])) b.append(0) top+=1 elif (x[0]=="-1"): #print("%s %s" %(b[top],a[top][1])) print((b[top]), end=' ') print(a[top][1]) foo=a.pop() bar=b.pop() top-=1 ```
{ "language": "python", "test_cases": [ { "input": "6\n9 english\n6 mathematics\n8 geography\n-1\n3 graphics\n-1\n", "output": "1 mathematics\n0 graphics\n", "type": "stdin_stdout" } ] }
{ "difficulty": "interview", "problem_url": "https://www.codechef.com/DEC12/problems/BEX" }
vfc_1558
apps
verifiable_code
680
Solve the following coding problem using the programming language python: You are given two integer sequences $A_1, A_2, \ldots, A_N$ and $B_1, B_2, \ldots, B_M$. For any two sequences $U_1, U_2, \ldots, U_p$ and $V_1, V_2, \ldots, V_q$, we define Score(U,V)=∑i=1p∑j=1qUi⋅Vj.Score(U,V)=∑i=1p∑j=1qUi⋅Vj.Score(U, V) = \sum_{i=1}^p \sum_{j=1}^q U_i \cdot V_j \,. You should process $Q$ queries of three types: - $1$ $L$ $R$ $X$: Add $X$ to each of the elements $A_L, A_{L+1}, \ldots, A_R$. - $2$ $L$ $R$ $X$: Add $X$ to each of the elements $B_L, B_{L+1}, \ldots, B_R$. - $3$: Print $Score(A, B)$ modulo $998,244,353$. -----Input----- - 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 line of each test case contains two integers, $N$ and $M$, denoting the length of $A$ and $B$ respectively. - The second line contains $N$ integers, elements of $A$. - The third line contains $M$ integers, elements of $B$. - The next line will contain an integer, $Q$, number of queries. - Each of the next $Q$ lines will contain one of $3$ kinds of updates as mentioned in the statement It’s guaranteed that each update is a valid update operation. -----Output----- For each query of the third type, print a single line containing one integer - the answer to that query. -----Constraints----- - $1 \le T \le 10$ - $2 \le N, M, Q \le 10^5$ - $0 \le |A_i|, |B_i|, |X| \le 10^5$ -----Example Input----- 1 3 4 2 -1 5 3 3 2 4 6 3 1 2 3 -2 3 1 1 3 1 2 2 4 2 3 -----Example Output----- 72 24 90 -----Explanation----- Before the first operation, $A = [2, -1, 5],\ B = [3, 3, 2, 4]$ So, for the first operation, $Score(A,\ B) = 2*3 + 2*3 + 2*2 + 2*4$ $+ (-1)*3$ $+ (-1)*3$ $+ (-1)*2$ $+$ $(-1)*4$ $+ 5*3$ $+ 5*3$ $+ 5*2$ $+ 5*4$ $= 72.$ After the second query $A = [2, -3, 3]$, $B = [3, 3, 2, 4]$ So, for the third query, $Score(A, B) = 2*3 + 2*3 + 2*2$ $+ 2*4$ $+ (-3)*3$ $+ (-3)*3$ $+ (-3)*2$ $+ (-3)*4$ $+ 3*3$ $+ 3*3$ $+ 3*2$ $+ 3*4$ $= 24$. The input will be stdin and you should print your solution to stdout Now solve the problem and return the code.
```python t = int(input()) l,r,x = 0,0,0 ans = [] for i in range(t): (n,m) = tuple(map(int,input().split())) a = list(map(int,input().split())) b = list(map(int,input().split())) suma = sum(a) sumb = sum(b) q = int(input()) for j in range(q): l1 = list(map(int,input().split())) if l1[0] == 1: l = l1[1] r = l1[2] x = l1[3] suma = suma + (r-l+1)*x elif l1[0] == 2: l = l1[1] r = l1[2] x = l1[3] sumb = sumb + (r-l+1)*x else: ans.append((suma*sumb)%998244353) for i in range(len(ans)): print(ans[i]) ```
{ "language": "python", "test_cases": [ { "input": "1\n3 4\n2 -1 5\n3 3 2 4\n6\n3\n1 2 3 -2\n3\n1 1 3 1\n2 2 4 2\n3\n", "output": "72\n24\n90\n", "type": "stdin_stdout" } ] }
{ "difficulty": "interview", "problem_url": "https://www.codechef.com/problems/ARRQRY" }
vfc_1562
apps
verifiable_code
681
Solve the following coding problem using the programming language python: You are playing following game: given an array A of N natural numbers. All numbers in the array A are at most M. On every turn you may pick any two different elements Ai and Aj (i≠j), such that Ai, Aj ≤ M, and add K to both. The game ends when you are not able to continue. That is, when there is no pair (i,j) left such that both of them are less than equal to M. Let's call two arrays different if the sum of all their elements is different. When the game ends, you note down the final array A. How many different final arrays can you have. -----Input----- The first line contains three integers N, M and K. N elements of the array follow in the next line. -----Output----- Output single integer - answer for the given problem modulo 109+7. -----Constraints----- - 1 ≤ N ≤ 105 - 1 ≤ M,K ≤ 1012 - 1 ≤ Ai ≤ M -----Example----- Input: 3 3 2 1 2 3 Output: 2 -----Explanation----- All possible sums are 14 and 10. You can get them by, for example, these arrays: A=(5, 4, 5), A=(1, 4, 5) The above arrays are different because their sums are different. The input will be stdin and you should print your solution to stdout Now solve the problem and return the code.
```python from math import ceil from bisect import bisect_right as b_r from bisect import bisect_left as b_l ar = list(map(int , input().split())) a = [int(ceil((ar[1]-int(x)+1)/ar[2])) for x in input().split()] s = sum(a) ar[1] = max(a) m = ar[1] - (s-ar[1])%2 mi = s%2 print(int( (m-mi)//2 +1)%(10**9+7)) ```
{ "language": "python", "test_cases": [ { "input": "3 3 2\n1 2 3\n", "output": "2\n", "type": "stdin_stdout" } ] }
{ "difficulty": "interview", "problem_url": "https://www.codechef.com/problems/RRGAME" }
vfc_1566
apps
verifiable_code
682
Solve the following coding problem using the programming language python: Rohit collects coins: he has exactly one coin for every year from 1 to n. Naturally, Rohit keeps all the coins in his collection in the order in which they were released. Once Rohit's younger brother made a change — he took all the coins whose release year dated from l to r inclusively and put them in the reverse order. That is, he took a certain segment [l, r] and reversed it. At that, the segment's endpoints did not coincide. For example, if n = 8, then initially Rohit's coins were kept in the order 1 2 3 4 5 6 7 8. If Rohit's younger brother chose the segment [2, 6], then after the reversal the coin order will change to 1 6 5 4 3 2 7 8. Rohit suspects that someone else could have spoilt the permutation after his brother. Help him to find that out. Check if the given permutation can be obtained from the permutation 1 2 … n using exactly one segment reversal. If it is possible, find the segment itself. -----Input:----- - The first line contains an integer N which is the number of coins in Rohit's collection. - The second line contains space-separated n integers which are the spoilt sequence of coins. It is guaranteed that the given sequence is a permutation, i.e. it contains only integers from 1 to n, and every number is used exactly 1 time. -----Output:----- If it is impossible to obtain the given permutation from the original one in exactly one action, print 0 0. Otherwise, print two numbers l, r (1 ≤ l < r ≤ n) which are the endpoints of the segment that needs to be reversed to obtain from permutation 1 2 … n the given one. -----Constraints----- - $1 \leq N \leq 1000$ - $1 \leq A[N] \leq 10^9$ -----Sample Input:----- 8 1 6 5 4 3 2 7 8 -----Sample Output:----- 2 6 The input will be stdin and you should print your solution to stdout Now solve the problem and return the code.
```python n=int(input()) a=list(map(int,input().split())) l,r=-1,-1 for i in range(n): if a[i]!=i+1: l=i break for i in range(n-1,-1,-1): if a[i]!=i+1: r=i break j=r+1 for i in range(l,r+1): if a[i]==j: j-=1 continue else: print(0,0) return print(l+1,r+1) ```
{ "language": "python", "test_cases": [ { "input": "8\n1 6 5 4 3 2 7 8\n", "output": "2 6\n", "type": "stdin_stdout" } ] }
{ "difficulty": "interview", "problem_url": "https://www.codechef.com/BRBG2020/problems/PRMA" }
vfc_1570
apps
verifiable_code
683
Solve the following coding problem using the programming language python: -----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 The input will be stdin and you should print your solution to stdout Now solve the problem and return the code.
```python # cook your dish here a = int(input()) print(a) ```
{ "language": "python", "test_cases": [ { "input": "123\n", "output": "123\n", "type": "stdin_stdout" } ] }
{ "difficulty": "interview", "problem_url": "https://www.codechef.com/problems/START01" }
vfc_1574
apps
verifiable_code
684
Solve the following coding problem using the programming language python: Congratulations !!! You have successfully completed the heist by looting all the gifts in Santa's locker. Now it's time to decide who gets to take all the gifts, you or the Grinch, there will be no splitting. So you and Grinch decide to play a game. To start the game, an Integer N will be given. The game is played in turns and for each turn, the player can make any one of the following moves: - Divide N by any of it's odd divisors greater than 1. - Subtract 1 from N if N is greater than 1. Divisor of a number includes the number itself. The player who is unable to make a move loses the game. Since you are the mastermind of the heist, you get to play the first move. -----Input----- The first line contains a single integer T (1 ≤ T ≤ 100) — the number of test cases. The description of the test cases follows. The only line of each test case contains a single integer — N (1 ≤ N ≤ 109). -----Output----- For each test case, print " Me" if you win, and " Grinch" if otherwise (without quotes). -----Sample Input----- 7 1 2 3 4 5 6 12 -----Sample Output----- Grinch Me Me Grinch Me Grinch Me The input will be stdin and you should print your solution to stdout Now solve the problem and return the code.
```python # cook your dish here import math # Function to find the Largest # Odd Divisor Game to check # which player wins def findWinner(n, k): cnt = 0; # Check if n == 1 then # player 2 will win if (n == 1): print("Grinch"); # Check if n == 2 or n is odd elif ((n & 1) or n == 2): print("Me"); else: tmp = n; val = 1; # While n is greater than k and # divisible by 2 keep # incrementing tha val while (tmp > k and tmp % 2 == 0): tmp //= 2; val *= 2; # Loop to find greatest # odd divisor for i in range(3, int(math.sqrt(tmp)) + 1): while (tmp % i == 0): cnt += 1; tmp //= i; if (tmp > 1): cnt += 1; # Check if n is a power of 2 if (val == n): print("Grinch"); elif (n / tmp == 2 and cnt == 1): print("Grinch"); # Check if cnt is not one # then player 1 wins else: print("Me"); # Driver code def __starting_point(): for i in range(int(input())): n=int(input()) findWinner(n, 1); __starting_point() ```
{ "language": "python", "test_cases": [ { "input": "7\n1\n2\n3\n4\n5\n6\n12\n", "output": "Grinch\nMe\nMe\nGrinch\nMe\nGrinch\nMe\n", "type": "stdin_stdout" } ] }
{ "difficulty": "interview", "problem_url": "https://www.codechef.com/NQST2020/problems/WINALL" }
vfc_1578
apps
verifiable_code
685
Solve the following coding problem using the programming language python: You are given an axis-aligned rectangle in a 2D Cartesian plane. The bottom left corner of this rectangle has coordinates (0,0)$(0, 0)$ and the top right corner has coordinates (N−1,N−1)$(N-1, N-1)$. You are also given K$K$ light sources; each light source is a point inside or on the perimeter of the rectangle. For each light source, let's divide the plane into four quadrants by a horizontal and a vertical line passing through this light source. The light source can only illuminate one of these quadrants (including its border, i.e. the point containing the light source and two half-lines), but the quadrants illuminated by different light sources may be different. You want to assign a quadrant to each light source in such a way that when they illuminate their respective quadrants, the entire rectangle (including its perimeter) is illuminated. Find out whether it is possible to assign quadrants to light sources in such a way. -----Input----- - The first line of the input contains an integer T$T$ denoting the number of test cases. The description of the test cases follows. - The first line of each test case contains two space-separated integers K$K$ and N$N$. - Each of the next K$K$ lines contains two space-separated integers x$x$ and y$y$ denoting a light source with coordinates (x,y)$(x, y)$. -----Output----- For each test case, print a single line containing the string "yes" if it is possible to illuminate the whole rectangle or "no" if it is impossible. -----Constraints----- - 1≤T≤5,000$1 \le T \le 5,000$ - 1≤K≤100$1 \le K \le 100$ - 1≤N≤109$1 \le N \le 10^9$ - 0≤x,y≤N−1$0 \le x, y \le N-1$ - no two light sources coincide -----Example Input----- 2 2 10 0 0 1 0 2 10 1 2 1 1 -----Example Output----- yes no The input will be stdin and you should print your solution to stdout Now solve the problem and return the code.
```python # https://www.codechef.com/problems/RECTLIT def assess(sq,points): EWct = 0 NSct = 0 for a,b in points: EW = (a == 0 or a == sq) NS = (b == 0 or b == sq) if EW and NS: return 'yes' EWct += EW NSct += NS if NSct + EWct == 0 or len(points) == 1: return 'no' if EWct >= 2 or NSct >= 2: return 'yes' if len(points) == 2: return 'no' # now 3 points if NSct == 1 and EWct == 1: return 'yes' # 3 points, one on edge x = -1 for a,b in points: if EWct > 0: if a == 0 or a == sq: e = b elif x == -1: x = b else: y = b else: if b == 0 or b == sq: e = a elif x == -1: x = a else: y = a if (e-x)*(e-y) < 0: # edge splits mids return 'no' else: return 'yes' for ti in range(int(input())): k,n = map(int, input().split()) if k > 3: for ki in range(k): input() print('yes') else: pos = [tuple(map(int, input().split())) for ki in range(k)] print(assess(n-1,pos)) ```
{ "language": "python", "test_cases": [ { "input": "2\n2 10\n0 0\n1 0\n2 10\n1 2\n1 1\n", "output": "yes\nno\n", "type": "stdin_stdout" } ] }
{ "difficulty": "interview", "problem_url": "https://www.codechef.com/problems/RECTLIT" }
vfc_1582
apps
verifiable_code
686
Solve the following coding problem using the programming language python: Chef has been working in a restaurant which has N floors. He wants to minimize the time it takes him to go from the N-th floor to ground floor. He can either take the elevator or the stairs. The stairs are at an angle of 45 degrees and Chef's velocity is V1 m/s when taking the stairs down. The elevator on the other hand moves with a velocity V2 m/s. Whenever an elevator is called, it always starts from ground floor and goes to N-th floor where it collects Chef (collecting takes no time), it then makes its way down to the ground floor with Chef in it. The elevator cross a total distance equal to N meters when going from N-th floor to ground floor or vice versa, while the length of the stairs is sqrt(2) * N because the stairs is at angle 45 degrees. Chef has enlisted your help to decide whether he should use stairs or the elevator to minimize his travel time. Can you help him out? -----Input----- The first line contains a single integer T, the number of test cases. Each test case is described by a single line containing three space-separated integers N, V1, V2. -----Output----- For each test case, output a single line with string Elevator or Stairs, denoting the answer to the problem. -----Constraints----- - 1 ≤ T ≤ 1000 - 1 ≤ N, V1, V2 ≤ 100 -----Example----- Input: 3 5 10 15 2 10 14 7 14 10 Output: Elevator Stairs Stairs The input will be stdin and you should print your solution to stdout Now solve the problem and return the code.
```python n=int(input()) l=[] for i in range(0,n): a,b,c=map(int,input().split()) n1=(2**0.5)*(a/b) n2=2*(a/c) if n1>n2: l.append("Elevator") else: l.append("Stairs") for i in l: print(i) ```
{ "language": "python", "test_cases": [ { "input": "3\n5 10 15\n2 10 14\n7 14 10\n", "output": "Elevator\nStairs\nStairs\n", "type": "stdin_stdout" } ] }
{ "difficulty": "interview", "problem_url": "https://www.codechef.com/problems/ELEVSTRS" }
vfc_1586
apps
verifiable_code
687
Solve the following coding problem using the programming language python: The Little Elephant likes permutations. This time he has a permutation A[1], A[2], ..., A[N] of numbers 1, 2, ..., N. He calls a permutation A good, if the number of its inversions is equal to the number of its local inversions. The number of inversions is equal to the number of pairs of integers (i; j) such that 1 ≤ i < j ≤ N and A[i] > A[j], and the number of local inversions is the number of integers i such that 1 ≤ i < N and A[i] > A[i+1]. The Little Elephant has several such permutations. Help him to find for each permutation whether it is good or not. Print YES for a corresponding test case if it is good and NO otherwise. -----Input----- The first line of the input contains a single integer T, the number of test cases. T test cases follow. The first line of each test case contains a single integer N, the size of a permutation. The next line contains N space separated integers A[1], A[2], ..., A[N]. -----Output----- For each test case output a single line containing the answer for the corresponding test case. It should be YES if the corresponding permutation is good and NO otherwise. -----Constraints----- 1 ≤ T ≤ 474 1 ≤ N ≤ 100 It is guaranteed that the sequence A[1], A[2], ..., A[N] is a permutation of numbers 1, 2, ..., N. -----Example----- Input: 4 1 1 2 2 1 3 3 2 1 4 1 3 2 4 Output: YES YES NO YES -----Explanation----- Case 1. Here N = 1, so we have no pairs (i; j) with 1 ≤ i < j ≤ N. So the number of inversions is equal to zero. The number of local inversion is also equal to zero. Hence this permutation is good. Case 2. Here N = 2, and we have one pair (i; j) with 1 ≤ i < j ≤ N, the pair (1; 2). Since A[1] = 2 and A[2] = 1 then A[1] > A[2] and the number of inversions is equal to 1. The number of local inversion is also equal to 1 since we have one value of i for which 1 ≤ i < N (the value i = 1) and A[i] > A[i+1] for this value of i since A[1] > A[2]. Hence this permutation is also good. Case 3. Here N = 3, and we have three pairs (i; j) with 1 ≤ i < j ≤ N. We have A[1] = 3, A[2] = 2, A[3] = 1. Hence A[1] > A[2], A[1] > A[3] and A[2] > A[3]. So the number of inversions is equal to 3. To count the number of local inversion we should examine inequalities A[1] > A[2] and A[2] > A[3]. They both are satisfied in our case, so we have 2 local inversions. Since 2 ≠ 3 this permutations is not good. Case 4. Here we have only one inversion and it comes from the pair (2; 3) since A[2] = 3 > 2 = A[3]. This pair gives also the only local inversion in this permutation. Hence the number of inversions equals to the number of local inversions and equals to one. So this permutation is good. The input will be stdin and you should print your solution to stdout Now solve the problem and return the code.
```python from sys import stdin t = int(stdin.readline()) def count(n, arr): loc = 0 glob = 0 for i in range(n-1): if arr[i] > arr[i+1]: loc += 1 for i in range(n-1): for j in range(i+1, n): if glob > loc: return 0 if arr[i] > arr[j]: glob += 1; if glob == loc: return 1 return 0 for _ in range(t): n = int(stdin.readline()) arr = list(map(int, stdin.readline().split())) result = count(n, arr) if result: print("YES") else: print("NO") ```
{ "language": "python", "test_cases": [ { "input": "4\n1\n1\n2\n2 1\n3\n3 2 1\n4\n1 3 2 4\n", "output": "YES\nYES\nNO\nYES\n", "type": "stdin_stdout" } ] }
{ "difficulty": "interview", "problem_url": "https://www.codechef.com/COOK28/problems/LEPERMUT" }
vfc_1590
apps
verifiable_code
689
Solve the following coding problem using the programming language python: In africa jungle , there were zebra's who liked to spit. There owner watched them for whole day and noted in his sheet where each zebra spitted. Now he's in a confusion and wants to know if in the jungle there are two zebra's which spitted at each other. Help him solve this task. If the zebra is present in position a spits b metres right , he can hit only zebra in position a+b , if such a zebra exists. -----Input:----- - The first line contains integer t(1<=t<100)- amount of zebras in jungle. - Each of following t lines contains two integers a(i) and b(i)(-10^4<=x(i)<=10^4,1<|d(i)|<=2.10^4) - records in owner sheet. - a(i) is the position of i-th zebra and b(i) is distance at which the i-th camel spitted. Positive values of d(i) correspond to spits right, negative values correspond to spit left.No two zebras may stand in the same position. -----Output:----- If there are two zebras , which spitted at each other , output YES, otherwise , output NO. -----Sample Input:----- 2 0 1 1 -1 -----Sample Output:----- YES The input will be stdin and you should print your solution to stdout Now solve the problem and return the code.
```python # cook your dish here t=int(input()) i=0 a=0 d=dict() while i<t: l=input().split() d[int(l[0])]=int(l[0])+int(l[1]) i+=1 for k in d: if d[k] in d: if d[d[k]]==k: a=1 break if a==1: print("YES") else: print("NO") ```
{ "language": "python", "test_cases": [ { "input": "2\n0 1\n1 -1\n", "output": "YES\n", "type": "stdin_stdout" } ] }
{ "difficulty": "interview", "problem_url": "https://www.codechef.com/CMR12121/problems/ZBJNG" }
vfc_1598
apps
verifiable_code
690
Solve the following coding problem using the programming language python: You are a king and you are at war. If the enemy breaks through your frontline you lose. Enemy can break the line only if the sum of morale of any $K$ continuous soldiers is strictly less than $M$. So, you being a motivational orator decides to boost their morale by giving a speech. On hearing it morale of a soldier multiplies by $X$ which depends on you and your speech (i.e. $X$ can be any positive value) but atmost only $K$ continuous speakers can hear your speech. N soldiers are standing on the frontline with $A[i]$ morale. Determine the minimum number of speeches you need to give. -----Input:----- The first line contains three space seperated integers $N,K,M$. The next line contains $N$ space integers, ith of which denotes the morale of $ith$ soldier. -----Output:----- Output the minimum number of speeches required. In case if it is impossible to achieve, print $-1$. -----Constraints:----- $1 \leq N,M \leq 10^5$ $1 \leq k \leq N$ $0 \leq Ai \leq 10^5$ -----Sample Input:----- 6 2 5 1 1 1 1 1 1 -----Sample Output:----- 2 -----Explanation:----- We multiply 2nd ,3rd and 5th,6th by 5. Resulting array will be 1 5 5 1 5 5. The input will be stdin and you should print your solution to stdout Now solve the problem and return the code.
```python n,k,m = map(int,input().split()) ar = list(map(int,input().split())) fsum = [ar[0]] for i in range(1,n): fsum.append(fsum[i-1]+ar[i]) i = k #print(fsum) c = 0 while i <= n: if i == k: s = fsum[i-1] else: s = fsum[i-1]-fsum[i-k-1] if s == 0: c = -1 break if s < m: c += 1 if i<n: for j in range(i,i-k-1,-1): if ar[j-1] >0: j += k-1 i = j break if i<n: for j in range(i,i-k-1,-1): if ar[j-1] >0: j += k-1 i = j break i += 1 i = k while i <= n: if i==k: s = fsum[i-1] else: s = fsum[i-1] - fsum[i-k-1] if s == 0 : c = -1 break i += 1 print(c) ```
{ "language": "python", "test_cases": [ { "input": "6 2 5\n1 1 1 1 1 1\n", "output": "2\n", "type": "stdin_stdout" } ] }
{ "difficulty": "interview", "problem_url": "https://www.codechef.com/INTW2020/problems/MORALE99" }
vfc_1602
apps
verifiable_code
691
Solve the following coding problem using the programming language python: You are given a sequence $A_1, A_2, \ldots, A_N$. For each valid $i$, the star value of the element $A_i$ is the number of valid indices $j < i$ such that $A_j$ is divisible by $A_i$. Chef is a curious person, so he wants to know the maximum star value in the given sequence. Help him find it. -----Input----- - The first line of the input contains a single integer $T$ which denotes the number of test cases. - The first line of each test case contains a single integer $N$ . - The second line of each test case contains $N$ space-separated integers $A_1, A_2, \ldots, A_N$. -----Output----- For each test case, print a single line containing one integer ― the maximum star value. -----Constraints----- - $1 \le T \le 10$ - $1 \le N \le 10^5$ - $1 \le A_i \le 10^6$ for each valid $i$ - Sum of $N$ over all test cases does not exceed $100,000$. -----Subtasks----- Subtask #1 (20 points): sum of $N$ over all test cases does not exceed $5,000$ Subtask #2 (80 points): original constraints -----Example Input----- 1 7 8 1 28 4 2 6 7 -----Example Output----- 3 -----Explanation----- $A_5 = 2$ divides $4$, $28$ and $8$, so its star value is $3$. There is no element with a higher star value. The input will be stdin and you should print your solution to stdout Now solve the problem and return the code.
```python T = int(input()) for _ in range(T): n = int(input()) arr = list(map(int, input().split())) a = [0 for _ in range(max(arr)+1)] star_val = [] for i in range(len(arr)): j = 1 val = 0 while j*arr[i] <= len(a): val += a[j*arr[i]-1] j += 1 star_val.append(val) a[arr[i]-1] += 1 print(max(star_val)) ```
{ "language": "python", "test_cases": [ { "input": "1\n7\n8 1 28 4 2 6 7\n", "output": "3\n", "type": "stdin_stdout" } ] }
{ "difficulty": "interview", "problem_url": "https://www.codechef.com/problems/MSV" }
vfc_1606
apps
verifiable_code
692
Solve the following coding problem using the programming language python: There is crazy man named P29892P. He always tries to do crazy things as he thinks. One day he invented a machine and named it as ANGEN. The ANGEN is used to perform range operations. The range operation means performing operations on range values from {L..R}. At any time it performs operations on values in between L to R. ANGEN can perform following operations U I V - Update the value present at I with value V A L R - Find the sum between range L and R M L R - Find the maximum number between L and R m L R - Find the minimum number between L and R S L R - Find second maximum value in between L and R s L R - Find second mimimum value in between L and R If it is not possible perform operation ANGEN returns “NA” with out quotes. For Invalid operations ANGEN returns “!!!” with out quotes. Now P29892P challenging his friends and you too, to build his invention with yourown code. So it's your time to defeat P29892P by implementing his invention with your own ability. Let's go and solve the problem. -----Input----- Input description. - The first line of the input contains an integer N denoting the number of integers. - The next line contains N space separated integers." - The next line contains a single integer Q denoting the number of Questions. - The next Q lines contains T Question type , L and R. -----Output----- Print output for each question in separate line. -----Constraints----- Should contain all the constraints on the input data that you may have. Format it like: - 1 ≤ N ≤ 100000 - 1 ≤ values[i] ≤ 1000000000 - 1 ≤ Q ≤ 10000 - T in { A..Z, a..z } - 1 ≤ L ≤ R ≤ N -----Example----- Input: 6 1 2 5 3 10 6 6 A 1 5 M 1 3 m 5 6 s 3 6 U 1 7 S 1 2 Output: 21 5 6 5 2 -----Explanation----- ... The input will be stdin and you should print your solution to stdout Now solve the problem and return the code.
```python VQ = "UAMmSs" n = int(input()) a = list(map(int, input().split())) for _ in range(int(input())): q, x, y = input().split() if q not in VQ: print("!!!") continue if q == "U": a[int(x) - 1] = int(y) continue l = int(x) - 1 r = int(y) if q == "A": print(sum(a[l:r])) continue if q == "M": print(max(a[l:r])) continue if q == "m": print(min(a[l:r])) continue s = sorted(set(a[l:r])) if len(s) < 2: print("NA") else: print(s[1] if q == "s" else s[-2]) ```
{ "language": "python", "test_cases": [ { "input": "6\n1 2 5 3 10 6\n6\nA 1 5\nM 1 3\nm 5 6\ns 3 6\nU 1 7\nS 1 2\n", "output": "21\n5\n6\n5\n2\n", "type": "stdin_stdout" } ] }
{ "difficulty": "interview", "problem_url": "https://www.codechef.com/CRES2016/problems/CRES104" }
vfc_1610
apps
verifiable_code
693
Solve the following coding problem using the programming language python: Write a program to find the factorial value of any number entered by the user. -----Input----- The first line contains an integer T, the total number of testcases. Then T lines follow, each line contains an integer N. -----Output----- For each test case, display the factorial of the given number N in a new line. -----Constraints----- - 1 ≤ T ≤ 1000 - 0 ≤ N ≤ 20 -----Example----- Input 3 3 4 5 Output 6 24 120 The input will be stdin and you should print your solution to stdout Now solve the problem and return the code.
```python # cook your dish here x=int(input()) for i in range(x): s=int(input()) fact=1 for i in range(1,s+1): fact=fact*i print(fact) ```
{ "language": "python", "test_cases": [ { "input": "3\n3\n4\n5\n", "output": "6\n24\n120\n", "type": "stdin_stdout" } ] }
{ "difficulty": "interview", "problem_url": "https://www.codechef.com/problems/FLOW018" }
vfc_1614
apps
verifiable_code
694
Solve the following coding problem using the programming language python: The MarkiT online virtual market startup wants to organize its grand opening in NIT Patna. but they want maximum crowd for their inauguration. So the manager told this to Praveen a student in NITP who suggested them: The first-year students come to campus every x hour, Second-year students come to campus every y hour, Third-year students come to campus every z hour and Fourth-year is very busy so they don't come regularly. So Praveen being very clever told him the no of times in n days he can have an audience of all year student (1st,2nd & 3rd) at max. So can you code what Praveen has done? -----Input:----- - First line will contain $T$, number of testcases. Then the testcases follow. - Each testcase contains of a 2 line of input, first line contain one integers $N$ (No of Days). -Next line contain 3 space separated integer the value of x y z -----Output:----- For each testcase, output in a single line answer the no of times audience consists of all year. -----Constraints----- - $1 \leq T \leq 1000$ - $1 \leq N \leq 10^8$ - $1 \leq x,y,z \leq 10^5$ -----Sample Input:----- 1 10 8 10 6 -----Sample Output:----- 2 -----EXPLANATION:----- First favourable condition will come on 5th day and Second on 10th day. The input will be stdin and you should print your solution to stdout Now solve the problem and return the code.
```python import math def fun(num1,num2): if num1>num2: a=num1 b=num2 else: a=num2 b=num1 rem=a%b while(rem!=0): a=b b=rem rem=a%b gcd=b return (int((num1*num2)/gcd)) for _ in range (int(input())): hours=int(input())*24 x,y,z=list(map(int,input().split())) lcm=x lcm=fun(x,y) lcm=fun(lcm,z) print(int(hours//lcm)) ```
{ "language": "python", "test_cases": [ { "input": "1\n10\n8 10 6\n", "output": "2\n", "type": "stdin_stdout" } ] }
{ "difficulty": "interview", "problem_url": "https://www.codechef.com/STRT2020/problems/CRWDCN" }
vfc_1618
apps
verifiable_code
695
Solve the following coding problem using the programming language python: You are given three non-negative integers $X$, $Y$ and $N$. Find the number of integers $Z$ such that $0 \le Z \le N$ and $(X \oplus Z) < (Y \oplus Z)$, where $\oplus$ denotes the bitwise XOR operation. -----Input----- - 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 three space-separated integers $X$, $Y$ and $N$. -----Output----- For each test case, print a single line containing one integer ― the number of integers $Z$ which satisfy all conditions. -----Constraints----- - $1 \le T \le 1,000$ - $0 \le X, Y, N \le 2^{30} - 1$ -----Subtasks----- Subtask #1 (5 points): $X, Y, N \le 2^6 - 1$ Subtask #2 (95 points): original constraints -----Example Input----- 3 1 2 10 2 1 10 0 0 7 -----Example Output----- 6 5 0 The input will be stdin and you should print your solution to stdout Now solve the problem and return the code.
```python # cook your dish here tc=int(input()) for j in range(tc): ip=list(map(int,input().rstrip().split())) x=ip[0] y=ip[1] n=ip[2] cnt=0 if(x==y): print('0') continue ln=bin(x).replace("0b", "") rn=bin(y).replace("0b", "") ll=len(ln) rl=len(rn) #print(ln) #print(rn) if(ll==len(rn)): for i in range(ll): if(ln[i]!=rn[i]): ln=ln[i:] rn=rn[i:] break #print(ln) if(ln[0]=='0'): ln=ln[1:] ll-=1 #print(rn) if(rn[0]=='0'): rn=rn[1:] rl-=1 ll=len(ln) rl=len(rn) if(ll>rl): lb=ll else: lb=rl pl=2**lb hpl=pl//2 amn=((n+1)//pl)*hpl rm=(n+1)%pl if((rm*2)<=pl): amn+=rm else: amn+=hpl #print("amn = ",amn) aln=(n+1)-amn #print("aln = ",aln) if(x<y): print(amn) else: print(aln) ```
{ "language": "python", "test_cases": [ { "input": "3\n1 2 10\n2 1 10\n0 0 7\n", "output": "6\n5\n0\n", "type": "stdin_stdout" } ] }
{ "difficulty": "interview", "problem_url": "https://www.codechef.com/problems/XORCOMP" }
vfc_1622
apps
verifiable_code
696
Solve the following coding problem using the programming language python: You are given a permutation of natural integers from 1 to N, inclusive. Initially, the permutation is 1, 2, 3, ..., N. You are also given M pairs of integers, where the i-th is (Li Ri). In a single turn you can choose any of these pairs (let's say with the index j) and arbitrarily shuffle the elements of our permutation on the positions from Lj to Rj, inclusive (the positions are 1-based). You are not limited in the number of turns and you can pick any pair more than once. The goal is to obtain the permutation P, that is given to you. If it's possible, output "Possible", otherwise output "Impossible" (without quotes). -----Input----- The first line of the input contains an integer T denoting the number of test cases. The description of T test cases follows. The first line of each test case contains two space separated integers N and M denoting the size of the permutation P and the number of pairs described above. The next line contains N integers - the permutation P. Each of the following M lines contain pair of integers Li and Ri. -----Output----- For each test case, output a single line containing the answer to the corresponding test case. -----Constraints----- - 1 ≤ T ≤ 35 - 1 ≤ N, M ≤ 100000 - 1 ≤ Li ≤ Ri ≤ N -----Example----- Input: 2 7 4 3 1 2 4 5 7 6 1 2 4 4 6 7 2 3 4 2 2 1 3 4 2 4 2 3 Output: Possible Impossible The input will be stdin and you should print your solution to stdout Now solve the problem and return the code.
```python t=int(input()) for _ in range(t): n,m=list(map(int,input().split())) l=list(map(int,input().split())) k=[] for i in range(m): a,b=list(map(int,input().split())) k.append([a,b]) k.sort() c=[] flag=1 x=k[0][0] y=k[0][1] for i in k[1:]: if i[0]<=y: y=max(y,i[1]) else: c.append([x-1,y-1]) x=i[0] y=i[1] c.append([x-1,y-1]) m=[] j=0 for i in c: while j<i[0]: m.append(l[j]) j+=1 x=l[i[0]:i[1]+1] m+=sorted(x) j=i[1]+1 while j<n: m.append(l[j]) j+=1 if m==sorted(l): print('Possible') else: print('Impossible') ```
{ "language": "python", "test_cases": [ { "input": "2\n7 4\n3 1 2 4 5 7 6\n1 2\n4 4\n6 7\n2 3\n4 2\n2 1 3 4\n2 4\n2 3\n", "output": "Possible\nImpossible\n", "type": "stdin_stdout" } ] }
{ "difficulty": "interview", "problem_url": "https://www.codechef.com/problems/PERMSUFF" }
vfc_1626
apps
verifiable_code
698
Solve the following coding problem using the programming language python: For years you have been working hard in Santa's factory to manufacture gifts for kids in substandard work environments with no pay. You have finally managed to escape the factory and now you seek revenge. You are planning a heist with the Grinch to steal all the gifts which are locked in a safe. Since you have worked in the factory for so many years, you know how to crack the safe. The passcode for the safe is an integer. This passcode keeps changing everyday, but you know a way to crack it. You will be given two numbers A and B . Passcode is the number of X such that 0 ≤ X < B and gcd(A,B) = gcd(A+X,B). Note : gcd(A,B) is the greatest common divisor of A & B. -----Input----- The first line contains the single integer T (1 ≤ T ≤ 50) — the number of test cases. Next T lines contain test cases per line. Each line contains two integers A & B ( 1 ≤ A < B ≤ 1010 ) -----Output----- Print T integers, one for each Test case. For each test case print the appropriate passcode for that day. -----Sample Input----- 3 4 9 5 10 42 9999999967 -----Output----- 6 1 9999999966 The input will be stdin and you should print your solution to stdout Now solve the problem and return the code.
```python import math t = int(input()) def phi(n): res = n i = 2 while i*i<=n: if n%i==0: res/=i res*=(i-1) while n%i==0: n/=i i+=1 if n>1: res/=n res*=(n-1) return int(res) while t: a,m = list(map(int,input().split())) g = math.gcd(a,m) print(phi(m//g)) t-=1 ```
{ "language": "python", "test_cases": [ { "input": "3\n4 9\n5 10\n42 9999999967\n", "output": "6\n1\n9999999966\n", "type": "stdin_stdout" } ] }
{ "difficulty": "interview", "problem_url": "https://www.codechef.com/NQST2020/problems/HEIST101" }
vfc_1634
apps
verifiable_code
699
Solve the following coding problem using the programming language python: Chef wants to host some Division-3 contests. Chef has $N$ setters who are busy creating new problems for him. The $i^{th}$ setter has made $A_i$ problems where $1 \leq i \leq N$. A Division-3 contest should have exactly $K$ problems. Chef wants to plan for the next $D$ days using the problems that they have currently. But Chef cannot host more than one Division-3 contest in a day. Given these constraints, can you help Chef find the maximum number of Division-3 contests that can be hosted in these $D$ days? -----Input:----- - The first line of input contains a single integer $T$ denoting the number of test cases. The description of $T$ test cases follows. - The first line of each test case contains three space-separated integers - $N$, $K$ and $D$ respectively. - The second line of each test case contains $N$ space-separated integers $A_1, A_2, \ldots, A_N$ respectively. -----Output:----- For each test case, print a single line containing one integer ― the maximum number of Division-3 contests Chef can host in these $D$ days. -----Constraints----- - $1 \leq T \leq 10^3$ - $1 \leq N \leq 10^2$ - $1 \le K \le 10^9$ - $1 \le D \le 10^9$ - $1 \le A_i \le 10^7$ for each valid $i$ -----Subtasks----- Subtask #1 (40 points): - $N = 1$ - $1 \le A_1 \le 10^5$ Subtask #2 (60 points): Original constraints -----Sample Input:----- 5 1 5 31 4 1 10 3 23 2 5 7 20 36 2 5 10 19 2 3 3 300 1 1 1 -----Sample Output:----- 0 2 7 4 1 -----Explanation:----- - Example case 1: Chef only has $A_1 = 4$ problems and he needs $K = 5$ problems for a Division-3 contest. So Chef won't be able to host any Division-3 contest in these 31 days. Hence the first output is $0$. - Example case 2: Chef has $A_1 = 23$ problems and he needs $K = 10$ problems for a Division-3 contest. Chef can choose any $10+10 = 20$ problems and host $2$ Division-3 contests in these 3 days. Hence the second output is $2$. - Example case 3: Chef has $A_1 = 20$ problems from setter-1 and $A_2 = 36$ problems from setter-2, and so has a total of $56$ problems. Chef needs $K = 5$ problems for each Division-3 contest. Hence Chef can prepare $11$ Division-3 contests. But since we are planning only for the next $D = 7$ days and Chef cannot host more than $1$ contest in a day, Chef cannot host more than $7$ contests. Hence the third output is $7$. The input will be stdin and you should print your solution to stdout Now solve the problem and return the code.
```python for T in range(int (eval(input()))): N,K,D=list(map(int,input().split())) A=list(map(int,input().split())) P=sum(A)//K print(min(P,D)) ```
{ "language": "python", "test_cases": [ { "input": "5\n1 5 31\n4\n1 10 3\n23\n2 5 7\n20 36\n2 5 10\n19 2\n3 3 300\n1 1 1\n", "output": "0\n2\n7\n4\n1\n", "type": "stdin_stdout" } ] }
{ "difficulty": "interview", "problem_url": "https://www.codechef.com/problems/DIVTHREE" }
vfc_1638
apps
verifiable_code
700
Solve the following coding problem using the programming language python: Now-a-days, Manish is becoming famous for bank robbery in the country because of his cleverness, he never robs own his own.He has four workers A , B, C, and D , all working under him.All of the four take some amount for that. There are total N banks in the country and Manish wants to rob all the banks with minimum amount spent on workers.Only one worker is allowed to rob the ith bank but following a condition that if one worker robs ith bank, then he can't rob (i+1) th bank. So ,Manish wants you to calculate minimum amount that he has to spend on all workers to rob all N banks. -----Input Format----- First line consists number of test cases T.For each test case, first line consists an integer N, number of banks in the country and the next N line consists 4 integers amount taken by A,B,C and D respectively to rob ith bank. -----Output format----- For each test case ,output an integer, minimum amount paid by Manish in separate line . -----Constraint----- - 1<=T<=1000 - 1<=N<=100000 - 1<=amount<=100000 -----Subtasks----- Subtask-1 (10 points) - 1<=T<=5 - 1<=N<=10 - 1<=amount<=10 Subtask-1 (30 points) - 1<=T<=100 - 1<=N<=1000 - 1<=amount<=1000 Subtask-1 (60 points) Original Constraints Sample Input 1 3 4 7 2 9 5 6 4 7 2 6 4 3 Sample Output 10 The input will be stdin and you should print your solution to stdout Now solve the problem and return the code.
```python for t in range(int(input())): n = int(input()) l = [] m = [] x = list(map(int,input().split())) l.append(x) m.append(list(x)) for i in range(1,n): x = list(map(int,input().split())) l.append(x) temp = [] for i in range(4): temp.append (x[i]+min(m[-1][:i]+m[-1][i+1:])) m.append(temp) print(min(m[-1])) ```
{ "language": "python", "test_cases": [ { "input": "1\n3\n4 7 2 9\n5 6 4 7\n2 6 4 3\n", "output": "10\n", "type": "stdin_stdout" } ] }
{ "difficulty": "interview", "problem_url": "https://www.codechef.com/ICOD2016/problems/ICODE16D" }
vfc_1642
apps
verifiable_code
701
Solve the following coding problem using the programming language python: You are given a sequence of N$N$ powers of an integer k$k$; let's denote the i$i$-th of these powers by kAi$k^{A_i}$. You should partition this sequence into two non-empty contiguous subsequences; each element of the original sequence should appear in exactly one of these subsequences. In addition, the product of (the sum of elements of the left subsequence) and (the sum of elements of the right subsequence) should be maximum possible. Find the smallest position at which you should split this sequence in such a way that this product is maximized. -----Input----- - The first line of the input contains a single integer T$T$ denoting the number of test cases. The description of T$T$ test cases follows. - The first line of each test case contains two space-separated integers N$N$ and k$k$. - The second line contains N$N$ space-separated integers A1,A2,…,AN$A_1, A_2, \dots, A_N$. -----Output----- For each test case, print a single line containing one integer — the size of the left subsequence. If there is more than one possible answer, print the smallest possible one. -----Constraints----- - 1≤T≤10$1 \le T \le 10$ - 2≤N≤105$2 \le N \le 10^5$ - 2≤k≤109$2 \le k \le 10^9$ - 0≤Ai≤105$0 \le A_i \le 10^5$ for each valid i$i$ -----Subtasks----- Subtask #1 (30 points): - 2≤N≤1,000$2 \le N \le 1,000$ - 0≤Ai≤1,000$0 \le A_i \le 1,000$ for each valid i$i$ Subtask #2 (70 points): original constraints -----Example Input----- 1 5 2 1 1 3 3 5 -----Example Output----- 4 -----Explanation----- Example case 1: The actual sequence of powers is [21,21,23,23,25]=[2,2,8,8,32]$[2^1, 2^1, 2^3, 2^3, 2^5] = [2, 2, 8, 8, 32]$. The maximum product is 20⋅32=640$20 \cdot 32 = 640$. In the optimal solution, the sequence is partitioned into [2,2,8,8]$[2, 2, 8, 8]$ and [32]$[32]$. The input will be stdin and you should print your solution to stdout Now solve the problem and return the code.
```python # cook your dish here def main(): for _ in range(int(input())): N, k = [int(x) for x in input().split()] Powers = [k ** int(x) for x in input().split()] s1, s2 = 0, sum(Powers) ans = (0, None) i = 0 while i < N - 1: s1 += Powers[i] s2 -= Powers[i] z = s1 * s2 if z > ans[0]: ans = (z, i) # print(z) i += 1 print(ans[1] + 1) main() ```
{ "language": "python", "test_cases": [ { "input": "1\n5 2\n1 1 3 3 5\n", "output": "4\n", "type": "stdin_stdout" } ] }
{ "difficulty": "interview", "problem_url": "https://www.codechef.com/problems/MXM" }
vfc_1646
apps
verifiable_code
702
Solve the following coding problem using the programming language python: This is a peculiar functioning setup. Two Tanks are separated from each other by a wall .There is a pipe in the wall which connects both tanks which allows flow of water between them . Due to this ,there is change in temperature of both tanks , every minute temperature of Tank with larger temperature among two decreases by one and temperature of Tank with smaller temperature among two increases by two until equilibrium is reached , But there is a problem . The pipe can't control this flow of water if there is Non-equilibrium (inequality of temperature on both sides ) even after $m$ minutes and the pipe will burst after it , your task is to predict whether the pipe will burst or not . Note: If equilibrium cannot be reached the process will continue forever. The initial temperature of Cold Tank is $Tc$ , of Hot Tank it is $Th$. -----Input:----- - First line will contain $T$, number of testcases. Then the testcases follow. - Each testcase contains of a single line of input, three integers $m, Tc,Th$. -----Output:----- For each testcase, output in a single line answer "Yes" if Pipe will burst after m minutes "No" if pipe will not burst. -----Constraints----- - $1 \leq T \leq 100000$ - $1 \leq m,Tc,Th \leq 10^9$ - $Tc \leq Th $ -----Sample Input:----- 2 4 5 10 2 2 5 -----Sample Output:----- Yes No The input will be stdin and you should print your solution to stdout Now solve the problem and return the code.
```python for i in range(int(input())): m,tc,th=map(int,input().split()) x=(th-tc) if x%3!=0: print("Yes") else: if (x//3)<=m: print("No") else: print("Yes") ```
{ "language": "python", "test_cases": [ { "input": "2\n4 5 10\n2 2 5\n", "output": "Yes\nNo\n", "type": "stdin_stdout" } ] }
{ "difficulty": "interview", "problem_url": "https://www.codechef.com/COCA2020/problems/HOTNCOLD" }
vfc_1650
apps
verifiable_code
703
Solve the following coding problem using the programming language python: Chef gives an integer $K$ in the input. If the given number is beautiful binary number, print it, Else find its previous beautiful binary number. A beautiful binary number is a number whose binary representation does not contain any consecutive 1s. Note: 1 is also a beautiful binary number. -----Input:----- - First-line will contain $T$, the number of test cases. Then the test cases follow. - Each test case contains a single line of input, one integer $K$. -----Output:----- For each test case, print a beautiful number. -----Constraints----- - $1 \leq T \leq 10^5$ - $1 \leq K \leq 10^5$ -----Sample Input:----- 3 3 6 8 -----Sample Output:----- 2 5 8 -----EXPLANATION:----- For 1) 3 is not a beautiful binary number because the binary representation of 3 is "11" which has consecutive 1s. hence 2 which is less than 3 is printed. For 3) 8 is already a beautiful binary number with no consecutive 1s in its binary representation. so, print 8 as it is. The input will be stdin and you should print your solution to stdout Now solve the problem and return the code.
```python pref = [] for i in range(10 ** 5 + 10): b = bin(i)[2:] if not any(b[j] == b[j+1] == '1' for j in range(len(b) - 1)): pref.append(i) else: pref.append(pref[-1]) for i in range(int(input())): print(pref[int(input())]) ```
{ "language": "python", "test_cases": [ { "input": "3\n3\n6\n8\n", "output": "2\n5\n8\n", "type": "stdin_stdout" } ] }
{ "difficulty": "interview", "problem_url": "https://www.codechef.com/PBK02020/problems/ITGUY10" }
vfc_1654
apps
verifiable_code
704
Solve the following coding problem using the programming language python: Eugene has to do his homework. But today, he is feeling very lazy and wants to you do his homework. His homework has the following given maths problem. You are given three integers: A, N, M. You write the number A appended to itself N times in a row. Let's call the resulting big number X. For example, if A = 120, N = 3, then X will be 120120120. Find out the value of X modulo M. -----Input----- The first line of the input contains an integer T denoting the number of test cases. The description of T test cases follow. Each test case is described in one line containing three integers: A, N and M as described in the problem statement. -----Output----- For each test case, output a single line containing an integer denoting the value of X modulo M. -----Constraints----- - 1 ≤ T ≤ 105 - 0 ≤ A ≤ 109 - 1 ≤ N ≤ 1012 - 2 ≤ M ≤ 109 -----Subtasks----- Subtask #1 (15 points): - 0 ≤ A ≤ 100 - 1 ≤ N ≤ 105 Subtask #2 (25 points): - 1 ≤ N ≤ 109 Subtask #3 (60 points): - Original constraints -----Example----- Input: 2 12 2 17 523 3 11 Output: 5 6 -----Explanation----- Example 1: As A = 12, N = 2, X = 1212, 1212 modulo 17 = 5 Example 2. As A = 523, N = 3, X = 523523523, 523523523 modulo 11 = 6 The input will be stdin and you should print your solution to stdout Now solve the problem and return the code.
```python # cook your dish here for _ in range(int(input())): a,n,m = map(int,input().split(' ')) s = len(str(a)) #print(s) c = 10**s - 1 w = c*m b = pow(10,n*s,w)-1 d = b//c ans = (d%m)*(a%m) print(ans%m) ```
{ "language": "python", "test_cases": [ { "input": "2\n12 2 17\n523 3 11\n\n\n", "output": "5\n6\n", "type": "stdin_stdout" } ] }
{ "difficulty": "interview", "problem_url": "https://www.codechef.com/problems/KBIGNUMB" }
vfc_1658
apps
verifiable_code
705
Solve the following coding problem using the programming language python: A city of dimension N x N is constructed with grid of lanes. These lanes are fenced by government so that no one can cross any grid diagonally. Although a train line runs diagonally from (0,0) to (N,N). Our chef has a weird kind of phobia and is very afraid to cross the railway line. He is at point (0,0) and wants to get to the point (N,N). Calculate number of path through which it is possible to reach to its destination travelling the minimum distance. . Note that: 1. Since he is already at position (0,0) he can go to either part of grid (i.e. left or right part - divided by diagonal) but he will remain in that part for the whole path. 2. He is only afraid to "cross" the line, i.e. during the route he can go to position (m,m) where 0 3. You have to calculate the number of path possible. If there is more than one path then you have to print the number of path of minimum distances. -----Input----- The first line of the input contains an integer T denoting the number of test cases, for each test case enter the grid size i.e value of N. -----Output----- For each test case, output a single line with number of such paths possible. (Note : If no such path possible print 0) -----Constraints----- - 1 <= T <= 100 - 0 <= N <= 30 -----Example----- Input: 2 2 5 Output: 4 84 The input will be stdin and you should print your solution to stdout Now solve the problem and return the code.
```python ar = [] ar.append(1) for i in range(1, 31): ar.append(ar[i-1]*(4*i-2)/(i+1)) t = int(input()) while(t>0): n = int(input()) if(n==0): print(0) else: print(ar[n]*2) t=t-1 ```
{ "language": "python", "test_cases": [ { "input": "2\n2\n5\n", "output": "4\n84\n", "type": "stdin_stdout" } ] }
{ "difficulty": "interview", "problem_url": "https://www.codechef.com/ALGT2013/problems/TNMALG02" }
vfc_1662
apps
verifiable_code
706
Solve the following coding problem using the programming language python: Chef has $N$ small boxes arranged on a line from $1$ to $N$. For each valid $i$, the weight of the $i$-th box is $W_i$. Chef wants to bring them to his home, which is at the position $0$. He can hold any number of boxes at the same time; however, the total weight of the boxes he's holding must not exceed K at any time, and he can only pick the ith box if all the boxes between Chef's home and the ith box have been either moved or picked up in this trip. Therefore, Chef will pick up boxes and carry them home in one or more round trips. Find the smallest number of round trips he needs or determine that he cannot bring all boxes home. -----Input----- - 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 line of each test case contains two space-separated integers $N$ and $K$. - The second line contains $N$ space-separated integers $W_1, W_2, \ldots, W_N$. -----Output----- For each test case, print a single line containing one integer ― the smallest number of round trips or $-1$ if it is impossible for Chef to bring all boxes home. -----Constraints----- - $1 \le T \le 100$ - $1 \le N, K \le 10^3$ - $1 \le W_i \le 10^3$ for each valid $i$ -----Example Input----- 4 1 1 2 2 4 1 1 3 6 3 4 2 3 6 3 4 3 -----Example Output----- -1 1 2 3 -----Explanation----- Example case 1: Since the weight of the box higher than $K$, Chef can not carry that box home in any number of the round trip. Example case 2: Since the sum of weights of both boxes is less than $K$, Chef can carry them home in one round trip. Example case 3: In the first round trip, Chef can only pick up the box at position $1$. In the second round trip, he can pick up both remaining boxes at positions $2$ and $3$. Example case 4: Chef can only carry one box at a time, so three round trips are required. The input will be stdin and you should print your solution to stdout Now solve the problem and return the code.
```python t=int(input()) for i in range(t): x,y=0,0 n,m=list(map(int,input().split())) l=list(map(int,input().split())) if(max(l)>m): print(-1) else: for i in range(len(l)): y+=l[i] if(y>m): y=l[i] x+=1 if(y>0): x+=1 print(x) ```
{ "language": "python", "test_cases": [ { "input": "4\n1 1\n2\n2 4\n1 1\n3 6\n3 4 2\n3 6\n3 4 3\n", "output": "-1\n1\n2\n3\n", "type": "stdin_stdout" } ] }
{ "difficulty": "interview", "problem_url": "https://www.codechef.com/problems/CHEFNWRK" }
vfc_1666
apps
verifiable_code
708
Solve the following coding problem using the programming language python: You are given a square matrix $M$ with $N$ rows (numbered $1$ through $N$) and $N$ columns (numbered $1$ through $N$). Initially, all the elements of this matrix are equal to $A$. The matrix is broken down in $N$ steps (numbered $1$ through $N$); note that during this process, some elements of the matrix are simply marked as removed, but all elements are still indexed in the same way as in the original matrix. For each valid $i$, the $i$-th step consists of the following: - Elements $M_{1, N-i+1}, M_{2, N-i+1}, \ldots, M_{i-1, N-i+1}$ are removed. - Elements $M_{i, N-i+1}, M_{i, N-i+2}, \ldots, M_{i, N}$ are removed. - Let's denote the product of all $2i-1$ elements removed in this step by $p_i$. Each of the remaining elements of the matrix (those which have not been removed yet) is multiplied by $p_i$. Find the sum $p_1 + p_2 + p_3 + \ldots + p_N$. Since this number could be very large, compute it modulo $10^9+7$. -----Input----- - 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 $N$ and $A$. -----Output----- For each test case, print a single line containing one integer ― the sum of products at each step modulo $10^9+7$. -----Constraints----- - $1 \le T \le 250$ - $1 \le N \le 10^5$ - $0 \le A \le 10^9$ - the sum of $N$ over all test cases does not exceed $10^5$ -----Example Input----- 1 3 2 -----Example Output----- 511620149 -----Explanation----- Example case 1: The input will be stdin and you should print your solution to stdout Now solve the problem and return the code.
```python # cook your dish here for _ in range(int(input())): n,k = list(map(int,input().split())) mod = 10**9+7 s=0 for i in range(1,n+1): p = pow(k,(2*i)-1,mod) # print(p) s=(s+p)%mod # print(k) k = (p*k)%mod print(s) ```
{ "language": "python", "test_cases": [ { "input": "1\n3 2\n", "output": "511620149\n", "type": "stdin_stdout" } ] }
{ "difficulty": "interview", "problem_url": "https://www.codechef.com/problems/MATBREAK" }
vfc_1674
apps
verifiable_code
709
Solve the following coding problem using the programming language python: $Gogi$, $Tapu$ and $Sonu$ are the elite members of $Tapu$ $Sena$. $Gogi$ is always stoned and asks absurd questions, But this time he asked a question which seems to be very serious and interesting. $Tapu$ wants to solve this question to impress $Sonu$. He gave an array of length N to $Tapu$, $Tapu$ can perform the following operations exactly once: - Remove any subarray from the given array given the resulting array formed after the removal is non-empty. - Reverse the whole array. Remember you can’t shuffle the elements of the array. Tapu needs to find out the maximum possible GCD of all the numbers in the array after applying the given operations exactly once. Tapu is very weak at programming, he wants you to solve this problem so that he can impress $Sonu$. -----Input:----- - The first line contains $T$, the number of test cases. - For each test case -FIrst line contains $N$. - Last line contains $N$ numbers of the array. -----Output:----- A single integer in a new line, maximum possible GCD. -----Constraints----- - $1 \leq T \leq 10^2$ - $1 \leq N \leq 10^4$ - $1 \leq a[i] \leq 10^9$ Summation of N for all testcases is less than $10^6$ -----Sample Input 1:----- 1 1 2 -----Sample Output 1:----- 2 The input will be stdin and you should print your solution to stdout Now solve the problem and return the code.
```python # cook your dish here try: t = int(input()) for _ in range(t): n = int(input()) a = list(map(int, input().split())) gcd = max(a[0], a[-1]) print(gcd) except EOFError:pass ```
{ "language": "python", "test_cases": [ { "input": "1\n1\n2\n", "output": "2\n", "type": "stdin_stdout" } ] }
{ "difficulty": "interview", "problem_url": "https://www.codechef.com/CORE2020/problems/CORE2002" }
vfc_1678
apps
verifiable_code
710
Solve the following coding problem using the programming language python: Blob is a computer science student. He recently got an internship from Chef's enterprise. Along with the programming he has various other skills too like graphic designing, digital marketing and social media management. Looking at his skills Chef has provided him different tasks A[1…N] which have their own scores. Blog wants to maximize the value of the expression A[d]-A[c]+A[b]-A[a] such that d>c>b>a. Can you help him in this? -----Input:----- - The first line contain the integer N - The second line contains N space separated integers representing A[1], A[2] … A[N] -----Output:----- The maximum score that is possible -----Constraints----- - $4 \leq N \leq 10^4$ - $0 \leq A[i] \leq 10^5$ -----Sample Input:----- 6 3 9 10 1 30 40 -----Sample Output:----- 46 The input will be stdin and you should print your solution to stdout Now solve the problem and return the code.
```python def maxval(arr): fn = [float('-inf')]*(len(arr)+1) sn = [float('-inf')]*len(arr) tn = [float('-inf')]*(len(arr)-1) fon = [float('-inf')]*(len(arr)-2) for i in reversed(list(range(len(arr)))): fn[i] = max(fn[i + 1], arr[i]) for i in reversed(list(range(len(arr) - 1))): sn[i] = max(sn[i + 1], fn[i + 1] - arr[i]) for i in reversed(list(range(len(arr) - 2))): tn[i] = max(tn[i + 1], sn[i + 1] + arr[i]) for i in reversed(list(range(len(arr) - 3))): fon[i] = max(fon[i + 1], tn[i + 1] - arr[i]) return fon[0] n = int(input()) arr = list(map(int,input().split())) print(maxval(arr)) ```
{ "language": "python", "test_cases": [ { "input": "6\n3 9 10 1 30 40\n", "output": "46\n", "type": "stdin_stdout" } ] }
{ "difficulty": "interview", "problem_url": "https://www.codechef.com/WNTR2020/problems/WC04" }
vfc_1682
apps
verifiable_code
711
Solve the following coding problem using the programming language python: The notorious hacker group "Sed" managed to obtain a string $S$ from their secret sources. The string contains only lowercase English letters along with the character '?'. A substring of $S$ is a contiguous subsequence of that string. For example, the string "chef" is a substring of the string "codechef", but the string "codeh" is not a substring of "codechef". A substring of $S$ is good if it is possible to choose a lowercase English letter $C$ such that the following process succeeds: - Create a string $R$, which is a copy of the substring, but with each '?' replaced by the letter $c$. Note that all occurrences of '?' must be replaced by the same letter. - For each lowercase English letter: - Compute the number of times it occurs in $S$ and the number of times it occurs in $R$; let's denote them by $A$ and $B$ respectively. The '?' characters in the original string $S$ are ignored when computing $A$. - Check the parity of $A$ and $B$. If they do not have the same parity, i.e. one of them is even while the other is odd, then this process fails. - If the parities of the number of occurrences in $S$ and $R$ are the same for each letter, the process succeeds. For different substrings, we may choose different values of $C$. Help Sed find the number of good substrings in the string $S$. -----Input----- - 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 a single string $S$. -----Output----- For each test case, print a single line containing one integer ― the number of good substrings. -----Constraints----- - $1 \le T \le 10^5$ - $1 \le S \le 10^5$ - $S$ contains only lowercase English letters ('a' through 'z') and '?' - the sum of $|S|$ over all test cases does not exceed $10^5$ -----Example Input----- 5 aa? a??? ???? asfhaslskfak af??avvnfed?fav?faf???? -----Example Output----- 2 6 4 2 27 -----Explanation----- Example case 1: All letters occur an even number of times in $S$. The six substrings of $S$ are "a", "a", "?", "aa", "a?" and "aa?" (note that "a" is counted twice). Which of them are good? - "a" is not good as 'a' occurs an odd number of times in this substring and there are no '?' to replace. - "?" is also not good as replacing '?' by any letter causes this letter to occur in $R$ an odd number of times. - "aa" is a good substring because each letter occurs in it an even number of times and there are no '?' to replace. - "a?" is also a good substring. We can replace '?' with 'a'. Then, $R$ is "aa" and each letter occurs in this string an even number of times. Note that replacing '?' e.g. with 'b' would not work ($R$ would be "ab", where both 'a' and 'b' occur an odd number of times), but we may choose the replacement letter $C$ appropriately. - "aa?" is not a good substring. For any replacement letter $C$, we find that $C$ occurs an odd number of times in $R$. Example case 2: We especially note that "a???" is not a good substring. Since all '?' have to be replaced by the same character, we can only get strings of the form "aaaa", "abbb", "accc", etc., but none of them match the criteria for a good substring. Example case 3: Any substring with even length is good. The input will be stdin and you should print your solution to stdout Now solve the problem and return the code.
```python def convertToParitys(s): """ This converts the string s to an int, which is a bitMap of the parity of each letter odd ? = first bit set odd a = second bit set odd b = third bit set etc """ keys = '?abcdefghijklmnopqrstuvwxyz' paritys = {c:0 for c in keys} for c in s: paritys[c] += 1 for c, v in paritys.items(): paritys[c] = v%2 out = 0 bitValue = 1 for c in keys: if paritys[c]: out += bitValue bitValue *= 2 return out def getSolutionBitMaps(s): """ these are the 27 valid bitmaps that a substring can have even ? and the parities the same 26 cases of odd ? and one bit different in the parity compared to s """ out = [] sP = convertToParitys(s) if sP%2: sP -= 1 # to remove the '?' parity #even case - out.append(sP) #odd cases - need to xor sP with 1 + 2**n n = 1 to 26 inc to flip ? bit and each of the others for n in range(1,27): out.append(sP^(1+2**n)) return out def getLeadingSubStringBitMapCounts(s): """ This calculates the bit map of each of the len(s) substrings starting with the first character and stores as a dictionary. Getting TLE calculating each individually, so calculating with a single pass """ out = {} bM = 0 keys = '?abcdefghijklmnopqrstuvwxyz' paritys = {c:0 for c in keys} values = {c:2**i for i,c in enumerate(keys)} out[bM] = out.setdefault(bM, 0) + 1 #add the null substring bMis = [] i = 0 bMis = [0] for c in s: i += 1 if paritys[c]: paritys[c] = 0 bM -= values[c] else: paritys[c] = 1 bM += values[c] out[bM] = out.setdefault(bM, 0) + 1 bMis.append(bM) return out,bMis def solve(s): out = 0 bMjCounts,bMis = getLeadingSubStringBitMapCounts(s) #print('bMjCounts') #print(bMjCounts) solutions = getSolutionBitMaps(s) #print('solutions') #print(solutions) for bMi in bMis: for bMs in solutions: if bMs^bMi in bMjCounts: out += bMjCounts[bMs^bMi] #print(i,bMi,bMs,bMs^bMi,bMjCounts[bMs^bMi]) if 0 in solutions: out -= len(s) #remove all null substrings out //= 2 # since j may be less that i each substring is counted twice return out T = int(input()) for tc in range(T): s = input() print(solve(s)) ```
{ "language": "python", "test_cases": [ { "input": "5\naa?\na???\n????\nasfhaslskfak\naf??avvnfed?fav?faf????\n\n", "output": "2\n6\n4\n2\n27\n", "type": "stdin_stdout" } ] }
{ "difficulty": "interview", "problem_url": "https://www.codechef.com/problems/SEDPASS" }
vfc_1686
apps
verifiable_code
712
Solve the following coding problem using the programming language python: Chef got into a fight with the evil Dr Doof. Dr Doof has decided to destroy all even numbers from the universe using his Evil-Destroy-inator. Chef has $N$ integers with him. To stop Doof, Chef has to find an odd number which is an integer multiple of all $N$ numbers that he has with him. Find if it is possible for Chef to prevent Dr Doof from destroying the even numbers. Formally, given $N$ positive integers, find if there exists an odd number which is an integer multiple of all the given $N$ numbers. If yes, print "YES", otherwise "NO". You can print any letter in any case. -----Input----- - First line contains $T$, number of testcases. Each testcase consists of $2$ lines. - The first line of each test case consists of a positive integer $N$, denoting the number of positive integers Chef has. - The second line of each test case contains $N$ space separated integers $A_i$ each denoting an integer that Chef has with him. -----Output----- For every test case, if there exists such an odd number, print "YES" on a separate line, otherwise "NO". The judge is case insensitive. That means, your code can print any letter in any case ( "Yes", "yes" or "YES" are all accepted). -----Constraints----- - $1 \leq T \leq 10^3$ - $1 \leq N \leq 10^3$ - $1 \leq A_i \leq 10^3$ -----Sample Input----- 2 5 1 2 5 4 3 1 7 -----Sample Output----- NO YES -----Explanation----- For test $1$: There exists no odd number. For test $2$: The possible odd numbers can be $7$, $21$, $49$, $315$, … The input will be stdin and you should print your solution to stdout Now solve the problem and return the code.
```python def gcd(a,b): if b==0: return a return gcd(b,a%b) for _ in range(int(input())): n = int(input()) arr = list(map(int,input().split())) value = arr[0] if n!=1: for i in arr[1:]: value = value*i//gcd(value, i) if value%2==0: print("NO") else: print("YES") ```
{ "language": "python", "test_cases": [ { "input": "2\n5\n1 2 5 4 3\n1\n7\n", "output": "NO\nYES\n", "type": "stdin_stdout" } ] }
{ "difficulty": "interview", "problem_url": "https://www.codechef.com/COLE2020/problems/CLLCM" }
vfc_1690
apps
verifiable_code
715
Solve the following coding problem using the programming language python: Recently Rocky had participated in coding competition and he is sharing one of the problem with you which he was unable to solve. Help Rocky in solving the problem. Suppose the alphabets are arranged in a row starting with index 0$0$ from AtoZ$A to Z$. If in a coded language A=27$A=27$ and AND=65$AND=65$. Help Rocky to find a suitable formula for finding all the value for given test cases? (All alphabets are in Upper case only). -----Input:----- First line of the input contains string s$s$. -----Output:----- Output the possible integer values of the given string s$s$ according to the question . -----Constraints----- - 1≤s≤100$1 \leq s \leq 100$ -----Sample Input:----- A AND -----Sample Output:----- 27 65 The input will be stdin and you should print your solution to stdout Now solve the problem and return the code.
```python # cook your dish here s = input().strip() start_w = 27 w_dict = {} words = ["A", "B", "C", "D", "E", "F", "G", "H", "I", "J", "K", "L", "M", "N", "O", "P", "Q", "R", "S", "T", "U", "V", "W", "X", "Y", "Z"] for word in words: w_dict[word] = start_w start_w = start_w - 1 total_wt = 0 for c in s: total_wt = total_wt + w_dict[c] print(total_wt) ```
{ "language": "python", "test_cases": [ { "input": "A\nAND\n", "output": "27\n65\n", "type": "stdin_stdout" } ] }
{ "difficulty": "interview", "problem_url": "https://www.codechef.com/CFUN2020/problems/CODSTAN6" }
vfc_1702
apps
verifiable_code
716
Solve the following coding problem using the programming language python: Chef has created a special dividing machine that supports the below given operations on an array of positive integers. There are two operations that Chef implemented on the machine. Type 0 Operation Update(L,R): for i = L to R: a[i] = a[i] / LeastPrimeDivisor(a[i]) Type 1 Operation Get(L,R): result = 1 for i = L to R: result = max(result, LeastPrimeDivisor(a[i])) return result; The function LeastPrimeDivisor(x) finds the smallest prime divisor of a number. If the number does not have any prime divisors, then it returns 1. Chef has provided you an array of size N, on which you have to apply M operations using the special machine. Each operation will be one of the above given two types. Your task is to implement the special dividing machine operations designed by Chef. Chef finds this task quite easy using his machine, do you too? -----Input----- The first line of the input contains an integer T denoting the number of test cases. The description of T test cases follows. The first line of each test case contains two space-separated integers N, M, denoting the size of array A and the number of queries correspondingly. The second line of each test case contains N space-separated integers A1, A2, ..., AN denoting the initial array for dividing machine. Each of following M lines contain three space-separated integers type, L, R - the type of operation (0 - Update operation, 1 - Get operation), and the arguments of function, respectively -----Output----- For each test case, output answer of each query of type 1 (Get query) separated by space. Each test case from the same file should start from the new line. -----Constraints----- - 1 ≤ T ≤ 100 - 1 ≤ Ai ≤ 106 - 1 ≤ L ≤ R ≤ N - 0 ≤ type ≤ 1 - Sum of M over all test cases in a single test file does not exceed 106 -----Subtasks----- Subtask #1: (10 points) - 1 ≤ N, M ≤ 103 Subtask #2: (25 points) - 1 ≤ N, M ≤ 105 - Ai is a prime number. Subtask #3: (65 points) - 1 ≤ N, M ≤ 105 -----Example----- Input: 2 6 7 2 5 8 10 3 44 1 2 6 0 2 3 1 2 6 0 4 6 1 1 6 0 1 6 1 4 6 2 2 1 3 0 2 2 1 1 2 Output: 5 3 5 11 1 -----Explanation----- Example case 1.The states of array A after each Update-operation: A: = [2 1 4 10 3 44] A: = [2 1 4 5 1 22] A: = [1 1 2 1 1 11] The input will be stdin and you should print your solution to stdout Now solve the problem and return the code.
```python import math n=1001 a=[True]*n l=[] for i in range(2,33): if(a[i]): for j in range(i*i,n,i): a[j]=False for pr in range(2,1001): if(a[pr]): l.append(pr) t=int(input()) for j in range(t): n,m=list(map(int,input().strip().split())) arr=[int(num) for num in input().strip().split()] Matrix =[] index=[0]*100000 factors=[0]*100000 ans='' for r in range(len(arr)): li=[] for val in l: while((arr[r]%val)==0): arr[r]=arr[r]/val li.append(val) factors[r]+=1 if(arr[r]!=1): li.append(arr[r]) arr[r]=1 factors[r]+=1 Matrix.append(li) for k in range(m): opr=[int(o) for o in input().strip().split()] L=opr[1] R=opr[2] if(opr[0]==0): for ran in range(L-1,R): if(index[ran]<factors[ran]): index[ran]+=1 else: result=1 for ran in range(L-1,R): if(index[ran]<factors[ran]): result=max(result,Matrix[ran][index[ran]]) ans+=str(result) ans+=' ' print(ans[:-1]) ```
{ "language": "python", "test_cases": [ { "input": "2\n6 7\n2 5 8 10 3 44\n1 2 6\n0 2 3\n1 2 6\n0 4 6\n1 1 6\n0 1 6\n1 4 6\n2 2\n1 3\n0 2 2\n1 1 2\n", "output": "5 3 5 11\n1\n", "type": "stdin_stdout" } ] }
{ "difficulty": "interview", "problem_url": "https://www.codechef.com/SEPT16/problems/DIVMAC" }
vfc_1706
apps
verifiable_code
717
Solve the following coding problem using the programming language python: Today is rose day, batch mates of Kabir and Tara decided to celebrate this day by exchanging roses with each other. Note:$Note:$ exchanging means both the boy and the girl will give rose to each other. In the class there are B$B$ boys and G$G$ girls. Exchange of rose will take place if and only if at least one of them hasn't received a rose from anyone else and a rose can be exchanged only once. Tara has to bring maximum sufficient roses for everyone and is confused as she don't know how many roses to buy.You are a friend of Kabir, so help him to solve the problem so that he can impress Tara by helping her. -----Input:----- - First line will contain T$T$, number of test cases. - Each test case contains two space separated integers B$B$ (Number of boys) and G$G$ (Number of Girls). -----Output:----- For each test case, output in a single line the total number of roses exchanged. -----Constraints:----- - 1≤T≤105$1 \leq T \leq 10^5$ - 1≤B≤109$1 \leq B \leq 10^{9}$ - 1≤G≤109$1 \leq G \leq 10^{9}$ -----Sample Input:----- 1 2 3 -----Sample Output:----- 8 The input will be stdin and you should print your solution to stdout Now solve the problem and return the code.
```python for i in range(int(input())): print(2*(sum(list(map(int, input().split())))-1)) ```
{ "language": "python", "test_cases": [ { "input": "1\n2 3\n", "output": "8\n", "type": "stdin_stdout" } ] }
{ "difficulty": "interview", "problem_url": "https://www.codechef.com/HECS2020/problems/CC001" }
vfc_1710
apps
verifiable_code
719
Solve the following coding problem using the programming language python: -----Problem----- Suppose there is a circle. There are N Juice shops on that circle. Juice shops are numbered 0 to N-1 (both inclusive). You have two pieces of information corresponding to each of the juice shop: (1) the amount of Juice that a particular Juice shop can provide and (2) the distance from that juice shop to the next juice shop. Initially, there is a man with a bottle of infinite capacity carrying no juice. He can start the tour at any of the juice shops. Calculate the first point from where the man will be able to complete the circle. Consider that the man will stop at every Juice Shop. The man will move one kilometer for each litre of the juice. -----Input----- - The first line will contain the value of N. - The next N lines will contain a pair of integers each, i.e. the amount of juice that a juice shop can provide(in litres) and the distance between that juice shop and the next juice shop. -----Output----- An integer which will be the smallest index of the juice shop from which he can start the tour. -----Constraints----- - 1 ≤ N ≤ 105 - 1 ≤ amt of juice, distance ≤ 109 -----Sample Input----- 3 1 5 10 3 3 4 -----Sample Output----- 1 -----Explanation----- He can start the tour from the SECOND Juice shop. p { text-align:justify } The input will be stdin and you should print your solution to stdout Now solve the problem and return the code.
```python import re,sys def isCirlePossible(juices,distances): if juices == [] or distances == []: return -1; total_juice_consumed = 0 juice_consumed = 0 start=0 for i in range(0,len(juices)): diff = juices[i] - distances[i] if juice_consumed >= 0: juice_consumed += diff else: juice_consumed = diff start = i total_juice_consumed += diff return start juices = [] distances = [] numLines = int(input()) for each in range(0,numLines): line = input() result = [int(x) for x in re.findall('\d+',line)] if len(result) == 2: juices.append(result[0]) distances.append(result[1]) print(isCirlePossible(juices,distances)) return ```
{ "language": "python", "test_cases": [ { "input": "3\n1 5\n10 3\n3 4\n", "output": "1\n", "type": "stdin_stdout" } ] }
{ "difficulty": "interview", "problem_url": "https://www.codechef.com/COMN2016/problems/SUMTOUR" }
vfc_1718
apps
verifiable_code
720
Solve the following coding problem using the programming language python: All strings in Chefland are beautiful because they are binary strings (a binary string contains only characters '0' and '1'). The beauty of a binary string $S$ is defined as the number of pairs $(i, j)$ ($1 \le i \le j \le |S|$) such that the substring $S_i, S_{i+1}, \ldots, S_j$ is special. For a binary string $U$, let's denote the number of occurrences of the characters '1' and '0' in $U$ by $cnt_1$ and $cnt_0$ respectively; then, $U$ is special if $cnt_0 = cnt_1 \cdot cnt_1$. Today, Chef's friend Araspa is celebrating her birthday. Chef wants to give Araspa the most beautiful binary string he can find. Currently, he is checking out binary strings in a shop, but he needs your help to calculate their beauties. Tell Chef the beauty of each binary string he gives you. -----Input----- - 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 a single string $S$. -----Output----- For each test case, print a single line containing one integer — the beauty of the string $S$. -----Constraints----- - $1 \le T \le 10$ - $1 \le |S| \le 10^5$ - each character of $S$ is '0' or '1' -----Example Input----- 2 010001 10 -----Example Output----- 4 1 -----Explanation----- Example case 1: The special substrings correspond to $(i, j) = (1, 2), (1, 6), (2, 3), (5, 6)$. The input will be stdin and you should print your solution to stdout Now solve the problem and return the code.
```python t = int(input()) for _ in range(t): s = input() pref = [0]*len(s) if s[0]=="1": pref[0]+=1 for i in range(1,len(s)): if s[i]=="1": pref[i]+=1 pref[i]=pref[i]+pref[i-1] k=1 cnt=0 while (k+k*k)<=len(s): r = k+k*k i=r-1 while i<len(s): if (i-r)>=0: if pref[i]-pref[i-r]==k: cnt+=1 i+=1 else: i+=abs(k-(pref[i]-pref[i-r])) else: if pref[i]==k: cnt+=1 i+=1 else: i+=abs(k-(pref[i])) k+=1 print(cnt) ```
{ "language": "python", "test_cases": [ { "input": "2\n010001\n10\n", "output": "4\n1\n", "type": "stdin_stdout" } ] }
{ "difficulty": "interview", "problem_url": "https://www.codechef.com/problems/BDGFT" }
vfc_1722
apps
verifiable_code
722
Solve the following coding problem using the programming language python: The kingdom of the snakes is an NxN grid. Their most-valued possession is a huge collection of poison, which is stored in the central KxK grid. It is guaranteed that both N and K are odd. What we mean by 'central' is this: suppose in the NxN grid, (i, j) refers to the cell in the i-th row and j-th column and (1,1) refers to the top-left corner and (N,N) refers to the bottom-right corner. Then the poison is stored in the KxK square whose top-left corner is ( (N - K)/2 + 1, (N - K)/2 + 1 ). But there are thieves who want to steal the poison. They cannot enter the NxN grid, but they can shoot arrows from outside. These arrows travel across a row (from left to right, or right to left), or across a column (top to bottom or bottom to top) in a straight line. If the arrow enters the KxK grid, some of the poison will stick to the arrow, and if it exits the NxN grid after that, the thieves will have successfully stolen some of the poison. As the King of the snakes, you want to thwart these attempts. You know that the arrows will break and stop if they hit a snake's scaly skin, but won't hurt the snakes. There are some snakes already guarding the poison. Each snake occupies some consecutive cells in a straight line inside the NxN grid. That is, they are either part of a row, or part of a column. Note that there can be intersections between the snakes. A configuration of snakes is 'safe', if the thieves cannot steal poison. That is, no matter which row or column they shoot arrows from, either the arrow should hit a snake and stop (this can happen even after it has entered and exited the KxK grid), or it shouldn't ever enter the KxK grid. The King has other duties for the snakes, and hence wants to remove as many snakes as possible from this configuration, such that the remaining configuration is still 'safe'. Help him find the minimum number of snakes he needs to leave behind to protect the poison. -----Input----- - The first line contains a single integer, T, the number of testcases. - The first line of each testcase contains three integers: N, K and M, where N is the size of the entire square grid, K is the size of the square containing the poison, and M is the number of initial snakes. - M lines follow, the i-th of which contains four integers: HXi, HYi, TXi, TYi. (HXi, HYi) is the cell which contains the head of the i-th snake. (TXi, TYi) is the cell which contains the tail of the i-th snake. It is guaranteed that both these cells will either lie in the same row, or same column. And hence the cells in between them, including these two cells, represents the i-th snake. -----Output----- - For each testcase, output a single integer in a new line: The minimum number of the snakes that the king can keep and still protect the poison. If it is not possible to protect the poison even with all the snakes, output -1. -----Constraints----- - 1 ≤ T ≤ 4 - 3 ≤ N ≤ 109 - 1 ≤ K ≤ N-2 - Both N and K will be odd integers - 1 ≤ M ≤ 105 - 1 ≤ HXi, HYi, TXi, TYi ≤ N - It is guaranteed that at least one of (HXi = TXi), and (HYi = TYi) will be true for all i - None of the cells in the KxK grid will be occupied by any snake -----Example----- Input: 2 7 3 7 1 1 6 1 1 2 3 2 5 2 5 2 2 4 2 6 6 2 6 4 5 6 5 7 7 1 7 4 7 3 7 1 1 6 1 1 2 3 2 5 2 5 2 2 6 2 6 6 2 6 4 5 6 5 7 7 1 7 4 Output: 3 -1 -----Explanation----- The first example corresponds to: Note that the top-left corner cell of the NxN grid is by definition, (1,1). The inner square contains the poison, and the seven snakes are shown in different colours. The green snake is the 1st snake in the input. We can remove all but 3 snakes and protect the poison. One such configuration is this: You can check that no arrow can enter the inner poison square and exit the outer square without hitting a snake. Hence is it safe. Also, you cannot do this with fewer snakes. Hence 3 is the answer. The second testcase corresponds to: You can check that even with all the snakes, this is not a safe configuration, because the thieves can shoot an arrow down the 5th column, and they can steal poison. Hence, the answer is -1. The input will be stdin and you should print your solution to stdout Now solve the problem and return the code.
```python '''input 2 7 3 5 5 2 5 2 2 4 2 6 6 2 6 4 5 6 5 7 7 1 7 4 7 3 7 1 1 6 1 1 2 3 2 5 2 5 2 2 6 2 6 6 2 6 4 5 6 5 7 7 1 7 4 ''' for _ in range(int(input())): n, k, m = list(map(int, input().split())) row_s = [] col_s = [] for _ in range(m): h_x, h_y, t_x, t_y = list(map(int, input().split())) if h_x == t_x: if (h_x < (((n - k) // 2) + 1)) or (h_x > (((n - k) // 2) + k)): col_s.append([min(h_y, t_y), max(h_y, t_y)]) else: row_s.append([h_x, h_x]) if h_y == t_y: if (h_y < (((n - k) // 2) + 1)) or (h_y > (((n - k) // 2) + k)): row_s.append([min(h_x, t_x), max(h_x, t_x)]) else: col_s.append([h_y, h_y]) row_s.sort() col_s.sort() poss = True if len(col_s) == 0 or len(row_s) == 0: print(-1) continue # print(row_s, col_s) next_row = ((n - k) // 2) + 1 i = 0 count_row = 0 while i < len(row_s): max_next = next_row if next_row < row_s[i][0]: poss = False break while i < len(row_s) and row_s[i][0] <= next_row: # print(max_next, row_s[i], next_row) max_next = max(max_next, row_s[i][1] + 1) # print(max_next, row_s[i], next_row) i += 1 next_row = max_next count_row += 1 if next_row > (((n - k) // 2) + k): break if next_row < (((n - k) // 2) + k) and i >= len(row_s) : poss = False break # print(count_row) next_col = ((n - k) // 2) + 1 i = 0 count_col = 0 while i < len(col_s): max_next = next_col if next_col < col_s[i][0]: poss = False break while i < len(col_s) and col_s[i][0] <= next_col: # print(max_next, col_s[i], next_col) max_next = max(max_next, col_s[i][1] + 1) # print(max_next, col_s[i], next_col) i += 1 next_col = max_next count_col += 1 if next_col > (((n - k) // 2) + k): break if next_col < (((n - k) // 2) + k) and i >= len(col_s) : poss = False break # print(count_col) print(count_col + count_row if poss else -1) # print(row_s, col_s) ```
{ "language": "python", "test_cases": [ { "input": "2\n7 3 7\n1 1 6 1\n1 2 3 2\n5 2 5 2\n2 4 2 6\n6 2 6 4\n5 6 5 7\n7 1 7 4\n7 3 7\n1 1 6 1\n1 2 3 2\n5 2 5 2\n2 6 2 6\n6 2 6 4\n5 6 5 7\n7 1 7 4\n", "output": "3\n-1\n", "type": "stdin_stdout" } ] }
{ "difficulty": "interview", "problem_url": "https://www.codechef.com/problems/PROTEPOI" }
vfc_1730
apps
verifiable_code
723
Solve the following coding problem using the programming language python: In this problem, you will be given a polynomial, you have to print what it becomes after differentiation. Following are the rules for differentiation: - For a polynomial f(x), its differentiation is defined as f'(x). - If a is a constant, then differentiation of af(x) is af'(x). - If f(x) = h(x) + g(x) , then f'(x) = h'(x) + g'(x) - If f(x) = x n, then f'(x) = nxn-1. This is true for all n ≠ 0 . - If f(x) = c, where c is a constant, f'(x) = 0. If you are still uncomfortable with differentiation, please read the following: - Link to Wikihow page - Link to Wikipedia entry. -----Input----- First line contains T, the number of test cases to follow. Each test case contains the follows, the first line contains N, the number of non zero terms in the polynomial. Then N lines follow, each line contains a pair of integer which denotes a term in the polynomial, where the first element denotes the coefficient (a) and the second denotes the exponent (p) of the term. -----Output----- Print the polynomial after differentiation in the desired format as described below. - If the coefficient of a term in the output polynomial is 3, and the corresponding exponent is 2, print it as 3x^2 - Print " + " (with single space on both side) between each output term. - Print the terms in decreasing value of exponent. - For the constant term (if any), you have to just print the coefficient. You should not print x^0. -----Constraints----- - 1 ≤ T ≤ 10 - Subtask 1 (20 points) - 1 ≤ N ≤ 3 - 1 ≤ a ≤ 10 - 0 ≤ p ≤ 10 - Subtask 2 (80 points) - 1 ≤ N ≤ 10000 - 1 ≤ a ≤ 100000000 - 0 ≤ p ≤ 100000000 - No two inputs in a test case will have the same exponent. -----Example----- Input: 2 1 1 2 3 1 3 1 1 1 0 Output: 2x^1 3x^2 + 1 The input will be stdin and you should print your solution to stdout Now solve the problem and return the code.
```python def ans(l): s = "" i = 0 while (i < len(l)): temp = l[i] k = temp[1] if (k != 0): s += str(temp[0]) + "x^" + str(k) else: s += str(temp[0]) i += 1 if (i < len(l)): s += " + " if (len(s) > 0): return s else: return "0" test = int(input()) while (test != 0): test -= 1 N = int(input()) l = [] while (N != 0): n,m = list(map(int,input().split())) if (m > 0): l += [[n*m,m-1]] N -= 1 print(ans(l)) ```
{ "language": "python", "test_cases": [ { "input": "2\n1\n1 2\n3\n1 3\n1 1\n1 0\n", "output": "2x^1\n3x^2 + 1\n", "type": "stdin_stdout" } ] }
{ "difficulty": "interview", "problem_url": "https://www.codechef.com/LOCFEB16/problems/POLYDIFR" }
vfc_1734
apps
verifiable_code
724
Solve the following coding problem using the programming language python: Petya is preparing a problem for a local contest in his school. The problem is to find a longest increasing subsequence in a given permutation. A permutation of size n$n$ is a sequence of n$n$ numbers a1,…,an$a_1, \ldots, a_n$ such that every number from 1$1$ to n$n$ occurs in the sequence exactly once. An increasing subsequence of length k$k$ of the sequence a1,…,an$a_1, \ldots, a_n$ is a sequence of indices i1,…,ik$i_1, \ldots, i_k$ such that 1≤i1<…<ik≤n$1 \leq i_1 < \ldots < i_k \leq n$ and ai1<…<aik$a_{i_1} < \ldots < a_{i_k}$. A longest increasing subsequence is an increasing subsequences with the largest length. Note that in general there may be several longest increasing subsequences. Petya had some tests prepared, but then lost the input part for some of them. He now has a test for a certain value of n$n$, and a sequence i1,…,ik$i_1, \ldots, i_k$ that is supposed to be a longest increasing subsequence. Petya now has to reconstruct a permutation of size n$n$ with this sequence being an answer. Petya doesn't want to take any risks, so he additionally wants this sequence to be the only longest increasing subsequence, that is, all other increasing subsequences have to have strictly smaller length. Help Petya determine if this is possible, and if so, construct any such permutation. -----Input:----- The first line contains an integer T$T$, denoting number of test cases. The first line of every test case contains two integers n$n$ and k−$k-$ the size of the permutation and the length of the longest increasing subsequence (1≤k≤n≤105$1 \leq k \leq n \leq 10^5$). The second line contains k$k$ integers i1,…,ik−$i_1, \ldots, i_k-$ the longest increasing subsequence (1≤i1<…<ik≤n$1 \leq i_1 < \ldots < i_k \leq n$). -----Output:----- If there is no permutation with the sequence i1,…,ik$i_1, \ldots, i_k$ being the only longest increasing subsequence, print NO . Otherwise, print YES on the first line, followed by n$n$ numbers describing any suitable permutation on the second line. -----Constraints----- - 1≤T≤10$1 \leq T \leq 10$ - 1≤k≤n≤105$1 \leq k \leq n \leq 10^5$ - 1≤i1<…<ik≤n$1 \leq i_1 < \ldots < i_k \leq n$ -----Sample Input:----- 2 3 2 1 2 2 1 1 -----Sample Output:----- YES 2 3 1 NO The input will be stdin and you should print your solution to stdout Now solve the problem and return the code.
```python # cook your dish here for _ in range(int(input())): n,k = [int(c) for c in input().split()] a = [int(c) for c in input().split()] ls = a if n==1: print("YES") print(1) continue if k==1: print("NO") continue if k==2 and n>2: if ls[0]!=ls[1]-1: print("NO") continue ans = [0 for i in range(n+1)] count = n for i in range(1,a[1]): if i != a[0]: ans[i] =count count-=1 for i in a[::-1]: ans[i] = count count-=1 for i in range(1,n+1): if ans[i] == 0: ans[i] = count count-=1 print("YES") print(*ans[1:]) ```
{ "language": "python", "test_cases": [ { "input": "2\n3 2\n1 2\n2 1\n1\n", "output": "YES\n2 3 1\nNO\n", "type": "stdin_stdout" } ] }
{ "difficulty": "interview", "problem_url": "https://www.codechef.com/problems/INVLIS" }
vfc_1738
apps
verifiable_code
725
Solve the following coding problem using the programming language python: The Little Elephant and his friends from the Zoo of Lviv were returning from the party. But suddenly they were stopped by the policeman Big Hippo, who wanted to make an alcohol test for elephants. There were N elephants ordered from the left to the right in a row and numbered from 0 to N-1. Let R[i] to be the result of breathalyzer test of i-th elephant. Considering current laws in the Zoo, elephants would be arrested if there exists K consecutive elephants among them for which at least M of these K elephants have the maximal test result among these K elephants. Using poor math notations we can alternatively define this as follows. The elephants would be arrested if there exists i from 0 to N-K, inclusive, such that for at least M different values of j from i to i+K-1, inclusive, we have R[j] = max{R[i], R[i+1], ..., R[i+K-1]}. The Big Hippo is very old and the Little Elephant can change some of the results. In a single operation he can add 1 to the result of any elephant. But for each of the elephants he can apply this operation at most once. What is the minimum number of operations that the Little Elephant needs to apply, such that the sequence of results, after all operations will be applied, let elephants to avoid the arrest? If it is impossible to avoid the arrest applying any number of operations, output -1. -----Input----- The first line of the input contains an integer T denoting the number of test cases. The description of T test cases follows. The first line of each test case contains three space-separated integers N, K, M. The second line contains N space-separated integers R[0], R[1], ..., R[N-1] denoting the test results of the elephants. -----Output----- For each test case, output a single line containing the minimum number of operations needed to avoid the arrest. -----Constraints----- - 1 ≤ T ≤ 10 - 1 ≤ M ≤ K ≤ N ≤ 17 - 1 ≤ R[i] ≤ 17 -----Example----- Input: 4 5 3 2 1 3 1 2 1 5 3 3 7 7 7 7 7 5 3 3 7 7 7 8 8 4 3 1 1 3 1 2 Output: 0 1 1 -1 -----Explanation----- Example case 1. Let's follow the poor math definition of arrest. We will consider all values of i from 0 to N-K = 2, inclusive, and should count the number of values of j described in the definition. If it less than M = 2 then this value of i does not cause the arrest, otherwise causes.i{R[i],...,R[i+K-1]}max{R[i],...,R[i+K-1]}For which j = i, ..., i+K-1 we have R[j] = maxConclusioni=0{1, 3, 1}max = 3R[j] = 3 for j = 1does not cause the arresti=1{3, 1, 2}max = 3R[j] = 3 for j = 1does not cause the arresti=2{1, 2, 1}max = 2R[j] = 2 for j = 3does not cause the arrest So we see that initial test results of the elephants do not cause their arrest. Hence the Little Elephant does not need to apply any operations. Therefore, the answer is 0. Example case 2.We have N = 5, K = 3, M = 3. Let's construct similar table as in example case 1. Here the value of i will cause the arrest if we have at least 3 values of j described in the definition.i{R[i],...,R[i+K-1]}max{R[i],...,R[i+K-1]}For which j = i, ..., i+K-1 we have R[j] = maxConclusioni=0{7, 7, 7}max = 7R[j] = 7 for j = 0, 1, 2causes the arresti=1{7, 7, 7}max = 7R[j] = 7 for j = 1, 2, 3causes the arresti=2{7, 7, 7}max = 7R[j] = 7 for j = 2, 3, 4causes the arrest So we see that for initial test results of the elephants each value of i causes their arrest. Hence the Little Elephant needs to apply some operations in order to avoid the arrest. He could achieve his goal by adding 1 to the result R[2]. Then results will be {R[0], R[1], R[2], R[3], R[4]} = {7, 7, 8, 7, 7}. Let's check that now elephants will be not arrested.i{R[i],...,R[i+K-1]}max{R[i],...,R[i+K-1]}For which j = i, ..., i+K-1 we have R[j] = maxConclusioni=0{7, 7, 8}max = 8R[j] = 8 for j = 2does not cause the arresti=1{7, 8, 7}max = 8R[j] = 8 for j = 2does not cause the arresti=2{8, 7, 7}max = 8R[j] = 8 for j = 2does not cause the arrest So we see that now test results of the elephants do not cause their arrest. Thus we see that using 0 operations we can't avoid the arrest but using 1 operation can. Hence the answer is 1. Example case 3.We have N = 5, K = 3, M = 3. Let's construct similar table as in example case 1. Here the value of i will cause the arrest if we have at least 3 values of j described in the definition.i{R[i],...,R[i+K-1]}max{R[i],...,R[i+K-1]}For which j = i, ..., i+K-1 we have R[j] = maxConclusioni=0{7, 7, 7}max = 7R[j] = 7 for j = 0, 1, 2causes the arresti=1{7, 7, 8}max = 8R[j] = 8 for j = 3does not cause the arresti=2{7, 8, 8}max = 8R[j] = 8 for j = 3, 4does not cause the arrest So we see that for initial test results of the elephants the value of i = 0 causes their arrest. Hence the Little Elephant needs to apply some operations in order to avoid the arrest. He could achieve his goal by adding 1 to the result R[1]. Then results will be {R[0], R[1], R[2], R[3], R[4]} = {7, 8, 7, 8, 8}. Let's check that now elephants will be not arrested.i{R[i],...,R[i+K-1]}max{R[i],...,R[i+K-1]}For which j = i, ..., i+K-1 we have R[j] = maxConclusioni=0{7, 8, 7}max = 8R[j] = 8 for j = 1does not cause the arresti=1{8, 7, 8}max = 8R[j] = 8 for j = 1, 3does not cause the arresti=2{7, 8, 8}max = 8R[j] = 8 for j = 3, 4does not cause the arrest So we see that now test results of the elephants do not cause their arrest. Thus we see that using 0 operations we can't avoid the arrest but using 1 operation can. Hence the answer is 1. Note that if we increase by 1 the result R[2] instead of R[1] then the value i = 2 will cause the arrest since {R[2], R[3], R[4]} will be {8, 8, 8} after this operation and we will have 3 values of j from 2 to 4, inclusive, for which R[j] = max{R[2], R[3], R[4]}, namely, j = 2, 3, 4. Example case 4. When M = 1 the Little Elephant can't reach the goal since for each value of i from 0 to N-K we have at least one value of j for which R[j] = max{R[i], R[i+1], ..., R[i+K-1]}. The input will be stdin and you should print your solution to stdout Now solve the problem and return the code.
```python def magic(): def check(art,k,m): n=len(art) for i in range(n-k+1): maxi=0 maxi=max(art[i:i+k]) total=0 total=art[i:i+k].count(maxi) if total>=m: return False return True for _ in range(eval(input())): n,k,m=list(map(int,input().split())) arr=list(map(int,input().split())) dp=[] ans=100 for mask in range(0,(1<<n)): size=bin(mask).count('1') if ans>size: art=list(arr) for i in range(n): if mask & (1<<i): art[i]+=1 if check(art,k,m): ans=size print(ans if ans!=100 else -1) magic() ```
{ "language": "python", "test_cases": [ { "input": "4\n5 3 2\n1 3 1 2 1\n5 3 3\n7 7 7 7 7\n5 3 3\n7 7 7 8 8\n4 3 1\n1 3 1 2\n", "output": "0\n1\n1\n-1\n", "type": "stdin_stdout" } ] }
{ "difficulty": "interview", "problem_url": "https://www.codechef.com/problems/LEALCO" }
vfc_1742
apps
verifiable_code
726
Solve the following coding problem using the programming language python: Today, Chef decided to cook some delicious meals from the ingredients in his kitchen. There are $N$ ingredients, represented by strings $S_1, S_2, \ldots, S_N$. Chef took all the ingredients, put them into a cauldron and mixed them up. In the cauldron, the letters of the strings representing the ingredients completely mixed, so each letter appears in the cauldron as many times as it appeared in all the strings in total; now, any number of times, Chef can take one letter out of the cauldron (if this letter appears in the cauldron multiple times, it can be taken out that many times) and use it in a meal. A complete meal is the string "codechef". Help Chef find the maximum number of complete meals he can make! -----Input----- - 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 line of each test case contains a single integer $N$. - $N$ lines follow. For each $i$ ($1 \le i \le N$), the $i$-th of these lines contains a single string $S_i$. -----Output----- For each test case, print a single line containing one integer — the maximum number of complete meals Chef can create. -----Constraints----- - $1 \le T \le 100$ - $1 \le N \le 100$ - $|S_1| + |S_2| + \ldots + |S_N| \le 1,000$ - each string contains only lowercase English letters -----Example Input----- 3 6 cplusplus oscar deck fee hat near 5 code hacker chef chaby dumbofe 5 codechef chefcode fehcedoc cceeohfd codechef -----Example Output----- 1 2 5 -----Explanation----- Example case 1: After mixing, the cauldron contains the letter 'c' 3 times, the letter 'e' 4 times, and each of the letters 'o', 'd', 'h' and 'f' once. Clearly, this is only enough for one "codechef" meal. Example case 2: After mixing, the cauldron contains the letter 'c' 4 times, 'o' 2 times, 'd' 2 times, 'e' 4 times, 'h' 3 times and 'f' 2 times, which is enough to make 2 meals. The input will be stdin and you should print your solution to stdout Now solve the problem and return the code.
```python # cook your dish here t=int(input()) while t>0: n=int(input()) li=[] c,o,d,e,h,f=0,0,0,0,0,0 for i in range(0,n): s=input() for i in range(len(s)): if s[i]=='c': c=c+1 elif s[i]=='o': o=o+1 elif s[i]=='d': d=d+1 elif s[i]=='e': e=e+1 elif s[i]=='h': h=h+1 elif s[i]=='f': f=f+1 e=e//2 c=c//2 print(min(c,o,d,e,h,f)) t-=1 ```
{ "language": "python", "test_cases": [ { "input": "3\n6\ncplusplus\noscar\ndeck\nfee\nhat\nnear\n5\ncode\nhacker\nchef\nchaby\ndumbofe\n5\ncodechef\nchefcode\nfehcedoc\ncceeohfd\ncodechef\n", "output": "1\n2\n5\n", "type": "stdin_stdout" } ] }
{ "difficulty": "interview", "problem_url": "https://www.codechef.com/problems/CFMM" }
vfc_1746