problem_id
int64
0
5k
question
stringlengths
50
14k
solutions
stringlengths
12
764k
test_cases
stringlengths
2
23.6M
difficulty
stringclasses
3 values
starter_code
stringlengths
0
952
500
Implement a basic calculator to evaluate a simple expression string. The expression string contains only non-negative integers, +, -, *, / operators and empty spaces . The integer division should truncate toward zero. Example 1: Input: "3+2*2" Output: 7 Example 2: Input: " 3/2 " Output: 1 Example 3: Input: ...
["class Solution:\n def calculate(self, s):\n \"\"\"\n :type s: str\n :rtype: int\n \"\"\"\n \n if not s:\n return 0\n \n pre_op = '+'\n stack = [0]\n cur_num = 0\n digits = '0123456789'\n s += '#'\n ...
[{"input": ["\"3+2*2\""], "output": 7}]
interview
class Solution: def calculate(self, s: str) -> int:
501
Given a string s, you are allowed to convert it to a palindrome by adding characters in front of it. Find and return the shortest palindrome you can find by performing this transformation. Example 1: Input: "aacecaaa" Output: "aaacecaaa" Example 2: Input: "abcd" Output: "dcbabcd"
["class Solution:\n def shortestPalindrome(self, s):\n if len(s)<2:\n return s\n if len(s)==40002:\n return s[20000:][::-1]+s\n for i in range(len(s)-1,-1,-1):\n if s[i]==s[0]:\n j=0\n while j<(i+1)//2 and s[i-j]==s[j]:\n ...
[{"input": ["\"aacecaaa\""], "output": "\"aaacecaa\"aacecaaa\""}]
interview
class Solution: def shortestPalindrome(self, s: str) -> str:
502
In a network of nodes, each node i is directly connected to another node j if and only if graph[i][j] = 1. Some nodes initial are initially infected by malware.  Whenever two nodes are directly connected and at least one of those two nodes is infected by malware, both nodes will be infected by malware.  This spread of ...
["class Solution(object):\n def minMalwareSpread(self, graph, initial):\n # 1. Color each component.\n # colors[node] = the color of this node.\n\n N = len(graph)\n colors = {}\n c = 0\n\n def dfs(node, color):\n colors[node] = color\n for nei, adj in e...
[{"input": [[[1, 1, 0], [1, 1, 0], [0, 0, 1], [], []], [0, 1]], "output": 0}]
interview
class Solution: def minMalwareSpread(self, graph: List[List[int]], initial: List[int]) -> int:
503
Given a sentence text (A sentence is a string of space-separated words) in the following format: First letter is in upper case. Each word in text are separated by a single space. Your task is to rearrange the words in text such that all words are rearranged in an increasing order of their lengths. If two words have t...
["class Solution:\n def arrangeWords(self, text: str) -> str:\n p=text.split(' ')\n final=''\n j=sorted(p,key=len)\n temp=' '.join(j)\n if temp[0]>='a' and temp[0]<='z':\n s=temp[0].swapcase()\n final=final+s[0]\n else:\n final=final+temp[0]\...
[{"input": ["\"Leetcode is cool\""], "output": "Is cool\" \"leetcode"}]
interview
class Solution: def arrangeWords(self, text: str) -> str:
504
You are given a string s that consists of lower case English letters and brackets.  Reverse the strings in each pair of matching parentheses, starting from the innermost one. Your result should not contain any brackets.   Example 1: Input: s = "(abcd)" Output: "dcba" Example 2: Input: s = "(u(love)i)" Output: "iloveu"...
["class Solution:\n def reverseParentheses(self, s: str) -> str:\n \n \n stack = []\n curr = ''\n for c in s:\n if c=='(':\n stack.append(curr)\n curr = ''\n stack.append('(')\n elif c==')':\n stack.a...
[{"input": ["\"(abcd)\""], "output": "\"dcba\""}]
interview
class Solution: def reverseParentheses(self, s: str) -> str:
505
Given a string s of '(' , ')' and lowercase English characters.  Your task is to remove the minimum number of parentheses ( '(' or ')', in any positions ) so that the resulting parentheses string is valid and return any valid string. Formally, a parentheses string is valid if and only if: It is the empty string, conta...
["class Solution:\n def minRemoveToMakeValid(self, s: str) -> str:\n if not s: return s\n l=0\n r=0\n res=''\n for i,c in enumerate(s):\n if c=='(':\n l+=1\n if c==')':\n if l==r:\n continue\n els...
[{"input": ["\"lee(t(c)o)de)\""], "output": "\"lee(t(c)o)de\""}]
interview
class Solution: def minRemoveToMakeValid(self, s: str) -> str:
506
Implement atoi which converts a string to an integer. The function first discards as many whitespace characters as necessary until the first non-whitespace character is found. Then, starting from this character, takes an optional initial plus or minus sign followed by as many numerical digits as possible, and interpre...
["class Solution:\n def myAtoi(self, str):\n \"\"\"\n :type str: str\n :rtype: int\n \"\"\"\n base = \"0123456789\"\n plus = \"+\"\n minus = \"-\"\n sum = 0\n flag = 1\n bit = 0\n INT_MAX = 2147483647\n INT_MIN = -214748...
[{"input": ["\"42\""], "output": 0}]
interview
class Solution: def myAtoi(self, s: str) -> int:
507
Given a sorted array consisting of only integers where every element appears twice except for one element which appears once. Find this single element that appears only once. Example 1: Input: [1,1,2,3,3,4,4,8,8] Output: 2 Example 2: Input: [3,3,7,7,10,11,11] Output: 10 Note: Your solution should run in O(lo...
["class Solution:\n def singleNonDuplicate(self, nums):\n \"\"\"\n :type nums: List[int]\n :rtype: int\n \"\"\"\n return self.singleNonDuplicateUtil(nums, 0, len(nums)-1)\n \n def singleNonDuplicateUtil(self, nums, l, r):\n if l < r:\n mid = in...
[{"input": [[1, 1, 2, 3, 3, 4, 4, 8, 8]], "output": 2}]
interview
class Solution: def singleNonDuplicate(self, nums: List[int]) -> int:
508
There is an infinitely long street that runs west to east, which we consider as a number line. There are N roadworks scheduled on this street. The i-th roadwork blocks the point at coordinate X_i from time S_i - 0.5 to time T_i - 0.5. Q people are standing at coordinate 0. The i-th person will start the coordinate 0 at...
["from heapq import heapify, heappush, heappop\nimport sys\ninput = sys.stdin.readline\n\ndef solve():\n N, Q = list(map(int, input().split()))\n events = []\n for i in range(N):\n S, T, X = list(map(int, input().split()))\n events.append((S-X-0.5, 1, X))\n events.append((T-X-0.5, 0, X))\n...
[{"input": "4 6\n1 3 2\n7 13 10\n18 20 13\n3 4 2\n0\n1\n2\n3\n5\n8\n", "output": "2\n2\n10\n-1\n13\n-1\n"}]
interview
509
Given is an undirected connected graph with N vertices numbered 1 to N, and M edges numbered 1 to M. The given graph may contain multi-edges but not self loops. Each edge has an integer label between 1 and N (inclusive). Edge i has a label c_i, and it connects Vertex u_i and v_i bidirectionally. Snuke will write an int...
["import sys\nsys.setrecursionlimit(10**6)\n\nclass UnionFind():\n def __init__(self, n):\n self.n = n\n self.parents = [-1] * n\n\n def find(self, x):\n if self.parents[x] < 0:\n return x\n else:\n self.parents[x] = self.find(self.parents[x])\n return ...
[{"input": "3 4\n1 2 1\n2 3 2\n3 1 3\n1 3 1\n", "output": "1\n2\n1\n"}]
interview
510
You are given a string S of length N consisting of lowercase English letters. Process Q queries of the following two types: - Type 1: change the i_q-th character of S to c_q. (Do nothing if the i_q-th character is already c_q.) - Type 2: answer the number of different characters occurring in the substring of S betwee...
["n = int(input())\ns = list(input())\ns = [ord(i)-97 for i in s]\n\ndic = {}\nfor i in range(26):\n dic[i] = []\n\nfor i in range(n):\n dic[s[i]].append(i)\n\nfor i in range(26):\n dic[i].append(float('inf'))\n\nfrom bisect import bisect_left\nq = int(input())\nfor i in range(q):\n x, y, z = input().split(...
[{"input": "7\nabcdbbd\n6\n2 3 6\n1 5 z\n2 1 1\n1 4 a\n1 7 d\n2 1 7\n", "output": "3\n1\n5\n"}]
interview
511
There are N Snuke Cats numbered 1, 2, \ldots, N, where N is even. Each Snuke Cat wears a red scarf, on which his favorite non-negative integer is written. Recently, they learned the operation called xor (exclusive OR).What is xor? For n non-negative integers x_1, x_2, \ldots, x_n, their xor, x_1~\textrm{xor}~x_2~\text...
["n=int(input())\na=list(map(int,input().split()))\n\nX=[]\nb=a[0]\nfor i in range(1,n) :\n b^=a[i]\n\nfor i in range(n) :\n x=b^a[i]\n X.append(x)\n\nfor i in X :\n print(i,end=\" \")\n", "def main():\n N = int(input())\n A = list(map(int, input().split()))\n\n scarf_0 = 0\n for a in A[1:]:\n ...
[{"input": "4\n20 11 9 24\n", "output": "26 5 7 22\n"}]
interview
512
There is a tree with N vertices numbered 1 to N. The i-th edge in this tree connects Vertex a_i and Vertex b_i, and the color and length of that edge are c_i and d_i, respectively. Here the color of each edge is represented by an integer between 1 and N-1 (inclusive). The same integer corresponds to the same color, and...
["import sys\ninput = sys.stdin.readline\nsys.setrecursionlimit(10**5)\n\nN, Q = map(int, input().split())\n\npath = [[] for _ in range(N)]\n\nfor _ in range(N-1) :\n a, b, c, d = (int(i) for i in input().split())\n path[a-1].append((b-1, c-1, d))\n path[b-1].append((a-1, c-1, d))\n\n# doubling\u306b\u5fc5\u89...
[{"input": "5 3\n1 2 1 10\n1 3 2 20\n2 4 4 30\n5 2 1 40\n1 100 1 4\n1 100 1 5\n3 1000 3 4\n", "output": "130\n200\n60\n"}]
interview
513
We have a tree with N vertices, whose i-th edge connects Vertex u_i and Vertex v_i. Vertex i has an integer a_i written on it. For every integer k from 1 through N, solve the following problem: - We will make a sequence by lining up the integers written on the vertices along the shortest path from Vertex 1 to Vertex k...
["import bisect\nimport sys\nsys.setrecursionlimit(10**7)\n\ndef dfs(v):\n pos=bisect.bisect_left(dp,arr[v])\n changes.append((pos,dp[pos]))\n dp[pos]=arr[v]\n ans[v]=bisect.bisect_left(dp,10**18)\n for u in g[v]:\n if checked[u]==0:\n checked[u]=1\n dfs(u)\n pos,val=chang...
[{"input": "10\n1 2 5 3 4 6 7 3 2 4\n1 2\n2 3\n3 4\n4 5\n3 6\n6 7\n1 8\n8 9\n9 10\n", "output": "1\n2\n3\n3\n4\n4\n5\n2\n2\n3\n"}]
interview
514
Write a program to take two numbers as input and print their difference if the first number is greater than the second number otherwise$otherwise$ print their sum. -----Input:----- - First line will contain the first number (N1$N1$) - Second line will contain the second number (N2$N2$) -----Output:----- Output a sing...
["m = int(input())\nn = int(input())\nprint(m-n) if m>n else print(m+n)", "# cook your dish here\na = int(input())\nb = int(input())\nif a>b:\n print(a-b)\nelse:\n print(a+b)", "n1=int(input())\nn2=int(input())\nprint(n1-n2) if(n1>n2) else print(n1+n2)\n", "n1=int(input())\nn2=int(input())\nif(n1>n2):\n print(...
[{"input": ["82", "28"], "output": ["54"]}]
interview
515
Witua is a little student from the University of Lviv. He enjoys studying math. Witua knows a lot of famous mathematicians like Eratosthenes, Pythagoras, Fermat, Diophantus, Furko, Gauss and so on. However, his favorite one is Euler. The only thing Witua likes more than Euler is Euler’s totient function φ. He is explor...
["# cook your dish here\ndef modular_pow(base, exponent, modulus):\n result = 1\n while exponent > 0:\n if(exponent %2 == 1):\n result = (result * base) % modulus\n exponent = exponent//2\n base = (base * base)%modulus\n return result\ndef passesMillerRabinTest(n, a):\n s = 0\n d = n-1\n while(d%2 == 0):\n s += 1...
[{"input": ["3", "2", "3", "4"], "output": ["2", "3", "3"]}]
interview
516
Almir had a small sequence $A_1, A_2, \ldots, A_N$. He decided to make $K$ copies of this sequence and concatenate them, forming a sequence $X_1, X_2, \ldots, X_{NK}$; for each valid $i$ and $j$ ($0 \le j < K$), $X_{j \cdot N + i} = A_i$. For example, if $A = (1, 2, 3)$ and $K = 4$, the final sequence is $X = (1, 2, 3,...
["# cook your dish here\ndef count(k,n,m):\n sum1=(m*(m+1))//2\n sum2=(m*(m-1))//2\n ct=0\n for i in range(n):\n for j in range(n):\n if i<j and k[i]>k[j]:\n ct+=sum1\n elif j<i and k[i]>k[j]:\n ct+=sum2\n return ct\n\ntest=int(input())\nfor _ in...
[{"input": ["2", "3 3", "2 1 3", "4 100", "99 2 1000 24", ""], "output": ["12", "30000"]}]
interview
517
Indian National Olympiad in Informatics 2015 A string is any nonempty sequence of 0s and 1s. Examples of strings are 00, 101, 111000, 1, 0, 01. The length of a string is the number of symbols in it. For example, the length of 111000 is 6. If u and v are strings, then uv is the string obtained by concatenating u and v. ...
["# cook your dish here\r\ndef offset(l, flag):\r\n x = 0\r\n # print(l)\r\n for i in range(1, len(l)):\r\n temp = []\r\n for j in range(i):\r\n v = getbig(l[i], l[j], fs)\r\n if v > 1:\r\n temp.append(v)\r\n if flag:\r\n x +=...
[{"input": ["3 176"], "output": ["6"]}]
interview
518
Finally, the pandemic is over in ChefLand, and the chef is visiting the school again. Chef likes to climb the stairs of his school's floor by skipping one step, sometimes chef climbs the stairs one by one. Simply, the chef can take one or 2 steps in one upward movement. There are N stairs between ground and next floor....
["for _ in range(int(input())):\n N=int(input())\n if N%2==0:\n print(N//2+1)\n else:\n print((N-1)//2+1)", "# cook your dish here\nfor _ in range(int(input())):\n n=int(input())\n print((n//2)+1)", "# cook your dish here\nfor t in range(int(input())):\n n = int(input())\n print(int(n...
[{"input": ["1", "3"], "output": ["2"]}]
interview
519
Indian National Olympiad in Informatics 2016 There are k types of brackets each with its own opening bracket and closing bracket. We assume that the first pair is denoted by the numbers 1 and k+1, the second by 2 and k+2 and so on. Thus the opening brackets are denoted by 1,2,.., k, and the corresponding closing brack...
["# cook your dish here\r\nimport bisect\r\nn, k1, *l = map(int, input().split())\r\nv_l, b_l = l[:n], l[n:]\r\n\r\nb_inv = {key:[] for key in range(2*k1)}\r\nfor i in range(n):\r\n b_l[i] -= 1\r\n b_inv[b_l[i]].append(i)\r\n\r\ndp = [[0 for _ in range(n)] for _ in range(n)]\r\nfor k in range(1, n):\r\n for j ...
[{"input": ["6 3 4 5 -2 1 1 6 1 3 4 2 5 6"], "output": ["13"]}]
interview
520
Write a program that takes in a letterclass ID of a ship and display the equivalent string class description of the given ID. Use the table below. Class ID Ship ClassB or bBattleShipC or cCruiserD or dDestroyerF or fFrigate -----Input----- The first line contains an integer T, the total number of testcases. Then T l...
["# cook your dish here\nt=int(input())\nfor i in range(t):\n n=input()\n if(n=='b' or n=='B'):\n print('BattleShip')\n elif(n=='c' or n=='C'):\n print('Cruiser')\n elif(n=='d' or n=='D'):\n print('Destroyer')\n else:\n print('Frigate')", "for _ in range(int(input())):\n p=...
[{"input": ["3", "B", "c", "D"], "output": ["BattleShip", "Cruiser", "Destroyer"]}]
interview
521
Nature photographing may be fun for tourists, but it is one of the most complicated things for photographers. To capture all the facets of a bird, you might need more than one cameras. You recently encountered such a situation. There are $n$ photographers, so there are $n$ cameras in a line on the x-axis. All the camer...
["from math import *\r\nfrom collections import *\r\nimport sys\r\ninput=sys.stdin.readline\r\nt=int(input())\r\nwhile(t):\r\n t-=1\r\n n=int(input())\r\n a=list(map(int,input().split()))\r\n p,q=map(int,input().split())\r\n s=0\r\n a.sort()\r\n for i in range(n//2):\r\n x=a[i]\r\n x1...
[{"input": ["2", "2", "0 1", "0 1", "2", "0 1", "100 1", ""], "output": ["0.785398163397", "0.000100999899"]}]
interview
522
Three Best Friends $AMAN$ , $AKBAR$ , $ANTHONY$ are planning to go to “GOA” , but just like every other goa trip plan there is a problem to their plan too. Their parents will only give permission if they can solve this problem for them They are a given a number N and they have to calculate the total number of triplets...
["import sys\r\ndef get_array(): return list(map(int , sys.stdin.readline().strip().split()))\r\ndef get_ints(): return list(map(int, sys.stdin.readline().strip().split()))\r\ndef input(): return sys.stdin.readline().strip()\r\nimport sys\r\nsys.setrecursionlimit(10**9)\r\nfrom math import sqrt,ceil,floor\r\nn=int(inpu...
[{"input": ["3"], "output": ["3"]}]
interview
523
Maheshmati and Sangu are playing a game. First, Maheshmati gives Sangu a sequence of $N$ distinct integers $a_1, a_2, \dots, a_N$ (not necessarily sorted) and an integer $K$. Sangu has to create all subsequences of this sequence with length $K$. For each subsequence, he has to write down the product of $K-2$ integers: ...
["f = 5003*[0]\nmodn = 1000000007\n\n\ndef qPow(a, b):\n nonlocal modn\n res = 1\n while b > 0:\n if (b & 1) == 1:\n res = res * a % modn\n a = a * a % modn\n b = b >> 1\n return res\n\n\ndef getF():\n nonlocal f\n f[0] = 1\n for i in range(1, 5001):\n f[i] = f[i-1] * i\n\n\ndef __starting_point():\n getF()\n T =...
[{"input": ["1", "4 3", "1 2 3 4"], "output": ["36"]}]
interview
524
There are total $N$ cars in a sequence with $ith$ car being assigned with an alphabet equivalent to the $ith$ alphabet of string $S$ . Chef has been assigned a task to calculate the total number of cars with alphabet having a unique even value in the given range X to Y (both inclusive) . The value of an alphabet is sim...
["arr = list(input())\r\nn = len(arr)\r\nans = list()\r\n#for i in arr:\r\n #ans.append(ord(i)-96)\r\nli = ['b','d','f','h','j','l','n','p','r','t','v','x','z']\r\ns = set(arr)\r\ntemp = s.intersection(li)\r\nfor _ in range(int(input())):\r\n x,y = list(map(int,input().split()))\r\n li = list(temp)\r\n #s =...
[{"input": ["bbccdd", "5", "1 2", "3 4", "5 6", "1 6", "2 5"], "output": ["1", "0", "1", "2", "2"]}]
interview
525
You are given three numbers $a$, $b$, $c$ . Write a program to determine the largest number that is less than or equal to $c$ and leaves a remainder $b$ when divided by $a$. -----Input:----- - First line will contain $T$, number of testcases. Then the testcases follow. - Each testcase contains a single line of input, ...
["# cook your dish here\nfor t in range(int(input())):\n a,b,c=map(int,input().split())\n p=(c//a)*a+b\n if p<=c:\n print(p)\n else:\n print(((c//a)-1)*a+b)", "try:\r\n for _ in range(int(input())):\r\n a,b,c=[int(i) for i in input().split()]\r\n r=c%a\r\n if(r>b):\r\n ...
[{"input": ["1", "7 2 10"], "output": ["9"]}]
interview
526
Aureole the Techno-cultural fest of JEC thought of conducting a workshop on big data, as the topic is hot everyone wants to take part but due to limited seats in the Jashan Auditorium there is a selection criteria, a problem is given. the problem states a string is to be compressed when two or more consecutive characte...
["#include<sdg.h>\nfor _ in range(int(input())):\n s=input()\n n=len(s)\n if n==1:\n if s[0].isalpha(): print(\"-32\")\n else: print(0)\n else:\n num,ch=0,0\n p,q=0,0\n c=1\n x=s[0]\n ans=\"\"\n for i in range(1,n):\n if s[i-1]==s[i]:\n ...
[{"input": ["1", "aaabb"], "output": ["-40"]}]
interview
527
Given an Array of length $N$ containing elements $Ai$ ( i = 1 to n ) . You have to handle $Q$ queries on this array . Each Query is of two types k=(1 or 2). Type 1:- $k$ $l$ $r$ in which you have to tell whether the product of numbers in range l to r results in a perfect square or not. if product of numbers in range...
["def update(index, value, bi_tree):\n while index < len(bi_tree):\n bi_tree[index] += value\n index += index & -index\n\n\ndef get_sum(index, bi_tree):\n ans = 0\n while index > 0:\n ans += bi_tree[index]\n index -= index & -index\n\n return ans\n\n\ndef get_range_sum(left, right, bi_tree):\n ans = get_sum(right, ...
[{"input": ["4", "2 2 3 4", "4", "1 1 2", "1 3 4", "2 3 3", "1 1 4"], "output": ["YES", "NO", "YES"]}]
interview
528
Recently in JEC ants have become huge, the Principal is on a journey to snipe them !! Principal has limited $N$ practice Bullets to practice so that he can be sure to kill ants. - The Practice ground has max length $L$. - There is a Limit X such that if the bullet is fired beyond this, it will destroy and it wont be of...
["# cook your dish here\n\nfrom sys import stdin,stdout\nfrom collections import deque,defaultdict\nfrom math import ceil,floor,inf,sqrt,factorial,gcd,log\nfrom copy import deepcopy\nii1=lambda:int(stdin.readline().strip())\nis1=lambda:stdin.readline().strip()\niia=lambda:list(map(int,stdin.readline().strip().split()))...
[{"input": ["2", "1 10", "2 10"], "output": ["10", "4"]}]
interview
529
Given an integer N. Integers A and B are chosen randomly in the range [1..N]. Calculate the probability that the Greatest Common Divisor(GCD) of A and B equals to B. -----Input----- The first line of the input contains an integer T denoting the number of test cases. The description of T test cases follows. Each test c...
["import math\nfor _ in range(int(input())):\n n=int(input())\n s=int(math.sqrt(n))\n ans=0\n for i in range(1,s+1):\n ans+=(n//i)\n ans=ans*2-(s*s)\n g=math.gcd(n*n,ans)\n print(str(ans//g)+\"/\"+str(n*n//g)) ", "from collections import defaultdict\nimport sys\nimport math as m\nimport random as rd\nimport bisect as ...
[{"input": ["3", "1", "2", "3"], "output": ["1/1", "3/4", "5/9"]}]
interview
530
The median of a sequence is the element in the middle of the sequence after it is sorted. For a sequence with even size, the median is the average of the two middle elements of the sequence after sorting. For example, for a sequence $A = [1, 3, 3, 5, 4, 7, 11]$, the median is equal to $4$, and for $A = [2, 3, 4, 5]$, t...
["# cook your dish here\n# cook your dish here\nimport numpy as np\nimport sys\n\ndef findSeq(n, s, k, m, M):\n midInd = n // 2\n seqs = []\n for ind in range(midInd + 2, midInd - 3, -1):\n if ind >= n or ind < 0:\n continue \n seq = genBestSeq(n, ind, m, M, s)\n if seq is n...
[{"input": ["2", "3 6 1 1 5", "4 4 2 1 3", ""], "output": ["1 1 4", "-1"]}]
interview
531
Shashank is playing a game with his friends. There are n sticks located in a row at points $a_1,a_2, ...,a_n$. Each stick has a height- $h_i$. A person can chop a stick down, after which it takes over one of the regions [$a_i$ - $h_i$, $a_i$] or [$a_i$, $a_i$ + $h_i$]. The stick that is not chopped remains at the point...
["# cook your dish here\nn=int(input())\ncounts=dict()\nz=0\nupper=None\nfor i in range(0,n):\n a,h= [int(num) for num in input().split()]\n counts[a]=h\nfor key,count in counts.items():\n c=0\n x=key-count\n y=key+count\n c1=0\n c2=0\n for j in counts.keys():\n if j==key:\n continue\n else:\n if x<=j<=key:\n ...
[{"input": ["5", "1 2", "2 1", "5 10", "10 9", "19 1", "Sample Input 2:", "5", "1 2", "2 1", "5 10", "10 9", "20 1"], "output": ["3", "Sample Output 2:", "4"]}]
interview
532
To help Lavanya learn all about binary numbers and binary sequences, her father has bought her a collection of square tiles, each of which has either a 0 or a 1 written on it. Her brother Nikhil has played a rather nasty prank. He has glued together pairs of tiles with 0 written on them. Lavanya now has square tiles wi...
["n=int(input())\nmodulo=15746\nnum=[1,1]\nfor i in range(2,n+1):\n num.append((num[i-1]+num[i-2])%modulo)\nprint(num[n])", "def EXEC(n):\r\n if n < 3: return n\r\n else:\r\n x, y = 1, 2\r\n for _ in range(2, n):\r\n z = (x + y) % 15746\r\n x, y = y, z\r\n return y % ...
[{"input": ["4"], "output": ["5"]}]
interview
533
The chef is playing a game of long distance. Chef has a number K and he wants to find the longest distance between the index of the first and the last occurrence of K in a given array of N numbers. -----Input:----- - First-line will contain $T$, the number of test cases. Then the test cases follow. - Each test case c...
["# cook your dish here\nfor _ in range(int(input())):\n m,n=list(map(int,input().split()))\n a=[int(i) for i in input().split()]\n l=-1\n for i in range(n-1,-1,-1):\n if a[i]==m:\n l=i\n break\n f=-1\n for i in range(0,n):\n if a[i]==m:\n f=i\n ...
[{"input": ["2", "2 6", "2 3 4 2 1 6", "4 6", "2 3 4 2 1 6"], "output": ["3", "0"]}]
interview
534
Vasya's older brother, Petya, attends an algorithm course in his school. Today he learned about matchings in graphs. Formally, a set of edges in a graph is called a matching if no pair of distinct edges in the set shares a common endpoint. Petya instantly came up with an inverse concept, an antimatching. In an antimatc...
["def detect_triangle(adj): \n for x in range(len(adj)):\n for y in adj[x]:\n if not set(adj[x]).isdisjoint(adj[y]):\n return True\n\n \nfor _ in range(int(input())):\n n,m=list(map(int,input().split()))\n graph=[[] for i in range(n)]\n for i in range(m):\n u,v=list(map(int,input().split(...
[{"input": ["3", "3 3", "1 2", "1 3", "2 3", "4 2", "1 2", "3 4", "5 0"], "output": ["3", "1", "0"]}]
interview
535
Chef got in the trouble! He is the king of Chefland and Chessland. There is one queen in Chefland and one queen in Chessland and they both want a relationship with him. Chef is standing before a difficult choice… Chessland may be considered a chessboard with $N$ rows (numbered $1$ through $N$) and $M$ columns (numbered...
["def C(n):\n return n*(n-1)//2\n\n\ndef sol():\n equal, mini = False, min(N,M)\n total_ways = 2*C(N * M)\n if N==M:\n equal = True\n ways = 0\n if not equal:\n ways = (N*C(M)+M*C(N))\n diag = 0\n for i in range(2, mini+1):\n diag += 2*C(i)\n for i in range(mini+1,max(N,M)):\n diag += C(mini)\n diag *= 2\n w...
[{"input": ["2", "3 3 2 2", "4 4 2 3"], "output": ["24", "94"]}]
interview
536
2021 was approaching and the world was about to end. So 2 gods Saurabhx and Saurabhy (from Celesta) created the Cyberverse. But this time disappointed with humans both the gods decided not to have humans in this world. So they created a world of cyborgs. A world without humans. Isn’t it interesting? So let us dive into...
["# cook your dish here\nt=int(input())\nfor i in range(t):\n (n,k)=tuple(map(int,input().split()))\n print(k//n)", "# cook your dish here\nt=int(input())\nfor _ in range(t):\n n,k=map(int,input().split())\n print(k//n)", "# cook your dish here\nt=int(input())\nfor i in range(0,t):\n s=input().split(\" \")\n k=list(map...
[{"input": ["1", "5 8"], "output": ["1"]}]
interview
537
Zonal Computing Olympiad 2015, 29 Nov 2014 We say that two integers x and y have a variation of at least K, if |x − y| ≥ K (the absolute value of their difference is at least K). Given a sequence of N integers a1,a2,...,aN and K, the total variation count is the number of pairs of elements in the sequence with variati...
["n,k=[int(x) for x in input().split()]\r\na=[int(x) for x in input().split()]\r\nans=0\r\nfor i in range(n-1):\r\n for j in range(i+1,n):\r\n if(abs(a[i]-a[j])>=k):\r\n ans+=1\r\nprint(ans)\r\n", "n,k=map(int,input().split())\na=list(map(int,input().split()))\na.sort()\ni=0\ns=0\nfor j in range(0,...
[{"input": ["3 1", "3 1 3"], "output": ["2"]}]
interview
538
Sebi goes to school daily with his father. They cross a big highway in the car to reach to the school. Sebi sits in front seat beside his father at driving seat. To kill boredom, they play a game of guessing speed of other cars on the highway. Sebi makes a guess of other car's speed being SG kph, his father FG kph. T...
["# cook your dish here\nn=int(input())\nfor i in range(n):\n S, SG, FG, D, T = map(int, input().split())\n speed = (D*180)/T + S\n if abs(SG-speed) == abs(FG-speed):\n print('DRAW')\n elif abs(SG-speed) > abs(FG-speed):\n print('FATHER')\n else:\n print('SEBI')", "# cook your dish h...
[{"input": ["2", "100 180 200 20 60", "130 131 132 1 72", "", ""], "output": ["SEBI", "FATHER"]}]
interview
539
Bob has got some injury in his leg and due to this he can take exactly M steps in one move. Bob enters a square field of size NxN. The field is only having one gate(for both entrance and exit) at its one of the corners. Bob started walking along the perimeter of square field.(remember Bob can only take exactly M steps...
["# By Prathmesh Maurya\nt=eval(input())\nwhile(t!=0):\n t-=1\n n=eval(input())\n if n%2 == 0:\n print(n*4)\n elif n%4==3:\n print(n)\n else:\n print(n*2)\n", "t = eval(input())\n\ndef gcd(a, b):\n while b:\n t = a % b\n a, b = b, t\n return a\n\nfor i in range(t):\n n = eval(input())\n m = n + 1\n k = n * 4\n g =...
[{"input": ["2", "1", "2"], "output": ["2", "8"]}]
interview
540
Chef has a sequence of positive integers $A_1, A_2, \ldots, A_N$. He wants to choose some elements of this sequence (possibly none or all of them) and compute their MEX, i.e. the smallest positive integer which does not occur among the chosen elements. For example, the MEX of $[1, 2, 4]$ is $3$. Help Chef find the larg...
[" \r\nfor __ in range(int(input())):\r\n n,m=map(int,input().split())\r\n arr=list(map(int,input().split()))\r\n s=set(arr)\r\n mex=-1\r\n ele=1\r\n for i in range(1,n+1):\r\n if i not in s:\r\n mex = i\r\n break\r\n if m>mex:\r\n print(-1)\r\n elif m==mex...
[{"input": ["1", "3 3", "1 2 4", ""], "output": ["3"]}]
interview
541
"Humankind cannot gain anything without first giving something in return. To obtain, something of equal value must be lost. That is alchemy's first law of Equivalent Exchange. In those days, we really believed that to be the world's one, and only truth." -- Alphonse Elric Now, here we have an equivalent exchange law fo...
["for i in range(int(input())):\n n = int(input())\n c = list(map(int, input().split()))\n d = {}\n d[0] = -1\n parity = 0\n ans = 0\n for i in range(n):\n parity ^= 1 << (c[i]-1)\n for t in range(30):\n x = parity^(1<<t)\n if(x in d.keys()):\n ans = max(ans, i - d[x])\n ...
[{"input": ["4", "14", "5 4 2 2 3 2 1 3 2 7 4 9 9 9", "3", "1 2 1", "3", "1 1 1", "5", "1 2 3 4 1"], "output": ["3", "1", "1", "0"]}]
interview
542
Chef has just finished the construction of his new garden. He has sown the garden with patches of the most beautiful carpet grass he could find. He has filled it with patches of different color and now he wants to evaluate how elegant his garden is. Chef's garden looks like a rectangular grid of cells with N rows and M...
["# cook your dish here\nimport sys\nimport math\n\ndef main(grid):\n ans=0\n for i in range(len(grid)):\n for j in range(len(grid[0])):\n first_point=grid[i][j]\n for k in range(j+1,len(grid[0])):\n second_point=grid[i][k]\n if first_point==second_point:...
[{"input": ["3", "2 2", "aa", "aA", "3 3", "aba", "bab", "aba", "4 4", "aabb", "aabb", "bbaa", "bbaa", "", ""], "output": ["0", "1", "4"]}]
interview
543
Ram and Shyam are playing a game of Truth and Dare. In this game, Shyam will ask Ram to perform tasks of two types: - Truth task: Ram has to truthfully answer a question. - Dare task: Ram has to perform a given task. Each task is described by an integer. (If a truth task and a dare task are described by the same intege...
["# cook your dish here\nfor _ in range(int(input())):\n tr=int(input())\n trl=list(map(int,input().split()))\n dr = int(input())\n drl = list(map(int, input().split()))\n ts = int(input())\n tsl = list(map(int, input().split()))\n ds = int(input())\n dsl = list(map(int, input().split()))\n f...
[{"input": ["4", "2", "1 2", "3", "1 3 2", "1", "2", "2", "3 2", "2", "1 2", "3", "1 3 2", "1", "2", "3", "3 2 4", "3", "3 2 5", "2", "2 100", "1", "2", "1", "100", "2", "1 2", "3", "1 3 2", "1", "2", "3", "3 2 2", ""], "output": ["yes", "no", "yes", "yes"]}]
interview
544
An encoder encodes the first $16$ lowercase English letters using $4$ bits each. The first bit (from the left) of the code is $0$ if the letter lies among the first $8$ letters, else it is $1$, signifying that it lies among the last $8$ letters. The second bit of the code is $0$ if the letter lies among the first $4$ l...
["# cook your dish here\ndef decode(L,S):\n str_2=\"\"\n lst=[]\n for i in range(L//4):\n str_1 = \"abcdefghijklmnop\"\n S_1=S[(i*4):(4*(i+1))]\n for j in range(4):\n if(S_1[j]==\"1\"):\n str_1=str_1[len(str_1)//2:len(str_1)]\n else:\n str_1 = str_1[0:len(str_1) // 2]\n str_2=str_2+str_1\n print(str_2)\n\...
[{"input": ["3", "4", "0000", "8", "00001111", "4", "1001"], "output": ["a", "ap", "j"]}]
interview
545
Chef wants to serve mankind by making people immortal by preparing a dish, a dish of life - a dish with the best taste in the universe, one with the smell and splash of fresh water flowing down the springs of the mountain, one with the smell of the best lily flowers of the garden, one that has contained the very essenc...
["for _ in range(int(input())):\n n,k = list(map(int,input().split()))\n array = []\n tot = []\n for _ in range(n):\n temp = list(map(int,input().split()))\n aa = temp[0]\n del(temp[0])\n temp.sort()\n temp.insert(0,aa)\n array.append(temp)\n dic = {}\n array.sort(reverse=True)\n for i in array:\n del(i[0])\n fo...
[{"input": ["3", "3 4", "3 1 2 3", "2 1 3", "2 1 2", "2 3", "3 1 2 3", "2 1 3", "2 3", "2 1 2", "2 1 3"], "output": ["sad", "some", "all"]}]
interview
546
Motu and Patlu are playing with a Magical Ball. Patlu find some interesting pattern in the motion of the ball that ball always bounce back from the ground after travelling a linear distance whose value is some power of $2$. Patlu gave Motu total distance $D$ travelled by the ball and ask him to calculate the minimum nu...
["# cook your dish here\r\ntest=int(input())\r\nfor _ in range(test):\r\n n=int(input())\r\n n=list(bin(n))\r\n ans=n.count('1')\r\n print(ans-1)", "# cook your dish here\nt=int(input())\nfor i in range(t):\n n=int(input())\n binary=bin(n)\n setb=[ones for ones in binary[2:] if ones=='1']\n prin...
[{"input": ["1", "13"], "output": ["2"]}]
interview
547
Two players are playing a game. The game is played on a sequence of positive integer pairs. The players make their moves alternatively. During his move the player chooses a pair and decreases the larger integer in the pair by a positive multiple of the smaller integer in the pair in such a way that both integers in the...
["import sys\n\nt = int(input())\n\ndef g(a,b):\n if (a > b):\n tmp = a\n a = b\n b = tmp\n if (b == a):\n return 0\n if (b % a == 0):\n return int(b/a)-1\n r = g(b%a,a)\n q = int(b/a)\n if (r >= q):\n return q-1\n else:\n return q\n\ndef mex(x):\n n = len(list(x.keys()))\n for i in range(n):\n if (i not in x):...
[{"input": ["3", "1", "2 3", "2", "4 5", "5 6", "2", "2 3", "3 5"], "output": ["NO", "NO", "YES"]}]
interview
548
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 ...
["# cook your dish here\nfor i in range(int(input())):\n n=int(input())\n p=1\n l=n-1\n for j in range(n):\n for k in range(l):\n print(\" \",end='')\n for k in range(p):\n print(\"*\",end='')\n print()\n for k in range(l):\n print(\" \",end='')\n...
[{"input": ["4", "1", "2", "3", "4"], "output": ["*", "*", "*", "*", "***", "***", "*", "*", "***", "***", "*****", "*****", "*", "*", "***", "***", "*****", "*****", "*******", "*******"]}]
interview
549
In a fictitious city of CODASLAM there were many skyscrapers. The mayor of the city decided to make the city beautiful and for this he decided to arrange the skyscrapers in descending order of their height, and the order must be strictly decreasing but he also didn’t want to waste much money so he decided to get the mi...
["import sys\n \nnum=int(sys.stdin.readline())\ns=sys.stdin.readline().split()\nsky=list(map(int,s))\nsky.reverse()\ncuts=0\nchange=0\nt=False\ni=1\n \nwhile i<len(sky):\n if sky[i]<=sky[i-1]:\n for j in range(i-1,-1,-1):\n \n if sky[j]<=sky[i]-(i-j):\n break\n else:\n change+=sky[j]-(sky[i]-(i-j))\n \n...
[{"input": ["5", "1 2 3 4 5"], "output": ["8", "By:", "Chintan,Asad,Ashayam,Akanksha"]}]
interview
550
There is Chef and Chef’s Crush who are playing a game of numbers. Chef’s crush has a number $A$ and Chef has a number $B$. Now, Chef wants Chef’s crush to win the game always, since she is his crush. The game ends when the greatest value of A^B is reached after performing some number of operations (possibly zero), Wh...
["\ndef main():\n t = int(input())\n while (t):\n m, n = map(int, input().split())\n a , b= bin(m)[2:],bin(n)[2:]\n #print(a,b)\n max = m^n\n if len(a)>len(b):\n diff =len(a)-len(b)\n b= (\"0\"*diff)+b\n #print(b)\n elif len(a)<len(b):\n diff =len(b)-len(a)\n a= (\"0\"*diff)+a\n #print(a)\n ll = len(...
[{"input": ["1", "4 5"], "output": ["2 7"]}]
interview
551
Chef Tobby is playing a rapid fire with Bhuvan. He gives Bhuvan a string S and each time, Bhuvan has to guess whether there exists 2 equal subsequences in the string or not. Bhuvan got a perfect score in the game with Chef Tobby. However, Chef Tobby has now asked Bhuvan to write a program that will do this automaticall...
["t = int(input())\n\nfor _ in range(t):\n s = [x for x in input()]\n \n freq = {}\n \n for i in s:\n if i in freq:\n freq[i] += 1\n else:\n freq[i] = 1\n \n flag = 0\n \n for keys, values in freq.items():\n if(values >= 2):\n flag = 1\n break\n \n if(flag == 0):\n print(\"no\")\n else:\n print(\"yes\")", ...
[{"input": ["4", "likecs", "venivedivici", "bhuvan", "codechef"], "output": ["no", "yes", "no", "yes"]}]
interview
552
Chef has gone shopping with his 5-year old son. They have bought N items so far. The items are numbered from 1 to N, and the item i weighs Wi grams. Chef's son insists on helping his father in carrying the items. He wants his dad to give him a few items. Chef does not want to burden his son. But he won't stop botherin...
["def main():\n T = int(input())\n for t in range(T):\n N,K = map(int, input().split())\n W = list(map(int, input().split()))\n W.sort()\n if 2*K > N:\n K = N - K\n kid = sum(W[:K])\n dad = sum(W[K:])\n\n diff = dad - kid\n\n print(diff)\n\n\ndef __starting_point():\n main()\n__starting_point()", "# cook your...
[{"input": ["2", "5 2", "8 4 5 2 10", "8 3", "1 1 1 1 1 1 1 1"], "output": ["17", "2"]}]
interview
553
Consider the following operations on a triple of integers. In one operation, you should: - Choose an integer $d$ and an arithmetic operation ― either addition or multiplication. - Choose a subset of elements of the triple. - Apply the arithmetic operation to each of the chosen elements, i.e. either add $d$ to each of t...
["def eq_solve(v0, v1, u0, u1):\r\n den = u0 - v0\r\n num = u1 - v1\r\n if den != 0:\r\n return num / den\r\n return 1\r\n \r\ndef solve(p, q, r, a, b, c, rs):\r\n if p == a and q == b and r == c:\r\n return rs\r\n if rs >= 2:\r\n return 3\r\n res = 3\r\n adds = [a - p, b - ...
[{"input": ["2", "3 5 7", "6 5 10", "8 6 3", "9 7 8", ""], "output": ["1", "2"]}]
interview
554
Johnny was asked by his math teacher to compute nn (n to the power of n, where n is an integer), and has to read his answer out loud. This is a bit of a tiring task, since the result is probably an extremely large number, and would certainly keep Johnny occupied for a while if he were to do it honestly. But Johnny know...
["from math import log10\nfrom decimal import Decimal\ndef solve(n,k):\n \n mod=10**k\n x=Decimal(n)\n y=x*(x.log10())%1\n p=str(pow(10,y))\n c=0\n first=''\n for v in p:\n if c==k:\n break\n if v==\".\":\n continue\n first+=v\n c+=1\n last=str(pow(n,n,mod)).zfill(k)\n return (first,last)\nqueries=[]\nfor _ in ...
[{"input": ["2", "4 2", "9 3"], "output": ["25 56", "387 489"]}]
interview
555
Sherlock Holmes has decided to start a new academy to some of the young lads. He has conducted several tests and finally selected N equally brilliant students.Now he don't know whether to train all the N students or not. Now since Holmes was in a confusion, Watson came up with an idea. He wanted to test the obedience...
["t = eval(input())\n\nfor i in range(t):\n n = eval(input())\n a = list(map(int, input().split()))\n cnt = 2\n cnt1 = 2\n ll = len(a)\n if ll < 3:\n cnt1 = ll\n else:\n for j in range(2,ll):\n if a[j-1] + a[j-2] == a[j]:\n cnt += 1\n cnt1 = max(cnt1, cnt)\n else:\n cnt1 = max(cnt1, cnt)\n cnt = 2\n p...
[{"input": ["2", "5", "2 3 5 1 2", "3", "1 2 3"], "output": ["3", "3"]}]
interview
556
A robot is initially at $(0,0)$ on the cartesian plane. It can move in 4 directions - up, down, left, right denoted by letter u, d, l, r respectively. More formally: - if the position of robot is $(x,y)$ then u makes it $(x,y+1)$ - if the position of robot is $(x,y)$ then l makes it $(x-1,y)$ - if the position of robot...
["\r\n\r\nz = int(input())\r\ni = 0\r\nwhile i < z:\r\n n = int(input())\r\n p = int(n**(0.5))\r\n if p*(p+1) < n:\r\n p += 1\r\n # print(\"P\", p)\r\n x, y = 0, 0\r\n q = 0\r\n flag = True\r\n if p*(p+1) == n:\r\n # print(\"Even steps, nice\")\r\n q = p\r\n else:\r\n # remaining steps\r\n q = p...
[{"input": ["5", "1", "2", "3", "50", "12233443"], "output": ["0 1", "-1 1", "-1 0", "2 4", "-1749 812"]}]
interview
557
Prof. Sergio Marquina is a mathematics teacher at the University of Spain. Whenever he comes across any good question(with complexity k), he gives that question to students within roll number range i and j. At the start of the semester he assigns a score of 10 to every student in his class if a student submits a questi...
["# cook your dish here\nfor t in range(int(input())):\n n,m=[int(x)for x in input().rstrip().split()]\n s=[]\n for p in range(n):\n s.append(10)\n for c in range(m):\n i,j,k=[int(x)for x in input().rstrip().split()]\n for q in range(i-1,j):\n s[q]=s[q]*k\n print(sum(s)//n)\n \n \n \n \n", "# cook your dish here...
[{"input": ["1", "5 3", "1 3 5", "2 5 2", "3 4 7"], "output": ["202"]}]
interview
558
The city of Siruseri is impeccably planned. The city is divided into a rectangular array of cells with $M$ rows and $N$ columns. Each cell has a metro station. There is one train running left to right and back along each row, and one running top to bottom and back along each column. Each trains starts at some time $T$ ...
["from queue import PriorityQueue\r\nm,n=list(map(int,input().split()))\r\nrr=[]\r\ncc=[]\r\nspeed={'S':3,'O':2,'F':1}\r\nvisited=set()\r\ndp=[]\r\n\r\ndef qwerty(cur,x,y,f):\r\n\tif f==0:\r\n\t\tgg=rr[x][1]+y*rr[x][0]\r\n\t\twhile gg<cur:\r\n\t\t\tgg+=(2*(n-1))*rr[x][0]\r\n\t\treturn gg-cur+rr[x][0]\r\n\telif f==1:\r\...
[{"input": ["3 4", "F 3", "S 2", "O 2", "S 1", "F 2", "O 2", "F 4", "2 3 8 1 1"], "output": ["15"]}]
interview
559
Taxis of Kharagpur are famous for making sharp turns. You are given the coordinates where a particular taxi was on a 2-D planes at N different moments: (x1, y1), (x2, y2), ..., (xN, yN). In between these coordinates, the taxi moves on a straight line. A turn at the i-th (2 ≤ i ≤ N-1) coordinate is said to be a sharp tu...
["import math\nimport copy\ntry:\n import psyco\n psyco.full()\nexcept ImportError:\n pass\n \ndef isSharp(ang):\n return ang > math.pi/4 + 0.00001\n \ndef unitVector(p2, p1):\n d0 = p2[0] - p1[0]\n d1 = p2[1] - p1[1]\n d = math.sqrt(d0*d0 + d1*d1)\n if d != 0:\n return [d0/d, d1/d]\n return [0, 0]\n \ndef compVectors...
[{"input": ["5", "3", "0 0", "1 1", "2 1", "3", "0 0", "1 0", "6 1", "3", "0 0", "1 0", "1 1", "4", "0 0", "1 0", "1 1", "6 1", "6", "0 0", "1 0", "1 1", "2 1", "2 2", "3 2"], "output": ["yes yes", "yes yes", "no yes", "no yes", "no no"]}]
interview
560
Chef is the judge of a competition. There are two players participating in this competition — Alice and Bob. The competition consists of N races. For each i (1 ≤ i ≤ N), Alice finished the i-th race in Ai minutes, while Bob finished it in Bi minutes. The player with the smallest sum of finish times wins. If this total ...
["# cook your dish here\nfor i in range(int(input())):\n N=int(input())\n ALICE=list(map(int,input().split()))\n BOB=list(map(int,input().split()))\n ALICE[ALICE.index(max(ALICE))]=0\n BOB[BOB.index(max(BOB))]=0\n if sum(ALICE)<sum(BOB):\n print(\"Alice\")\n elif sum(BOB)<sum(ALICE):\n print(\"Bob\")\n else:\n print...
[{"input": ["3", "5", "3 1 3 3 4", "1 6 2 5 3", "5", "1 6 2 5 3", "3 1 3 3 4", "3", "4 1 3", "2 2 7"], "output": ["Alice", "Bob", "Draw"]}]
interview
561
This time minions are celebrating Diwali Festival. There are N minions in total. Each of them owns a house. On this Festival, Each of them wants to decorate their house. But none of them have enough money to do that. One of the minion, Kevin, requested Gru for money. Gru agreed for money distribution but he will be giv...
["def find_combinations(list, sum):\n if not list:\n if sum == 0:\n return [[]]\n return []\n return find_combinations(list[1:], sum) + \\\n [[list[0]] + tail for tail in\n find_combinations(list[1:], sum - list[0])]\nfor tc in range(int(input())):\n n,k=list(map(int,input().split()))\n a=list(map(int,input().sp...
[{"input": ["2", "4 9", "5 2 2 4", "4 9", "5 2 18 3"], "output": ["YES", "NO"]}]
interview
562
Chef loves to play chess, so he bought a new chessboard with width M$M$ and height N$N$ recently. Chef considers a chessboard correct if its width (number of columns) is equal to its height (number of rows) and each cell has no side-adjacent cell of the same color (this is the so-called "chess order" which you can see ...
["# cook your dish here\nn,m=map(int,input().split())\nL=[]\nfor i in range(n):\n s=input()\n L.append(s)\n\ncost=[]\nh2=[0]*(m+1)\ncost.append(h2)\nfor i in range(n):\n h=[0]\n for j in range(m):\n if(L[i][j]=='0' and (i+j)%2!=0):\n h.append(1)\n elif(L[i][j]=='1' and (i+j)%2==0):\...
[{"input": ["8 8", "00101010", "00010101", "10101010", "01010101", "10101010", "01010101", "10101010", "01010101", "4", "1 2 0 1001"], "output": ["7", "8", "6", "8"]}]
interview
563
The land of Programmers Army is surrounded by many islands. A unique number is associated with each island. The king of the islands is a very generous person, he donates a certain amount of gold coins to travelers for visiting each island that they visited to. Now, you are appointed as a traveler, who will travel to al...
["# cook your dish here\nfor i in range(int(input())):\n N = int(input())\n l = list(map(int, input().split()))\n for j in range(int(input())):\n q1, q2 = map(int, input().split())\n temp = l[q1 - 1 : q2]\n print(sum(temp))", "# cook your dish here\nfor _ in range(int(input())):\n n...
[{"input": ["1", "4", "10 2 5 50", "2", "1 3", "2 4"], "output": ["17", "57"]}]
interview
564
-----Coal Company ----- The Tunisian Coal Mining company uses a train to ferry out coal blocks from its coal mines. The train has N containers numbered from 1 to N which need to be filled with blocks of coal. Assume there are infinite coal blocks. The containers are arranged in increasing order of capacity, and the i...
["for _ in range(int(input())):\n n,k=list(map(int,input().split()))\n c=list(map(int,input().split()))\n count=1\n for i in range(n):\n if i+1<n:\n if c[i]-c[i+1]>=k or c[i+1]-c[i]>=k:\n continue\n else:\n count+=1\n c[i],c[i+1]=c[i+1],c[i]\n print(count)\n \n"]
[{"input": ["2", "3 2", "5 4 7", "5 1", "5 3 4 5 6"], "output": ["2", "1"]}]
interview
565
"If you didn't copy assignments during your engineering course, did you even do engineering?" There are $Q$ students in Chef's class. Chef's teacher has given the students a simple assignment: Write a function that takes as arguments an array $A$ containing only unique elements and a number $X$ guaranteed to be present...
["def f(a,y,index,sorted_pos):\n #print(a,y,index,sorted_pos)\n n=len(a)\n low=0\n high=n-1\n L,R=0,0\n l,r=0,0\n while(low<=high):\n mid=(low+high)//2\n #print(low,high,mid)\n if(a[mid]== y):\n break\n elif(mid > index[y]):\n high=mid-1\n L+=1\n #print(\"L\")\n if(a[mid] <y):\n l+=1\n #print(\" l \...
[{"input": ["1", "7 7", "3 1 6 7 2 5 4", "1", "2", "3", "4", "5", "6", "7"], "output": ["0", "1", "1", "2", "1", "0", "0"]}]
interview
566
Chef likes strings a lot but he likes palindromic strings more. Today, Chef has two strings A and B, each consisting of lower case alphabets. Chef is eager to know whether it is possible to choose some non empty strings s1 and s2 where s1 is a substring of A, s2 is a substring of B such that s1 + s2 is a palindromic s...
["t=int(input())\nfor _ in range (t):\n str1=input()\n str2=input()\n res='No'\n for i in str1:\n if i in str2:\n res='Yes'\n break\n print(res)\n", "for _ in range(int(input())):\n str1=input()\n str2=input()\n res='No'\n for i in str1:\n if i in str2:\n res='Yes'\n break\n print(res)", "# cook your dish her...
[{"input": ["3", "abc", "abc", "a", "b", "abba", "baab"], "output": ["Yes", "No", "Yes"]}]
interview
567
Chef's new hobby is painting, but he learned the fact that it's not easy to paint 2D pictures in a hard way, after wasting a lot of canvas paper, paint and of course time. From now on, he decided to paint 1D pictures only. Chef's canvas is N millimeters long and is initially all white. For simplicity, colors will be re...
["# cook your dish here\nimport sys\nimport math\n\ndef main(arr):\n for i in range(1,len(arr)-1):\n if arr[i]==arr[i-1] and arr[i]==arr[i+1]:\n return \"Yes\"\n return \"No\"\n\ntest=int(input())\nfor _ in range(test):\n b=int(input())\n arr=list(map(int,input().split()))\n print(main(...
[{"input": ["3", "4", "1 5 5 5", "4", "1 1 1 5", "3", "5 5 2", ""], "output": ["Yes", "Yes", "No"]}]
interview
568
Kefaa has developed a novel decomposition of a tree. He claims that this decomposition solves many difficult problems related to trees. However, he doesn't know how to find it quickly, so he asks you to help him. You are given a tree with $N$ vertices numbered $1$ through $N$. Let's denote an edge between vertices $u$ ...
["\ntest=int(input())\nfor t in range(test):\n n= int(input())\n\n adj=[[] for i in range(n+1)]\n\n for _ in range(n-1):\n a,b=list(map(int,input().split()))\n adj[a].append(b)\n adj[b].append(a)\n \n\n #print(adj)\n root=1\n q,s=[root],set([root])\n\n for x in q:\n adj[x]= [p for p in adj[x] if p not in s]\n q.ex...
[{"input": ["2", "4", "1 2", "1 3", "1 4", "7", "1 2", "2 3", "1 4", "4 5", "1 6", "6 7"], "output": ["YES", "1 2 3 4", "NO"]}]
interview
569
Sandu, a teacher in Chefland introduced his students to a new sequence i.e. 0,1,0,1,2,0,1,2,3,0,1,2,3,4........ The Sequence starts from 0 and increases by one till $i$(initially i equals to 1), then repeat itself with $i$ changed to $i+1$ Students being curious about the sequence asks the Nth element of the sequence. ...
["from math import sqrt\n\nfor _ in range(int(input())):\n n = int(input())\n\n x = int(sqrt(2 * n))\n\n while x * (x+1) // 2 <= n:\n x += 1\n\n while x * (x+1) // 2 > n:\n x -= 1\n\n n -= x * (x+1) // 2\n\n print(n)\n", "\"\"\"\nProblem Statement: https://www.codechef.com/ENCD2020/problems/ECAPR203\nAuthor: striker\...
[{"input": ["5", "8", "9", "20", "32", "109"], "output": ["2", "3", "5", "4", "4"]}]
interview
570
Ayu loves distinct letter sequences ,a distinct letter sequence is defined by a sequence of small case english alphabets such that no character appears more then once. But however there are two phrases that she doesn't like these phrases are "kar" and "shi" and she is given a sequence of distinct characters and she won...
["# cook your dish here\r\nfrom collections import deque, defaultdict\r\nfrom math import sqrt, ceil,factorial\r\nimport sys\r\nimport copy\r\ndef get_array(): return list(map(int, sys.stdin.readline().strip().split()))\r\ndef get_ints(): return map(int, sys.stdin.readline().strip().split())\r\ndef input(): return sys....
[{"input": ["2", "karp", "abcd"], "output": ["22", "24"]}]
interview
571
The chef has a recipe he wishes to use for his guests, but the recipe will make far more food than he can serve to the guests. The chef therefore would like to make a reduced version of the recipe which has the same ratios of ingredients, but makes less food. The chef, however, does not like fractions. The original rec...
["#! /usr/bin/env python\n\nfrom sys import stdin\nfrom functools import reduce\n\ndef gcd(a,b):\n\twhile b!=0:\n\t\ta,b=b,a%b\n\treturn a\n\t\ndef gcdl(l):\n\treturn reduce(gcd, l[1:],l[0])\n\ndef __starting_point():\n\tT=int(stdin.readline())\n\tfor case in range(T):\n\t\tnumbers=list(map(int, stdin.readline().split(...
[{"input": ["3", "2 4 4", "3 2 3 4", "4 3 15 9 6", ""], "output": ["1 1", "2 3 4", "1 5 3 2"]}]
interview
572
Today is Chef's birthday. His mom has surprised him with truly fruity gifts: 2 fruit baskets. The first basket contains N apples, and the second one contains M oranges. Chef likes apples and oranges very much but he likes them equally, and therefore, wants to have the minimum possible difference between the number of a...
["for _ in range(int(input())):\n a,o,g=map(int,input().split())\n while g>0:\n if a<o:\n a+=1\n g-=1\n elif o<a:\n o+=1\n g-=1\n else:\n break\n print(abs(a-o))", "# cook your dish here\ntest_case = int(input())\nfor i in range(test_case):\n apple ,orange ,coin = map(int,input().split())\n k = abs(apple -...
[{"input": ["3", "3 4 1", "5 2 1", "3 4 3"], "output": ["0", "2", "0"]}]
interview
573
After acquiring an extraordinary amount of knowledge through programming contests, Malvika decided to harness her expertise to train the next generation of Indian programmers. So, she decided to hold a programming camp. In the camp, she held a discussion session for n members (n-1 students, and herself). They are sitti...
["for _ in range(int(input())):\n n,m=map(int, input().split())\n if n==1:\n print(0)\n elif n==2:\n print(m)\n else:\n print(m*2+n-3)", "# cook your dish here\nfor i in range(int(input())):\n n,m=list(map(int,input().split()))\n if(n==1):\n print(0)\n continue\n if(n==2):\n print(m)\n continue\n ans=(n-1)+2*(m-...
[{"input": ["2", "2 1", "3 2"], "output": ["1", "4"]}]
interview
574
Find out the maximum sub-array of non negative numbers from an array. The sub-array should be continuous. That is, a sub-array created by choosing the second and fourth element and skipping the third element is invalid. Maximum sub-array is defined in terms of the sum of the elements in the sub-array. Sub-array A is...
["for t in range(int(input())):\n n=int(input())\n a=list(map(int,input().split()))\n s=0\n l=[]\n for i in range(n):\n if (a[i]<0):\n e=i\n ss=sum(a[s:e])\n l.append((ss,e-s,n-s))\n s=i+1\n e=n\n ss=sum(a[s:e])\n l.append((ss,e-s,n-s))\n x=max(l)\n s=n-x[2]\n e=x[1]+s\n for i in range(s,e):\n print(a[i], end...
[{"input": ["1", "6", "1 2 5 -7 2 3"], "output": ["1 2 5"]}]
interview
575
Chef found a strange string yesterday - a string of signs s, where each sign is either a '<', '=' or a '>'. Let N be the length of this string. Chef wants to insert N + 1 positive integers into this sequence and make it valid. A valid sequence is a sequence where every sign is preceded and followed by an integer, and t...
["for _ in range(int(input())):\n st=input().replace(\"=\",\"\")\n if not len(st):print(1)\n else:\n cu=mx=1\n for j in range(1,len(st)):\n if st[j]==st[j-1]:cu+=1\n else:mx=max(mx,cu);cu=1\n print(max(mx+1,cu+1))\n", "r=int(input())\nfor z in range(r):\n s=input()\n s=s.replace('=','')\n l=list(s)\n final=0\n a...
[{"input": ["4", "<<<", "<><", "<=>", "<=<"], "output": ["4", "2", "2", "3"]}]
interview
576
You came across this story while reading a book. Long a ago when the modern entertainment systems did not exist people used to go to watch plays in theaters, where people would perform live in front of an audience. There was a beautiful actress who had a disability she could not pronounce the character $'r'$. To win he...
["for _ in range(int(input())):\r\n S = input()\r\n n = len(S)\r\n a = n - S.count('a')\r\n print(2 ** n - 2 ** a)", "for _ in range(int(input())):\n s=input()\n n=len(s)\n c=s.count('a')\n l=n-c\n print(pow(2,n)-pow(2,l))\n\n", "for _ in range(int(input())):\n s=input()\n n=len(s)\n ...
[{"input": ["2", "abc", "aba"], "output": ["4", "6"]}]
interview
577
Not everyone probably knows that Chef has younder brother Jeff. Currently Jeff learns to read. He knows some subset of the letter of Latin alphabet. In order to help Jeff to study, Chef gave him a book with the text consisting of N words. Jeff can read a word iff it consists only of the letters he knows. Now Chef is cu...
["knows=input()\nn=eval(input())\nwhile n!=0:\n n=n-1\n word=input()\n for x in word:\n ctr=0\n for y in knows:\n if x==y:ctr=ctr+1;break\n if ctr==0:print('No');break\n else: print('Yes')", "import collections\n\ndef alphabet():\n s = input()\n d = collections.defaultdict(lambda : 0)\n for i in s:\n d[i]+=1\n n ...
[{"input": ["act", "2", "cat", "dog"], "output": ["Yes", "No"]}]
interview
578
Chef has a calculator which has two screens and two buttons. Initially, each screen shows the number zero. Pressing the first button increments the number on the first screen by 1, and each click of the first button consumes 1 unit of energy. Pressing the second button increases the number on the second screen by the n...
["# cook your dish here\nfor i in range(int(input())):\n n,b=map(int,input().split())\n ans=round(n/(2*b))*(n-b*round((n/(2*b))));\n print(ans)", "import math\nn=int(input())\nfor i in range(n):\n a,b=list(map(int,input().split()))\n if a>b:\n a1=math.ceil(a/(2*b))\n b1=a-a1*b\n a2=math.floor(a/(2*b))\n b2=a-a2*b\n...
[{"input": ["3", "10 2", "8 5", "6 1"], "output": ["12", "3", "9"]}]
interview
579
Chef has a circular sequence $A$ of $N$ non-negative integers $A_1, A_2, \ldots, A_N$ where $A_i$ and $A_{i+1}$ are considered adjacent, and elements $A_1$ and $A_N$ are considered adjacent. An operation on position $p$ in array $A$ is defined as replacing $A_p$ by the bitwise OR of elements adjacent to $A_p$. Formally...
["t=int(input())\ndef check():\n pref = [0]*n\n pref[0]=a[0]\n suff = [0]*n\n suff[-1]=a[-1]\n for i in range (1,n):\n pref[i] = pref[i-1]|a[i]\n suff[n-i-1] = suff[n-i]|a[n-i-1]\n if suff[1]==k:\n return 0\n elif pref[n-2]==k:\n return n-1\n else:\n for i in r...
[{"input": ["5", "3 6", "2 1 6", "3 6", "2 1 5", "3 7", "2 4 6", "3 7", "1 2 4", "3 7", "1 2 6"], "output": ["2 1 3", "-1", "-1", "-1", "2 3 1"]}]
interview
580
The chef is trying to decode some pattern problems, Chef wants your help to code it. Chef has one number K(odd) 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 li...
["from sys import stdin, stdout\nfrom math import floor, gcd, fabs, factorial, fmod, sqrt, inf, log\nfrom collections import defaultdict as dd, deque\nfrom heapq import merge, heapify, heappop, heappush, nsmallest\nfrom bisect import bisect_left as bl, bisect_right as br, bisect\nmod = pow(10, 9) + 7\nmod2 = 998244353\...
[{"input": ["4", "1", "3", "5", "7"], "output": ["1", "111", "111", "111", "11111", "11 11", "1 1 1", "11 11", "11111", "1111111", "11 11", "1 1 1 1", "1 1 1", "1 1 1 1", "11 11", "1111111"]}]
interview
581
Humpy, the little elephant, has his birthday coming up. He invited all his cousins but doesn’t know how many of them are really coming as some of them are having exams coming up. He will only get to know how many of them are coming on the day of his birthday. He ordered sugarcane for his party, of length L. Humpy’s m...
["# your code goes here\nfrom sys import stdin, stdout\nn = int(stdin.readline())\nwhile n:\n n -= 1\n k, l, e = map(int, stdin.readline().strip().split(' '))\n a = map(int, stdin.readline().strip().split(' '))\n x = float(l) / float(e + sum(a))\n if x - int(x):\n stdout.write(\"NO\\n\")\n else:\n stdout.write(\"YES\...
[{"input": ["2", "4 10 2", "2 2 3 1", "4 12 3", "6 5 7 3"], "output": ["YES", "NO"]}]
interview
582
You may have tried your level best to help Chef but Dr Doof has managed to come up with his masterplan in the meantime. Sadly, you have to help Chef once again. Dr Doof has designed a parenthesis-inator. It throws a stream of $N$ brackets at the target, $1$ bracket per second. The brackets can either be opening or clos...
["import sys\nimport bisect as bi\nimport math\nfrom collections import defaultdict as dd\ninput=sys.stdin.readline\n##sys.setrecursionlimit(10**7)\ndef cin():\n return list(map(int,sin().split()))\ndef ain(): \n return list(map(int,sin().split()))\ndef sin():\n return input()\ndef inin():\n retu...
[{"input": ["1", ")())((()", "3", "1 7 6"], "output": ["3", "8", "-1"]}]
interview
583
Let's call a sequence good if the sum of all its elements is $0$. You have a sequence of integers $A_1, A_2, \ldots, A_N$. You may perform any number of operations on this sequence (including zero). In one operation, you should choose a valid index $i$ and decrease $A_i$ by $i$. Can you make the sequence good using the...
["n=int(input())\nfor i in range(n):\n t=int(input())\n m=list(map(int,input().split()))\n p,q=0,0\n if t==1:\n if m[0]>=0:\n print('YES')\n else:\n print('NO')\n else:\n for i in m:\n if i<0:\n q+=i\n else:\n p+=i\n if p>=abs(q):\n print('YES')\n else:\n print('NO')", "n=int(input())\nfor i in r...
[{"input": ["2", "1", "-1", "2", "1 2"], "output": ["NO", "YES"]}]
interview
584
Given a binary string $S$ consisting of only 1’s and 0’s where 1 represents a Square and 0 represents a Circle. The diameter of the circle and the side of the square must be any integer (obviously > 0) . You will have to perfectly inscribe (as shown in the example below) the respective geometric figure at $S$$i+1$ insi...
["for z in range(int(input())):\n s = input()\n n = len(s)\n i = 0\n while i<n and s[i]=='1':\n i+=1\n if i==0:\n print(0)\n else:\n k = 0\n while i<n and s[i]=='0':\n i+=1\n k+=1\n print(k)\n", "# cook your dish h\nfor _ in range(int(input())):...
[{"input": ["3", "1110", "0010", "1001000"], "output": ["1", "0", "2"]}]
interview
585
You are an evil sorcerer at a round table with $N$ sorcerers (including yourself). You can cast $M$ spells which have distinct powers $p_1, p_2, \ldots, p_M$. You may perform the following operation any number of times (possibly zero): - Assign a living sorcerer to each positive integer cyclically to your left starting...
["import functools\n\ndef gcd(x,y):\n if(y == 0):\n return x\n return gcd(y, x%y)\n\nfor _ in range(int(input())):\n n, m= map(int, input().split())\n p = list(map(int, input().split()))\n \n ans = functools.reduce(lambda x,y: gcd(x, y), p)\n \n if(ans <= n):\n print(n-ans)\n else:\n f = [1]\n for k in range(ans//2...
[{"input": ["5", "4 1", "5", "6 2", "2 4", "1 4", "7 16 8 29", "1000000000 1", "998244353", "1 1", "20201220"], "output": ["3", "4", "0", "1755647", "0"]}]
interview
586
Teacher Sungjae wanted to hold a programming competition for his students where every participant need to be included into team. The participants submitted their team names before the deadline. After the competition ran for half an hour, (It is assured that each registered team will submit absolutely once within half ...
["# cook your dish here\nfor t in range(int(input())):\n n,k=map(int,input().split())\n a=[]\n sr=[]\n for i in range(k):\n x,y=input().split()\n y=int(y)\n a.append([10**10-y,x])\n sr.append(sorted(x))\n for i in range(n-k):\n x,y=input().split()\n y=int(y)\n ...
[{"input": ["1", "10 5", "amigoes 1", "bannermen 1", "monarchy 4", "outliers 5", "iniciador 10", "aegimos 2", "iiiacdnor 1", "eilorstu 1", "gimosae 3", "mnachroy 7"], "output": ["iniciador 11", "monarchy 11", "amigoes 6", "outliers 6", "bannermen 1"]}]
interview
587
On a planet called RUIZ LAND, which is ruled by the queen, Erika Ruiz. Each person on that planet has a strength value (strength value >0). That planet has a special rule made by the queen that a boy and a girl will form a couple if their Hate value is a prime number where $Hate$ is given by the formula:- Hate = (boy's...
["n=int(input())\r\na=list(map(int,input().split()))\r\nc=[]\r\nfor i in range(len(a)):\r\n if a[i]==2:\r\n c.append(1)\r\n else:\r\n c.append(a[i]^2)\r\nprint(*c)", "import copy\r\nn=int(input())\r\na=list(map(int,input().split()))\r\nc=[]\r\nfor i in range(len(a)):\r\n if a[i]==2:\r\n c....
[{"input": ["2", "10 16"], "output": ["8 18"]}]
interview
588
Vasya has ordered a pizza delivery. The pizza can be considered a perfect circle. There were $n$ premade cuts in the pizza when it was delivered. Each cut is a straight segment connecting the center of the pizza with its boundary. Let $O$ be the center of the pizza, $P_i$ be the endpoint of the $i$-th cut lying on the ...
["def gcd(a, b):\n if a == 0:\n return b\n return(gcd(b % a, a))\n\nt = int(input())\nfor T in range(t):\n n = int(input())\n l = [int(x) for x in input().split()]\n\n ang = []\n for i in range(1, n):\n ang.append(l[i] - l[i - 1])\n ang.append(360 - (l[-1] - l[0]))\n ang.sort()\n if ang == ang[::-1]:\n print(0)\n c...
[{"input": ["3", "4", "0 90 180 270", "2", "90 210", "2", "0 1"], "output": ["0", "1", "358"]}]
interview
589
Digory Kirke and Polly Plummer are two kids living next door to each other. The attics of the two houses are connected to each other through a passage. Digory's Uncle Andrew has been secretly doing strange things in the attic of his house, and he always ensures that the room is locked. Being curious, Digory suspects th...
["# cook your dish here\nfor i in range(int(input())):\n s = input()\n m = 0\n p = 0\n d = 0\n l = []\n for i in range(len(s)):\n if(s[i] == \".\"):\n m = m+1\n elif(s[i] == \"#\"):\n l.append(m)\n m=0\n for i in range(len(l)):\n if(l[i]>p):\n p = l[i]\n d = d+1\n print(d)\n \n \n \n \n", "t=int(inpu...
[{"input": ["4", "####", "##.#..#", "##..#.#", "##.#....#"], "output": ["0", "2", "1", "2"]}]
interview
590
Chef has an array A consisting of N integers (1-based indexing). He asks you to perform the following operation M times: for i = 2 to N: Ai = Ai + Ai-1 Your task is to find the xth element of the array (i.e., Ax) after performing the above operation M times. As the answer could be large, please output it modulo 10...
["for _ in range(int(input())):\n n,x,m = map(int,input().split())\n a = list(map(int,input().split()))\n for _ in range(m):\n for i in range(1,n):\n a[i] = a[i] + a[i-1]\n print(a[x-1]%(10**9+7))", "def modInverse(a, m) : \n m0 = m \n y = 0\n x = 1\n\n if (m == 1) : \n return 0\n\n while (a > 1) : \n q = a // m \...
[{"input": ["2", "3 2 3", "1 2 3", "3 3 3", "1 2 3"], "output": ["5", "15"]}]
interview
591
Ganesh lives in Gopalmath. He is looking for Jojo. So he decides to collect Aadhar Card Information of all the citizens of India from UIDAI. Someone told Ganesh that the sum of all the digits of Jojo’s Aadhar number is divisible by 10 and it is greater than zero. After finding all Aadhar numbers which are divisible by ...
["for _ in range(int(input())):\n N = input()\n num = list(N)\n s=0\n for n in num:\n if n.isnumeric():\n s+=int(n)\n #print(s)\n x=(10-s%10)%10\n print(int(N)*10+int(x))", "# cook your dish here\ndef s(n):\n sn=str(n)\n r=0\n for i in sn:\n r+=int(i)\n return r\n \nfor _ in range(int(input())):\n n=int(input())\n...
[{"input": ["1", "3"], "output": ["37"]}]
interview
592
Teddy and Tracy like to play a game based on strings. The game is as follows. Initially, Tracy writes a long random string on a whiteboard. Then, each player starting with Teddy makes turn alternately. Each turn, the player must erase a contiguous substring that exists in the dictionary. The dictionary consists of N wo...
["import sys\n\ndef mex(S,W,C,start,end):\n \"\"\"Returns Nim-number of S[start:end]\"\"\"\n key=(start,end)\n try:\n return C[key]\n except KeyError:\n pass\n A=set()\n for s in range(start,end):\n for e in range(start+1,end+1):\n if S[s:e] not in W: continue\n ...
[{"input": ["3", "codechef", "2", "code", "chef", "foo", "1", "bar", "mississippi", "4", "ssissi", "mippi", "mi", "ppi", "", ""], "output": ["Tracy", "Tracy", "Teddy"]}]
interview
593
Mathison recently inherited an ancient papyrus that contained some text. Unfortunately, the text was not a pangram. Now, Mathison has a particular liking for holoalphabetic strings and the text bothers him. The good news is that Mathison can buy letters from the local store in order to turn his text into a pangram. Ho...
["# cook your dish here\n# cook your dish here\nfor i in range(int(input())):\n a=list(map(int,input().split()))\n x=input()\n t=0\n for i in range(ord('a'),ord('z')+1):\n if chr(i) not in x:\n t+=a[i-97]\n print(t)", "t=int(input())\nx='abcdefghijklmnopqrstuvwxyz'\nfor i in range(t):\n a=0\n b=input().split()\n c=i...
[{"input": ["2", "1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26", "abcdefghijklmopqrstuvwz", "1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26", "thequickbrownfoxjumpsoverthelazydog"], "output": ["63", "0"]}]
interview
594
You are given a sequence of integers $A_1,A_2,…,A_N$ and a magical non-zero integer $x$ You have to select a subsegment of sequence A (possibly empty), and replace the elements in that subsegment after dividing them by x. Formally, replace any one subsegment $A_l, A_{l+1}, ..., A_r$ with $A_l/x, A_{l+1}/x, ..., A_r/x$ ...
["def solve(a,n):\n max1=curr=a[0]\n for i in range(1,n):\n curr=max(a[i],curr+a[i])\n max1=max(max1,curr)\n return max1\n \nn,k = list(map(int,input().split()))\na = list(map(int,input().split()))\nprint(sum(a)-solve(a,n)+solve(a,n)/k)\n", "# cook your dish here\ndef atharva(a:list,n:int): \n msf = mh = a[0]\n for i...
[{"input": ["3 2", "1 -2 3"], "output": ["0.5"]}]
interview
595
You are given a string $S$. Find the number of ways to choose an unordered pair of non-overlapping non-empty substrings of this string (let's denote them by $s_1$ and $s_2$ in such a way that $s_2$ starts after $s_1$ ends) such that their concatenation $s_1 + s_2$ is a palindrome. Two pairs $(s_1, s_2)$ and $(s_1', s_2...
["def binarySearch(arr, l, r, x):\n mid=0\n while l <= r: \n mid = l + (r - l)//2; \n if arr[mid] == x: \n return mid+1 \n elif arr[mid] < x: \n l = mid + 1\n else: \n r = mid - 1\n if mid!=len(arr):\n if arr[mid]<x:\n return mid+1\n return mid\ns=input()\nstrt=[]\nend=[]\nplc=[]\nlandr=[]\nl2r=[]\nlr=[]\n...
[{"input": ["abba"], "output": ["7"]}]
interview
596
Chef is playing a game on the non-negative x-axis. It takes him $1$ second to reach from $i^{th}$ position to $(i-1)^{th}$ position or $(i+1)^{th}$ position. The chef never goes to the negative x-axis. Also, Chef doesn't stop at any moment of time. The movement of chef can be described as follows. - At the start he is ...
["import sys\nfrom random import choice,randint\ninp=sys.stdin.readline\nout=sys.stdout.write\nflsh=sys.stdout.flush\n \nsys.setrecursionlimit(10**9)\ninf = 10**20\neps = 1.0 / 10**10\nmod = 10**9+7\ndd = [(-1,0),(0,1),(1,0),(0,-1)]\nddn = [(-1,0),(-1,1),(0,1),(1,1),(1,0),(1,-1),(0,-1),(-1,-1)]\n \ndef MI(): return map...
[{"input": ["5", "0 1", "1 1", "2 1", "1 3", "4 6"], "output": ["0", "1", "4", "5", "46"]}]
interview
597
Chef Ada is building a new restaurant in the following way: - First, $N$ points $X_1, X_2, \ldots, X_N$ are chosen on the $x$-axis. - Then, $N$ columns (numbered $1$ through $N$) are made. For simplicity, the columns are represented as vertical segments; for each valid $i$, the height of the $i$-th segment is $H_i$. - ...
["# cook your dish here\nt = int(input())\nfor _ in range(t):\n n = int(input())\n a = []\n b = []\n for i in range(n):\n x,y = list(map(int, input().split()))\n a.append(x)\n b.append(y)\n b.sort()\n xcor = []\n xcor.append(a[1]-a[0])\n xcor.append(a[n-1]-a[n-2])\n for i in range(1,n-1):\n xcor.append(a[i+1]-a[i-1...
[{"input": ["1", "5", "1 1", "2 2", "3 3", "4 4", "5 5"], "output": ["27"]}]
interview
598
Roman has no idea, why this problem is called Stone. He also has no idea on how to solve the followong problem: given array of N integers A and a number K. During a turn the maximal value over all Ai is chosen, let's call it MAX. Then Ai = MAX - Ai is done for every 1 <= i <= N. Help Roman to find out how will the arr...
["n, k = list(map(int, input().split()))\nA = list(map(int, input().split()))\nmaximum = max(A)\nminimum = min(A)\nif k == 0:\n for i in A:\n print(i, end=' ')\nelif k&1:\n for i in A:\n print(maximum - i, end=' ')\nelse:\n for i in A:\n print(i - minimum, end=' ')\n", "n,k = input().split()\nn=int(n)\nk=int(k)\na=l...
[{"input": ["4 1", "5 -1 7 0"], "output": ["2 8 0 7"]}]
interview
599
Chef just got a box of chocolates as his birthday gift. The box contains $N$ chocolates in a row (numbered $1$ through $N$), where $N$ is even. For each valid $i$, the $i$-th chocolate has a sweetness value $W_i$. Chef wants to eat all the chocolates in the first half of the box and leave all chocolates in the second h...
["from collections import deque\nt=int(input())\n\nfor i in range(t):\n n=int(input())\n N=[i for i in range(1, n+1)]\n w=list(map(int, input().split()))\n max_sweetness=max(w)\n sizes=[]\n cnt=0\n for i in range(n):\n if w[i]!=max_sweetness:\n cnt+= 1 \n else:\n sizes.append(cnt)\n cnt=0\n \n if cnt!=0:\n siz...
[{"input": ["2", "6", "1 1 2 1 1 1", "6", "1 1 2 1 1 2"], "output": ["3", "0"]}]
interview