Repository: parasjain-12/HackerEarth-Solution Branch: master Commit: e2156145ab71 Files: 153 Total size: 64.6 KB Directory structure: gitextract_2juf35yw/ ├── 2 arrays.py ├── @K R-r-riddikulus! once again.py ├── A Special Number.py ├── A beauty factor.py ├── Achhe Din.py ├── Add Alternate Elements of 2-Dimensional Array.py ├── Alien language.py ├── All Vowels.py ├── AltF4 and the beetles.py ├── Anagrams.py ├── Anshul, Usama And Punishment - A.py ├── Anti-palindrome strings.py ├── Arjit and Apex.py ├── Array operations.py ├── ArrayGame.py ├── Bag Of Numbers.py ├── Best Index.py ├── Biased Chandan.py ├── Binary Queries.py ├── Breakup App.py ├── Bricks Game.py ├── CAn you solve it?.py ├── Chandu and Consecutive Letters.py ├── Char Sum.py ├── Charged Up Array.py ├── Cost of balloons.py ├── Count Divisors.py ├── Counting Triangles.py ├── Crazy Kangaroo.py ├── Dedication Level = Infinity.py ├── Determining numbers.py ├── Difficult Characters.py ├── Digital Sequence.py ├── Distribute chocolates.py ├── Divisibility.py ├── Divisible.py ├── Does it Divide?.py ├── Duration.py ├── EEDC Lab.py ├── Easy Sum Set Problem.py ├── Easy one.py ├── Equal elements ├── Equalize strings.py ├── Erasing an array.py ├── Exchanging money.py ├── Factorial!.py ├── Find Product.py ├── Find the String.py ├── Finding the Subarrays.py ├── Finding vaccines.py ├── Fredo and Large Numbers.py ├── Frequency of Students.py ├── Going to office.py ├── Grid and phrase.py ├── Hackers with Bits.py ├── Hamiltonian and Lagrangian.py ├── Hello.py ├── Help Jarvis!.py ├── Honey Bees.py ├── ICPC Team Management.py ├── James and the menus.py ├── LAL Evaluation.py ├── Length of Valley ├── Life, the Universe, and Everything.py ├── Lift queries.py ├── Little Monk and Balanced Parentheses.py ├── Little_Jhool_psychic_powers.py ├── Long ATM Queue.py ├── Lunch boxes.py ├── MYSTERY.py ├── Manna's First Name.py ├── Mark The Answer.py ├── Mathematically beautiful numbers.py ├── Maximize the Earning.py ├── Maximize the modulo function.py ├── Maximum Of K- size subarrays (Deque).py ├── Maximum Sum.py ├── Maximum goodness.py ├── Maximum occurrence.py ├── Maze Problem.py ├── Memorise Me!.py ├── Micro and Array Update.py ├── Min-Max.py ├── Mind Palaces.py ├── Minimum Add to Make Parentheses Valid.py ├── Minimum moves.py ├── Modify Sequence.py ├── Modulo Strength.py ├── Monk And the operations.py ├── Monk Takes a Walk.py ├── Monk Teaches Palindrome.py ├── Monk and Inversions.py ├── Monk and Lucky Minimum.py ├── Monk and Power of Time.py ├── Monk and Rotation.py ├── Monk and Welcome Problem.py ├── Most Frequent.py ├── Multiple occurrences.py ├── N - Co Ordinates (Pair Practice).py ├── Neutralisation of charges.py ├── Not in Range.py ├── Number of cycles.py ├── Number of steps.py ├── Pair Sum.py ├── Pairs.py ├── Palindromic String.py ├── Passing the Parcel.py ├── Pepper and Contiguous Even Subarray.py ├── Perfect Subarray.py ├── Permute the Array.py ├── Polygon Possibility ├── Prasun the detective.py ├── Prime Number.py ├── Priority Interview.py ├── README.md ├── Rain Sound.py ├── Rectangles to squares.py ├── Reversed Linked List.py ├── Robotic moves.py ├── Roy and Profile Picture.py ├── Roy and Symmetric Logos.py ├── Saul Goodman's Problem Statement.py ├── Save Mrinal.py ├── Seating Arrangement.py ├── Seven-Segment Display.py ├── Simon cannot sleep.py ├── Simple Search.py ├── SnackDown Contest.py ├── Speed.py ├── Split Houses.py ├── Stack and Queue .py ├── Statistics.py ├── Strange Game.py ├── String.py ├── Sumita and equal array.py ├── Takeoff.py ├── Terrible Chandu.py ├── The Amazing Race.py ├── The Monk and Kundan.py ├── The best Internet Browser.py ├── Thief and Warehouses.py ├── Toggle String.py ├── Two Strings.py ├── VC pairs.py ├── Vada Pav List.py ├── Weighing the Stones.py ├── Wet Clothes.py ├── Word Queries.py ├── balanced brackets.py ├── e-maze-in.py ├── square sub matrix.py ├── super reduced strings.py └── zoos.py ================================================ FILE CONTENTS ================================================ ================================================ FILE: 2 arrays.py ================================================ n = int(input()) arr = list(map(int,input().split())) brr = list(map(int,input().split())) aneg = 0 bneg = 0 asum = 0 bsum = 0 for i in range(n): if arr[i]== -1: aneg +=1 else: asum +=arr[i] if brr[i] == -1: bneg +=1 else: bsum +=brr[i] d = abs(asum - bsum) if aneg ==bneg: print("Infinite") if (bsum-asum)>0 and aneg != bneg: if aneg == 0: print(0) if aneg==1: print(1) elif aneg>1: print(d+1) if (asum-bsum)>0 and aneg!=bneg: if bneg == 0: print(0) if bneg==1: print(1) elif bneg>1: print(d+1) ================================================ FILE: @K R-r-riddikulus! once again.py ================================================ n,d = map(int,input().split()) a = list(map(int,input().split())) print(*(a[d:]+a[:d])) #print(*(a[d:]+a[:d])) ================================================ FILE: A Special Number.py ================================================ ''' # Sample code to perform I/O: name = input() # Reading input from STDIN print('Hi, %s.' % name) # Writing output to STDOUT # Warning: Printing unwanted or ill-formatted data to output will cause the test cases to fail ''' # Write your code here def getSum(n): su = 0 while n!=0: rem = n%10 su += rem n = n//10 return su n = int(input()) li = [] for i in range(n): x = int(input()) for i in range(x,10000000000): su = getSum(i) if su%4 == 0: print(i) break ================================================ FILE: A beauty factor.py ================================================ # Simple Brute Force implementation #First we check that whether the length (k) is 9 or not because if k ==9 then we can have only 1 number that is 123456789 becz number must be distinct and greater than zero. # Step 2 : If start with the smallest number and remove all zero and indentical number (1231 here 1 is repeated two times which should not be as each number must be distinct) and # check there beautyFactor . if they match print it else increment the number and recursivly check it len of the number is equal to k. def checkBeautyFactor(n): su = 0 for i in str(n): su+= int(i) if len(str(su))>1: su = checkBeautyFactor(str(su)) return su b,k = map(int,input().split()) if k == 9: if b==9: print(123456789) else: print(-1) else: n = "1" + "0"*(k-1) while True: if "0" in str(n): n = int(n) n+=1 continue n = str(n) if len(set(n)) != len(n): n = int(n) n+=1 continue beautyFactor = checkBeautyFactor(str(n)) if beautyFactor == b: print(n) break n = int(n) n+=1 if len(str(n))>k: print(-1) break ================================================ FILE: Achhe Din.py ================================================ testCase = int(input()) from collections import Counter for _ in range(testCase): n = int(input()) ide = Counter(list(map(int,input().split()))) print(list(ide.keys())[list(ide.values()).index(1)]) ================================================ FILE: Add Alternate Elements of 2-Dimensional Array.py ================================================ a,b,c,d,e,f,g,h,i = map(int,input().split()) print(a+c+e+g+i) print(b+d+f+h) ================================================ FILE: Alien language.py ================================================ n = int(input()) for i in range(n): first_word = input() second_word = input() for i in second_word: if i in first_word: print("YES") break else: print("NO") ================================================ FILE: All Vowels.py ================================================ n = int(input()) s = input() a = 0 e=0 i=0 o=0 u=0 for j in range(n): if s[j]=='a': a+=1 if s[j]=='e': e+=1 if s[j]=='i': i+=1 if s[j]=='o': o+=1 if s[j]=='u': u+=1 if a>0 and e>0 and i>0 and o>0 and u>0: print("YES") else: print("NO") ================================================ FILE: AltF4 and the beetles.py ================================================ for _ in range(int(input())): n,a,b,c = map(int,input().split()) #store the beetles with 0 or 1 #0 means start time # 1 means end time beetles = [] for i in range(n): x,y = map(int,input().split()) beetles.append((x,0)) beetles.append((y,1)) # sort the array with my giving start time pripority and taking the consideration that endtime is less than the #previous once beetles.sort(key = lambda b: b[0]*2+ b[1]) total = n*a # since we have added a in starting , now we are adding b and substracting a ba = b-a cb = c-b max_ = total #checking whether the its the start time or end time # If it is start time we are adding ba #if end time we are adding cb for b in beetles: if b[1]==0: total += ba else: total += cb max_ = max(max_,total) print(max_) ================================================ FILE: Anagrams.py ================================================ from collections import Counter # Write your code here def cc(a,b): p = Counter(a) q = Counter(b) print(sum((q-p).values())+sum((p-q).values())) n = int(input()) for _ in range(n): a = input() b = input() cc(a,b) ================================================ FILE: Anshul, Usama And Punishment - A.py ================================================ n = int(input()) l = [20,30,40,90,80] if n>len(l): for i in range(5,n+1): if i%2==0: l.append(2*l[i-2]) else: l.append(3*l[i-2]) y=0 for i in range(n): for j in range(i+1,n): ma = abs(l[i]+l[j])+abs(l[i]-l[j]) y = max(y,ma) print(y) ================================================ FILE: Anti-palindrome strings.py ================================================ # Simple Solution First sort the string if sorted string is palindrome then print (-1) else print the sorted string. Because if the sorted string is palindrome then # the string will be something like this aaaaaa,bbbbb,ccccc,etc # Write your code here t = int(input()) for _ in range(t): inp = input() s = ''.join(sorted(inp)) if s == s[::-1]: print(-1) else: print(s) ================================================ FILE: Arjit and Apex.py ================================================ t = int(input()) for _ in range(t): m,n = map(int,input().split()) d = dict() new = dict() for i in range(m): p,q = map(int,input().split()) d[i] = (p,q) for i in range(n): p,q = map(int,input().split()) new[i] = (p,q) g,h = map(int,input().split()) g_c = 0 h_c=0 i = 0 j = 0 while is: s = curr_sum + arr[i] curr_sum = curr_sum + arr[i] new_arr[i] = s return new_arr t = int(input()) for _ in range(t): n = int(input()) li = list(map(int,input().split())) pre_sum = kadaneSum(li,n) post_sum = kadaneSum(li[::-1],n) post_sum = post_sum[::-1] ans = 0 for i in range(n-1): ans = max(ans,pre_sum[i]+post_sum[i+1]) print(ans) ================================================ FILE: ArrayGame.py ================================================ n,m=map(int,input().split()) a=list(map(int,input().split())) b=list(map(int,input().split())) a.sort() b.sort(reverse=True) p=0 if n>m: for i in range(m): if a[i]2 for any other index.(TRY with example ). The above step is done from line 20 to 30. Step 2: Now we will start a loop from index 1 as we have already cover the case for index 0. 2.a : We will first substract the previous value as we have added that in the step 1 and we don't need that now(because we have increase the index). 2.b : Then we will check whether the index 0 has reach the end or not: if index 0 has reach the end then the next index won't have same k value. Therefore we will decrese the k and subtract those value from the initialSum. If the index 0 has not reach the end :: Only one element is added.(Try to analyze with example ) ''' # Write your code here n = int(input()) a = list(map(int,input().split())) start = 1 k = 2 while start<=n: start += k k+=1 start -= k-1 k-=2 initialSum = sum(a[:start]) ma = initialSum for i in range(1,n): initialSum -= a[i-1] if start ma: ma =initialSum print(ma) ================================================ FILE: Biased Chandan.py ================================================ n = int(input()) x=[] for i in range(n): inq = int(input()) if inq == 0: if len(x)>0: x.pop() else: x.append(inq) print(sum(x)) ================================================ FILE: Binary Queries.py ================================================ n,q = map(int,input().split()) l =list(map(int,input().split())) q2 = [[ None for _ in range(3)] for _ in range(q)] for i in range(q): q2[i] = list(map(int,input().split())) if q2[i][0] ==1: if l[q2[i][1]-1] ==0: l[q2[i][1]-1]=1 else: l[q2[i][1]-1]=0 else: if l[q2[i][2]-1]==0: print('EVEN') else: print('ODD') ================================================ FILE: Breakup App.py ================================================ n = int(input()) result = {} msg = [] for _ in range(n): msg.append(list(input().split())) for i in range(n): for j in range(len(msg[i])): if msg[i][j].isdigit(): if msg[i][j] not in result: result[msg[i][j]]=0 if msg[i][0] == 'G:': result[msg[i][j]]+=2 else: result[msg[i][j]]+=1 if len(result) == 0: print('No Date') else: Keymax = max(result, key= lambda x: result[x]) if Keymax == '19' or Keymax == '20': print('Date') else: print('No Date') ================================================ FILE: Bricks Game.py ================================================ n = int(input()) temp = n for i in range(temp): n = n - i if n<=0 : print("Patlu") break n = n - i*2 if n<=0: print("Motu") break ================================================ FILE: CAn you solve it?.py ================================================ testCase = int(input()) for _ in range(testCase): n = int(input()) l = list(map(int,input().split())) mi = [] ma = [] for i in range(n): ma.append(l[i]+i) mi.append(l[i]-i) print(max((max(mi)-min(mi)),(max(ma)-min(ma)))) ================================================ FILE: Chandu and Consecutive Letters.py ================================================ n = int(input()) for i in range(n): s = input() st = [] for i in range(len(s)): if len(st)==0: st.append(s[i]) elif st[-1]==s[i]: pass else: st.append(s[i]) print(''.join(st)) ================================================ FILE: Char Sum.py ================================================ d = {} n = input() s = 'abcdefghijklmnopqrstuvwxyz' for i,j in enumerate(s): d[j] = i+1 cost = 0 for i in n: cost += d[i] print(cost) ================================================ FILE: Charged Up Array.py ================================================ T = int(input()) m = pow(10,9)+7 for _ in range(T): N = int(input()) A = list(map(int, input().split())) s=0 ch= int(pow(2,N)//2) for i in range(N): if A[i]>=ch: s+=A[i] print(s%m) ================================================ FILE: Cost of balloons.py ================================================ test_cases = int(input()) for i in range(test_cases): p,g = map(int, input().split()) n = int(input()) cost = 0 lower = min(p,g) maximum = max(p,g) p1_total = 0 p2_total = 0 for i in range(n): p1,p2 = map(int, input().split()) p1_total += p1 p2_total += p2 if p1_total >= p2_total: cost += p1_total*lower cost += p2_total*maximum else: cost += p2_total*lower cost += p1_total*maximum print(cost) ================================================ FILE: Count Divisors.py ================================================ l,r,k = map(int,input().split()) c=0 for i in range(l,r+1): if i%k==0: #print(i) c+=1 print(c) ================================================ FILE: Counting Triangles.py ================================================ n = int(input()) l = [] for _ in range(n): a = tuple(map(int,input().split())) a = sorted(a) l.append(tuple(a)) from collections import Counter d = Counter(l) ans = 0 for k,v in d.items(): if v ==1 : ans+=1 print(ans) ================================================ FILE: Crazy Kangaroo.py ================================================ t = int(input()) for _ in range(t): a,b,m = map(int,input().split()) ans =0 for i in range(a,b+1): if i%m == 0 : ans +=1 print(ans) ================================================ FILE: Dedication Level = Infinity.py ================================================ n= int(input()) l = [[0 for i in range(2)]for i in range(n)] for i in range(n): l[i] = list(input().split()) for i in range(n): l[i][1] = int(l[i][1]) l.sort(key = lambda x:x[1],reverse = True) for i in range(3): print(l[i][0]) ================================================ FILE: Determining numbers.py ================================================ # Write your code here from collections import Counter n = int(input()) arr = list(map(int,input().split())) a = Counter(arr) ans = [] for k,v in a.items(): if v==1: ans.append(k) ans.sort() print(*ans) ================================================ FILE: Difficult Characters.py ================================================ n = int(input()) for _ in range(n): s = input() from collections import Counter s2 = 'abcdefghijklmnopqrstuwvxyz' d = Counter(s2) for i in range(len(s)): if s[i] in s2: if d[s[i]]: d[s[i]]+=1 else: d[s[i]]=1 #print(d[s[i]]) else: d[s[i]]=0 ans = sorted(d.items(), key = lambda x : (x[1],-ord(x[0]))) for i in range(len(ans)): print(ans[i][0],end = ' ') print('\n') ================================================ FILE: Digital Sequence.py ================================================ a=int(input()) b=list(input().split()) max=0 for i in range(10): i=str(i) count=0 for j in b: if(i in j): count=count+1 if(count>max): max=count print(max) ================================================ FILE: Distribute chocolates.py ================================================ #Logic : Since there are n students and each student will recienve atleast one chocalate and the next student will receive k+1(Assuming k is the no.of chocolate given to first student) # then next student will get k+2,k+3.... so on # Minimum no.of chocolate n student will get will be n*(n+1)//2 where n = no.of student #Step 2: Calculate the chocalte left that is : c - ( Minimum no.of chocolate) # Step3 : Ans = chocalte left % n #Let us understand via example : c = 20 n = 3 # Since there are 3 student they are bound to recieve 6 chocolate #chocolateLeft = 20 -6 = 14 # If we divide 14 //3 we get 4 # therefore we can give each student 4 more chocolate that 5,6,7 = 18 #Therefore ans = 20 - 18 #Below is a shortcut way to calculate the above t = int(input()) for _ in range(t): c,n = map(int,input().split()) minimumChocolates = (n*(n+1))//2 if minimumChocolates >= c: print(c) else: c = c - minimumChocolates ans = c%n print(ans) ================================================ FILE: Divisibility.py ================================================ #If the last element contions zero then only we can divide the number formed by taking the last number by 10 # Therefore we only need to check that whether the last element of the last number is zero or not. N = int(input()) arr = list(map(int,input().split())) ele = arr[N-1] if ele % 10 == 0: print("Yes") else: print("No") ================================================ FILE: Divisible.py ================================================ n = int(input()) s = list(input().split()) half = n/2 x = [] for i in range(n): if i < half: x.append(s[i][0]) else: x.append(s[i][-1:]) temp = ''.join(x) if (int(temp) % 11 == 0): print("OUI") else: print("NON") ================================================ FILE: Does it Divide?.py ================================================ def prime(n) : # Corner cases if (n <= 1) : return 0 if (n <= 3) : return 1 # This is checked so that we can skip # middle five numbers in below loop if (n % 2 == 0 or n % 3 == 0) : return 0 i = 5 while(i * i <= n) : if (n % i == 0 or n % (i + 2) == 0) : return 0 i = i + 6 return 1 t = int(input()) for _ in range(t): n = int(input()) x = prime(n) if n==2: print('NO') elif n==1: print('YES') elif x==1: print('YES') elif x==0: y = prime(n+1) if y==1: print('NO') else: print('YES') ================================================ FILE: Duration.py ================================================ t = int(input()) for _ in range(t): sh,sm,eh,em = list(map(int,input().split())) starting_min = sh*60 + sm ending_min = eh*60 + em temp = ending_min - starting_min if temp>=60: hr = int(temp/60) mi = temp - hr*60 else: hr = 0 mi = temp print(hr,mi) ================================================ FILE: EEDC Lab.py ================================================ n = int(input()) a = list(map(int,input().split())) tmp_sum = sum(a) lucky = [] if n == 1: lucky.append(1) else: for i in range(n): if i == 0: lucky.append(0) else: lucky.append(lucky[i-1]) #print(lucky) if (tmp_sum - a[i])%3 == 0: if i == 0: if a[n-1] == 0: lucky[i]+=1 elif i == n-1: if a[n-2] == 0: lucky[i]+=1 else: if (a[i-1]+a[n-1])%10 == 0: lucky[i]+=1 lucky.insert(0,0) q = int(input()) for _ in range(q): l,r = map(int,input().split()) print(lucky[r] - lucky[l-1]) ================================================ FILE: Easy Sum Set Problem.py ================================================ n = int(input()) a = list(map(int,input().split())) m = int(input()) c = list(map(int,input().split())) l=[] for i in range(n): for j in range(m): x = c[j]-a[i] l.append(x) from collections import Counter l = Counter(l) ans = [] for k,v in l.items(): if v==n: ans.append(k) ans =sorted(ans) print(' '.join(map(str, ans))) ================================================ FILE: Easy one.py ================================================ from collections import Counter,defaultdict t = int(input()) for _ in range(t): n,q = map(int,input().split()) arr = list(map(int,input().split())) d = defaultdict(int) d = Counter(arr) for _ in range(q): temp = int(input()) print(d[temp]) ================================================ FILE: Equal elements ================================================ ''' # Sample code to perform I/O: name = input() # Reading input from STDIN print('Hi, %s.' % name) # Writing output to STDOUT # Warning: Printing unwanted or ill-formatted data to output will cause the test cases to fail ''' # Write your code here t = int(input()) def find(arr,ele,n): ans = 0 for i in range(n): temp = abs(ele-arr[i]) if temp == 1 or temp%2 !=0: ans += 1 return ans for _ in range(t): n = int(input()) arr = list(map(int,input().split())) l = [] for i in range(n): if arr[i]%2 == 0: l.append(2) else: l.append(1) x = n + 1 for k in range(1,3): x2 = find(arr,k,n) x = min(x,x2) print(x) ================================================ FILE: Equalize strings.py ================================================ n = int(input()) x = input() y = input() c = 0 l = 0 for i in range(n): if x[i] != y[i]: c+=1 else: if c > 0: l += 1 c =0 if c >0: l+=1 print(l) ================================================ FILE: Erasing an array.py ================================================ #When we encounter 1,0 sequence we have inversion and we can't erase the solution #Therefore the simple solution here is to just count the number of inversion that is the sequence of 1 follwed by 0. t = int(input()) for _ in range(t): n = int(input()) arr = list(map(int,input().split())) c = 1 for i in range(n-1): if arr[i] == 1 and arr[i+1]==0: c+=1 print(c) ================================================ FILE: Exchanging money.py ================================================ import math n,q = map(int,input().split()) arr = list(map(int,input().split())) g= arr[0] for i in range(n): g = math.gcd(g,arr[i]) for i in range(q): q1 = int(input()) if q1%g==0: print("YES") else: print("NO") ================================================ FILE: Factorial!.py ================================================ import math n = int(input()) print(math.factorial(n)) ================================================ FILE: Find Product.py ================================================ x = pow(10,9)+7 n = int(input()) l = list(map(int,input().split())) ans =1 for i in range(n): ans = (ans*l[i])%x print(ans) ================================================ FILE: Find the String.py ================================================ t = int(input()) from collections import Counter for _ in range(t): n,m = map(int,input().split()) matrix = {} for i in range(n): a = list(input()) matrix[i] = Counter(a) s = input() l = len(s) c = 0 c = 0 for i in range(len(s)): #print("fdefd",s[i],i) if i0: matrix[i][s[i]] -=1 #print(matrix[i],s[i]) c+=1 else: j = i%n if s[i] in matrix[j] and matrix[j][s[i]]>0: matrix[j][s[i]] -=1 #print("Else",matrix[j]) c+=1 if c==l: print("Yes") else: print("No") ================================================ FILE: Finding the Subarrays.py ================================================ n = int(input()) l1 = list(map(int,input().split())) ma = pow(10,7) ind1= [0]*(ma) ind2 = [0]*(ma) temp=0 for i in range(n): r = sum(l1) l = 0 for j in range(i,n): l+= l1[j] r = r - l1[j] if(n== j-i+1 or l/(j-i+1)>r/(n-j+i-1)): #print(r) ind1[temp] = i+1 ind2[temp] = j+1 #print(ind1[temp],ind2[temp]) temp+=1 print(temp) for i,j in zip(ind1,ind2): if i>0 or j>0: print(i,j) ================================================ FILE: Finding vaccines.py ================================================ # Simple Brute force # Since the interaction are between "C" and "G" RNA we calculate the number of "C" and "G" molecule in virus and in vaccine and then calculate the score # Write your code here noOfVaccine = int(input()) lenOfVirus = int(input()) virus = input() v = [0]*2 for i in virus: if i == 'C': v[0] += 1 elif i == 'G': v[1] += 1 ans = 0 number = 0 for j in range(noOfVaccine): le = int(input()) vacc = input() temp = [0]*2 #A temp array used to keep the count of "C" and "G" molecule for i in vacc: if i == 'C': temp[0]+=1 elif i =='G': temp[1]+=1 score = v[0]*temp[1] + v[1]*temp[0] if score > ans: ans = score number = j+1 print(number) ================================================ FILE: Fredo and Large Numbers.py ================================================ import sys def index(t,f,ma,p,q,s,d): if f>ma: return 0 if t==0: for k,v in d.items(): if v >= f: return k else: if f not in s: return 0 else: return p[q.index(f)] from collections import OrderedDict n = sys.stdin.readline() array = list(map(int, sys.stdin.readline().split())) d = OrderedDict() for i in array: #print(i) if i in d: d[i] +=1 else: d[i] = 1 #print(d) ma= max(d.values()) k = list(d.keys()) v = list(d.values()) s = set(v) for i in range(int(sys.stdin.readline())): t,f = map(int, sys.stdin.readline().split()) print(index(t,f,ma,k,v,s,d)) ================================================ FILE: Frequency of Students.py ================================================ # Write your code here n = int(input()) d = {} for _ in range(n): x = input() if x in d: d[x]+=1 else: d[x]=1 for k,v in sorted(d.items()): print(k, v) ================================================ FILE: Going to office.py ================================================ ''' # Sample code to perform I/O: name = input() # Reading input from STDIN print('Hi, %s.' % name) # Writing output to STDOUT # Warning: Printing unwanted or ill-formatted data to output will cause the test cases to fail ''' # Write your code here d = int(input()) oc,of,od = map(int,input().split()) cs,cb,cm,cd = map(int,input().split()) temp = d- of if temp > 0: costOnline = oc+ temp*od else: costOnline = oc time = d//cs costOffline = cb + cm*time + cd*d if costOnline<=costOffline: print("Online Taxi") else: print("Classic Taxi") ================================================ FILE: Grid and phrase.py ================================================ n,m = map(int,input().split()) matrix = [] for i in range(n): a= list(input()) matrix.append(a) c=0 #horizontal for i in range(n): for j in range(m): if m-j>=4: #print(m,j,i) if matrix[i][j]=='s' and matrix[i][j+1]=='a' and matrix[i][j+2]=='b' and matrix[i][j+3]=='a': #print("yes") c+=1 #Vertical for j in range(m): #ro for i in range(n): if n-i>=4: if matrix[i][j]=='s' and matrix[i+1][j]=='a' and matrix[i+2][j]=='b' and matrix[i+3][j]=='a': c+=1 for i in range(n): for j in range(m): if n-i>=4 and m-j>=4: #print(i,j) #print(matrix[i][j], matrix[i+1][j+1], matrix[i+2][j+2], matrix[i+3][j+3]) if matrix[i][j]=='s' and matrix[i+1][j+1]=='a' and matrix[i+2][j+2]=='b' and matrix[i+3][j+3]=='a': #print("yes") c+=1 for i in range(n-1,0,-1): for j in range(m): if i+1 >= 4 and m-j>=4: if matrix[i][j]=='s' and matrix[i-1][j+1]=='a' and matrix[i-2][j+2]=='b' and matrix[i-3][j+3]=='a': #print("ds") c+=1 print(c) ================================================ FILE: Hackers with Bits.py ================================================ n = int(input()) l = input() s1 = l.count('1') s2 = l.count('0') s= l.split('0') m = 0 for i in range(len(s)-1): z=s[i].count("1")+s[i+1].count("1") if(z>m): m=z if(s2==0): print(s1) elif(s1==0): print(0) elif(s1>m): print(m+1) else: print(m) ================================================ FILE: Hamiltonian and Lagrangian.py ================================================ n = int(input()) l = list(map(int,input().split()))[::-1] pq = [] pq.append(l[0]) m = l[0] for i in range(1,n): if l[i]>=m: pq.append(l[i]) m=l[i] print(*pq[::-1]) ================================================ FILE: Hello.py ================================================ print('Hello Kirti') ================================================ FILE: Help Jarvis!.py ================================================ t = int(input()) for i in range(t): l = input() li = [] for i in l: li.append(int(i)) mi = min(li) m = max(li) c=0 arr = [] for i in range(mi,m+1): arr.append(i) if sorted(li)==sorted(arr): print('YES') else: print('NO') ================================================ FILE: Honey Bees.py ================================================ for _ in range(int(input())): n, m = map(int, input().split()) M = [list(map(int, input().split())) for __ in range(n)] for k in range(int(input())): t, x, y = map(int,input().split()) s = 0 if t == 1: if y % 2 == 0: if x-1 >= 0: s += M[x-1][y] if x+1 < n: s += M[x+1][y] if y-1 >= 0: s += M[x][y-1] if y+1 < m: s += M[x][y+1] if x-1 >= 0 and y-1 >= 0: s += M[x-1][y-1] if x-1 >= 0 and y+1 < m: s += M[x-1][y+1] print(s) else: if x-1 >= 0: s += M[x-1][y] if y-1 >= 0: s += M[x][y-1] if y+1 < m: s += M[x][y+1] if x+1 < n and y-1 >= 0: s += M[x+1][y-1] if x+1 < n: s += M[x+1][y] if x+1 < n and y+1 < m: s += M[x+1][y+1] print(s) else: if y % 2 == 0: if x-2 >= 0 and y-1 >= 0: s += M[x-2][y-1] if x-2 >= 0: s += M[x-2][y] if x-2 >= 0 and y+1 < m: s += M[x-2][y+1] if x-1 >= 0 and y-2 >= 0: s += M[x-1][y-2] if y-2 >= 0: s += M[x][y-2] if x+1 < n and y-2 >= 0: s += M[x+1][y-2] if x+1 < n and y-1 >= 0: s += M[x+1][y-1] if x+2 < n: s += M[x+2][y] if x+1 < n and y+1 < m: s += M[x+1][y+1] if y+2 < m: s += M[x][y+2] if x+1 < n and y+2 < m: s += M[x+1][y+2] if x-1 >= 0 and y+2 < m: s += M[x-1][y+2] print(s) else: if x-1 >= 0 and y-1 >= 0: s += M[x-1][y-1] if x-2 >= 0: s += M[x-2][y] if x-1 >= 0 and y+1 < m: s += M[x-1][y+1] if x-1 >= 0 and y-2 >= 0: s += M[x-1][y-2] if y-2 >= 0: s += M[x][y-2] if x+1 < n and y-2 >= 0: s += M[x+1][y-2] if x+2 < n and y-1 >= 0: s += M[x+2][y-1] if x+2 < n: s += M[x+2][y] if x+2 < n and y+1 < m: s += M[x+2][y+1] if x+1 < n and y+2 < m: s += M[x+1][y+2] if y+2 < m: s += M[x][y+2] if x-1 >= 0 and y+2 < m: s += M[x-1][y+2] print(s) ================================================ FILE: ICPC Team Management.py ================================================ t = int(input()) for _ in range(t): n,k = map(int,input().split()) from collections import Counter d = Counter() s = 0 for i in range(n): temp = input() d[len(temp)] +=1 c=0 for x,v in d.items(): #print(v) if v%k==0: c+=1 if c==len(d): print("Possible") else: print("Not possible") ================================================ FILE: James and the menus.py ================================================ ''' # Sample code to perform I/O: name = input() # Reading input from STDIN print('Hi, %s.' % name) # Writing output to STDOUT # Warning: Printing unwanted or ill-formatted data to output will cause the test cases to fail ''' # Write your code here n,m = map(int,input().split()) lis = [] for i in range(n): temp = list(map(int,input().split())) lis.append(temp) mx = [0]*m su = [0]*n for i in range(n): for j in range(m): mx[j] = max(mx[j],lis[i][j]) su[i] += lis[i][j] cn = [0]*n goodNumber = 0 for i in range(n): for j in range(m): if mx[j] == lis[i][j]: cn[i]+=1 goodNumber = max(goodNumber,cn[i]) index = [] for i in range(len(cn)): if goodNumber == cn[i]: index.append(i) ansSum = 0 ansIndex = 0 if len(index)>1: for i in range(len(index)): temp = su[index[i]]/4 if temp>ansSum: ansSum = temp ansIndex = index[i] print(ansIndex+1) else: print(index[0]+1) ================================================ FILE: LAL Evaluation.py ================================================ n,m = map(int,input().split()) arr = list(map(int,input().split())) d = dict() c= 0 for i in range(n): temp = m-arr[i] if d.get(temp): #print(arr[i],temp) c+= d[temp] if d.get(arr[i]): d[arr[i]] +=1 else: d[arr[i]] =1 print(c) ================================================ FILE: Length of Valley ================================================ ''' # Sample code to perform I/O: name = input() # Reading input from STDIN print('Hi, %s.' % name) # Writing output to STDOUT # Warning: Printing unwanted or ill-formatted data to output will cause the test cases to fail ''' # Write your code here def area(arr,n): stack = [] right_index = [0]*n index = 0 while index0: x = stack.pop() right_index[x]= index while len(stack)>0: x = stack.pop() right_index[x]= index return right_index def l_area(arr,n): stack = [] left_index = [0]*n index = 0 while index0: x = stack.pop() left_index[x]= index while len(stack)>0: x = stack.pop() left_index[x]= index return left_index t = int(input()) for _ in range(t): n = int(input()) arr = list(map(int,input().split())) left_index = l_area(arr[::-1],n) left_index = left_index[::-1] for i in range(n): left_index[i] = n - left_index[i] + 1 right_index = area(arr,n) ans = [0]*n for i in range(n): ans[i] = right_index[i] - left_index[i] + 1 print(*ans,sep =' ') ================================================ FILE: Life, the Universe, and Everything.py ================================================ while True: n = int(input()) if n == 42: break else: print(n) ================================================ FILE: Lift queries.py ================================================ test_case = int(input()) a = 0 b = 7 for i in range(test_case): f = int(input()) temp = abs(f-a) temp2 = abs(f-b) if temp <= temp2: a = f print('A') else: b = f print('B') ================================================ FILE: Little Monk and Balanced Parentheses.py ================================================ n = int(input()) a = list(map(int,input().split())) s = [] ans = 0 for i in range(0,n): if a[i] > 0: s.append(i) elif a[i] < 0: if s and a[i] == -a[s[-1]]: s.pop() if not s: temp = -1 else: temp = s[-1] ans = max(ans,i - temp) else: s.append(i) print(ans) ================================================ FILE: Little_Jhool_psychic_powers.py ================================================ #Basics of Implementation #hackerearth #Little Jhool Psychic Powers #Solution n = input() if '111111' in n: print("Sorry, sorry!") elif "000000" in n: print("Sorry, sorry!") else: print("Good luck!" ) ================================================ FILE: Long ATM Queue.py ================================================ # Write your code here n = int(input()) l = list(map(int,input().split())) c=1 for i in range(n-1): if l[i+1]ma: ma = l[i] c+=1 print(c*r) ================================================ FILE: Maximize the modulo function.py ================================================ ''' This code uses two formula to solve that is : if c = a + b then c mod(n) = [ a mod(n) + b mod(n)] mod(n) example: c = 15 a = 7 b = 8 n = 2 15mod(2) = 1 7mod(2) = 1 8mod(2) = 0 [1+0]mod(2) = 1 2nd formula: ab mod(n) = [(a mod(n))(b mod(n))]mod(n) ''' # Write your code here def solve(k, n,m): pre = [0] for c in n: pre += [(pre[-1] * 10 + int(c))%k] ans = pre[-1]%k suf = 0 cur_pow = 1 for i in range(m - 1, -1, -1): ans = max(ans, (pre[i] * cur_pow%k + suf) % k) suf += cur_pow * int(n[i]) suf = suf%k cur_pow = cur_pow * 10%k return ans if __name__ == "__main__": T = int(input().strip()) for problem in range(1, T + 1): n,k = map(int, input().split()) A = input() print(solve(k, A,n)) ================================================ FILE: Maximum Of K- size subarrays (Deque).py ================================================ n,m= map(int,input().split()) l = list(map(int,input().split())) x=[] for i in range(0,n): if len(l[i:m+i])==m: x.append(max(l[i:m+i])) print(*x) ================================================ FILE: Maximum Sum.py ================================================ n = int(input()) arr= list(map(int,input().split())) s = 0 c = 0 for i in range(len(arr)): if arr[i]>=0: s += arr[i] c +=1 if s==0 and c==0: s = max(arr) c = 1 print(s,c) ================================================ FILE: Maximum goodness.py ================================================ n = int(input()) l = list(map(int,input().split())) ans = [1] le = [1] #d = {} #d[1] =1 for i in range(1,n): if l[i]==1: if ans[i-1]+ 1 >=1: ans.append(ans[i-1]+1) #d[ans[i-1]+1] = i+1 le.append(le[-1]+1) #print(i+1,d[ans[i-1]+1]) else: #d[1] = 1 le.append(1) ans.append(1) else: ans.append(ans[i-1]-1) le.append(le[-1]+1) x= [] ma = max(ans) for i in range(len(ans)): if ans[i]==ma: x.append(i) final_ans= [] for i in range(len(x)): final_ans.append(le[x[i]]) print(max(final_ans)) ================================================ FILE: Maximum occurrence.py ================================================ l = list(input()) from collections import Counter d = Counter(l) temp = max(d.values()) li = [] for k,v in d.items(): if v==temp: li.append(k) li.sort(key= lambda x: ord(x)) print(li[0],d[li[0]]) ================================================ FILE: Maze Problem.py ================================================ def issafe(p,q,arr): #print(type(p),type(n)) if p=0 and q=0 and arr[p][q]==1: #print("Sdsdsd") return True return False def solve(arr,i,j,sol): #print(n) if i==n-1 and j==n-1: sol[i][j]=1 return True #print(i,j) if issafe(i,j,arr)==True: sol[i][j]=1 #print(sol) if solve(arr,i,j+1,sol)==True: return True if solve(arr,i+1,j,sol)==True: return True sol[i][j]=0 return False t = int(input()) for _ in range(t): n = int(input()) arr =[] sol = [[ 0 for i in range(n) ] for i in range(n)] for i in range(n): x = list(map(int,input().split())) arr.append(x) if solve(arr,0,0,sol) ==True: print("POSSIBLE") else: print("NOT POSSIBLE") ================================================ FILE: Memorise Me!.py ================================================ from collections import Counter n= int(input()) a = list(map(int,input().split())) m = int(input()) p = Counter(a) for i in range(m): x = int(input()) if p[x]: print(p[x]) else: print('NOT PRESENT') ================================================ FILE: Micro and Array Update.py ================================================ n= int(input()) for _ in range(n): le,k = map(int,input().split()) array = list(map(int,input().split())) x = k-min(array) if x>0: print(x) else: print(0) ================================================ FILE: Min-Max.py ================================================ n = int(input()) arr = list(map(int,input().split())) ma = max(arr) mi = min(arr) s = sum(arr) a1_max = s-mi a2_min = s-ma print(a2_min,a1_max) ================================================ FILE: Mind Palaces.py ================================================ def binar(arr,l,r,x): if r >=l: mid = int(l+ (r-l)/2) if arr[mid]==x: return mid elif arr[mid]>x: return binar(arr,l,mid-1,x) else: return binar(arr,mid+1,r,x) else: return -1 n,m = map(int,input().split()) array = [] for i in range(n): temp = list(map(int,input().split())) array.append(temp) q = int(input()) for _ in range(q): x = int(input()) f =0 for i in range(n): t = binar(array[i],0,len(array[i])-1,x) if t!= -1: f=1 break if f==1: print(i,t) else: print("-1 -1") ================================================ FILE: Minimum Add to Make Parentheses Valid.py ================================================ s = input() op = '(' cl = ')' st = [s[0]] for i in range(1,len(s)): #print(st) if st: temp =s[i] if temp == cl: if op == st[-1]: st.pop() else: st.append(s[i]) else: st.append(s[i]) else: st.append(s[i]) print(len(st)) ================================================ FILE: Minimum moves.py ================================================ n = int(input()) for _ in range(n): x,y = map(int,input().split()) if x<0 or y<0 or y>x: print(-1) else: print(max(x,y)) ================================================ FILE: Modify Sequence.py ================================================ n = int(input()) l = list(map(int,input().split())) for i in range(n-1): if l[i]<=l[i+1]: l[i+1] = l[i+1]-l[i] else: print('NO') break else: if l[n-1]==0: print('YES') else: print('NO') ================================================ FILE: Modulo Strength.py ================================================ #NOTE array b is used to store the count of number of student that have same strength # Write your code here n,k = map(int,input().split()) a = list(map(int,input().split())) b = [0]*k for i in range(n): b[a[i]%k] += 1 ans = 0 for i in range(k): ans += b[i]*(b[i]-1) print(ans) ================================================ FILE: Monk And the operations.py ================================================ def abs_sum(row): s =0 for i in range(len(row)): s += abs(row[i]) return s def abs_add(row,v1): l = len(row) for i in range(l): row[i] += v1 x = abs_sum(row) return x n,m= map(int,input().split()) #input Matrix matrix = [] for i in range(n): a = list(map(int,input().split())) matrix.append(a) v1,v2,v3,v4 = map(int,input().split()) #convert n rows into n column col =[] for i in range(m): a = [] for j in range(n): a.append(matrix[j][i]) col.append(a) #row sum and update check f_sum1=0 for i in range(n): sum1=abs_sum(matrix[i]) x=0 temp = abs_add(matrix[i],v1) temp_2 = abs(len(matrix[i])*v2) if temp< temp_2: x = temp_2 else: x = temp if x>sum1: sum1 = x #print(sum1,i) f_sum1 += sum1 c_sum1=0 for i in range(m): sum1=abs_sum(col[i]) x12 = 0 temp = abs_add(col[i],v3) temp_1 = abs(len(col[i])*v4) if temp< temp_1: x12= temp_1 else: x12 = temp if x12>sum1: sum1 =x12 #print(sum1,i) c_sum1 += sum1 if c_sum1>f_sum1: print(c_sum1) else: print(f_sum1) ================================================ FILE: Monk Takes a Walk.py ================================================ t = int(input()) for _ in range(t): s = input() s = s.lower() c=0 for i in range(len(s)): if s[i] =='a' or s[i] =='e' or s[i] =='i' or s[i] =='o' or s[i] =='u': c+=1 print(c) ================================================ FILE: Monk Teaches Palindrome.py ================================================ t = int(input()) for _ in range(t): s = input() n = len(s) if s == s[::-1]: if n%2==0: print("YES EVEN") else: print("YES ODD") else: print("NO") ================================================ FILE: Monk and Inversions.py ================================================ t = int(input()) for _ in range(t): n= int(input()) matrix = [] for i in range(n): a = list(map(int,input().split())) matrix.append(a) #print(matrix) c=0 for i in range(n): for j in range(n): for p in range(n): for q in range(n): if i<=p and j<=q: if matrix[i][j]>matrix[p][q]: c+=1 print(c) ================================================ FILE: Monk and Lucky Minimum.py ================================================ testCase = int(input()) from collections import Counter for _ in range(testCase): n = int(input()) l = list(map(int,input().split())) p = Counter(l) x = min(l) if p[x]%2==0: print('Unlucky') else: print("Lucky") ================================================ FILE: Monk and Power of Time.py ================================================ n = int(input()) call = list(map(int,input().split())) ideal = list(map(int,input().split())) def check(a,b): if a[0] == b[0]: return True return False c=0 while len(call)>0: if check(call,ideal)==True: call = call[1:] ideal = ideal[1:] c+=1 else: call = call[1:n]+call[:1] #print(call) c+=1 print(c) ================================================ FILE: Monk and Rotation.py ================================================ testCase = int(input()) for _ in range(testCase): n,k = map(int,input().split()) l = list(map(int,input().split())) x = k%n print(*(l[n-x:]+l[:n-x])) ================================================ FILE: Monk and Welcome Problem.py ================================================ n= int(input()) a = list(map(int,input().split())) b = list(map(int,input().split())) c = [0]*n for i in range(n): c[i] = a[i]+b[i] print(*c) ================================================ FILE: Most Frequent.py ================================================ from collections import Counter n =int(input()) l = Counter(list(map(int,input().split()))) x = 100000 for k,v in l.items(): if v==max(l.values()): x = min(x,k) print(x) ================================================ FILE: Multiple occurrences.py ================================================ # Simple Brute force implementation # Only thing to note here is that in the dictonary/HaspMap we are storing key and list of values t = int(input()) for _ in range(t): n = int(input()) a = list(map(int,input().split())) d= {} for i in range(len(a)): try: if d[a[i]]: d[a[i]].append(i+1) except: d[a[i]] = [i+1] ans = 0 for k,v in d.items(): if len(v)>=2: ans += v[-1] - v[0] print(ans) ================================================ FILE: N - Co Ordinates (Pair Practice).py ================================================ from collections import Counter n = int(input()) d = Counter() for i in range(n): x = tuple(map(int,input().split())) d[x]+=1 for k,v in sorted(d.items()): print(*k,v) ================================================ FILE: Neutralisation of charges.py ================================================ n = int(input()) s =input() l = list(s) stack = [] stack.append(l[0]) for i in range(1,len(l)): stack.append(l[i]) try: if stack[-1] and stack[-2]: if stack[-1]==stack[-2]: stack.pop() stack.pop() except: pass print(len(stack)) print(''.join(stack)) ================================================ FILE: Not in Range.py ================================================ k = sum([i for i in range(1,1000001)]) t = int(input()) l = [] for _ in range(t): x,y = map(int,input().split()) l.append([x,y]) l = sorted(l) for i in range(len(l)-1): #print(i) if(l[i+1][0]<=l[i][1]): l[i+1][0] = l[i][1]+1 for i in range(len(l)): if(l[i][0]>l[i][1]): l[i] = 0 for i in range(len(l)): if(l[i]!=0): e = (l[i][1]*(l[i][1]+1))//2 o = (l[i][0]*(l[i][0]+1))//2 p = e-o+l[i][0] k-=p print(k) ================================================ FILE: Number of cycles.py ================================================ #Simple Cycles - No.of closed figure . #Hint - See Pattern # Write your code here t = int(input()) for _ in range(t): n = int(input()) ans = 1 ans += (n-1)*n print(ans) ================================================ FILE: Number of steps.py ================================================ ''' # Sample code to perform I/O: name = input() # Reading input from STDIN print('Hi, %s.' % name) # Writing output to STDOUT # Warning: Printing unwanted or ill-formatted data to output will cause the test cases to fail ''' # Write your code here n = int(input()) a = list(map(int,input().split())) b = list(map(int,input().split())) count = 0 mi = min(a) i = 0 while i =b[i]: while a[i]>mi: a[i] -= b[i] count+=1 if a[i] < mi: mi = a[i] i = 0 if a[i] != mi: count = -1 break i+=1 print(count) #A rare test case : # n=2 # a = [3,2] # b = [2,1] # then all a's element should be 1 ================================================ FILE: Pair Sum.py ================================================ n,m= map(int,input().split()) from collections import Counter arr = list(map(int,input().split())) d = Counter(arr) flag = 0 for i in range(n): temp = m - arr[i] if d[temp]: if arr[i]==temp and d[temp]==1: continue print("YES") flag = 1 break if flag==0: print("NO") ================================================ FILE: Pairs.py ================================================ def isPrime(n) : # Corner cases allprime = [True for i in range(ma+1)] p=2 x = [0]*(n+1) while (p*p<=n): if allprime[p]==True: for i in range(p*p,n+1,p): allprime[i] = False p+=1 for i in range(2,len(allprime)): if allprime[i]: x[i] = x[i-1]+1 else: x[i] = x[i-1] return x ma = 100005 x = isPrime(ma) t = int(input()) for _ in range(t): m,n = list(map(int,input().split())) if m==1: m =2 #print("ALl Prime lsit",allprime) no_of_prime = x[n]-x[m-1] print(no_of_prime*(n-m-no_of_prime+1)) ================================================ FILE: Palindromic String.py ================================================ n = input() if n== n[::-1]: print('YES') else: print('NO') ================================================ FILE: Passing the Parcel.py ================================================ ''' # Sample code to perform I/O: name = input() # Reading input from STDIN print('Hi, %s.' % name) # Writing output to STDOUT # Warning: Printing unwanted or ill-formatted data to output will cause the test cases to fail ''' # Write your code here n = int(input()) st = [True]*n song = input() ne = len(song) c = 0 j=0 i=0 while c=ne: i = i%ne if j>=n: j = j%n if song[i] =='a': if st[j] != False: i+=1 #st[j] == True j+=1 else: j+=1 elif song[i] == 'b': #print("sdsds") if st[j] != False: c+=1 i+=1 st[j] = False j+=1 else: j+=1 for i in range(len(st)): if st[i] == True: print(i+1) break ================================================ FILE: Pepper and Contiguous Even Subarray.py ================================================ t= int(input()) for _ in range(t): n = int(input()) l = list(map(int,input().split())) c=0 x=[-1] for i in range(n): if l[i]%2==0: c+=1 x.append(c) else: c=0 print(max(x)) ================================================ FILE: Perfect Subarray.py ================================================ n = int(input()) l = list(map(int,input().split())) import math def checkperfect(n): i = int(math.sqrt(n)) if n==i*i: return True else: return False c=0 for i in range(n): s= 0 for j in range(i,n): s+=l[j] if checkperfect(s): c+=1 print(c) ================================================ FILE: Permute the Array.py ================================================ t = int(input()) from collections import Counter for _ in range(t): n,k = map(int,input().split()) arr = list(map(int,input().split())) count = int(n/k) a= Counter(arr) l2 = 0 for p12,v in a.items(): if v==count: l2+=1 #print(l1) elif v>count: temp = int((v/count)) l2+=temp #print(temp) if l2 ==k: print("YES") else: print("NO") ================================================ FILE: Polygon Possibility ================================================ t= int(input()) for _ in range(t): n = int(input()) l = list(map(int,input().split())) s = sum(l) for i in range(len(l)): if l[i]>=s-l[i]: print('No') break else: print('Yes') ================================================ FILE: Prasun the detective.py ================================================ p = input().lower() q = input().lower() p =p.replace(" ","") q = q.replace(" ","") import re p = re.sub('[^a-z0-9]','',p) q= re.sub('[^a-z0-9]','',q) if sorted(p)==sorted(q): print('YES') else: print('NO') ================================================ FILE: Prime Number.py ================================================ # Write your code here def prime(x): l = [True for i in range(x+1)] p=2 while (p*p <= x): if l[p]==True: for i in range(2*p,x+1,p): l[i] = False p+=1 for i in range(2,x+1): if l[i]: print(i,end=' ') n = int(input()) prime(n) ================================================ FILE: Priority Interview.py ================================================ n = int(input()) x_y = [] for i in range(n): a,b = map(int,input().split()) x_y.append([a,b]) x_y.sort(key = lambda x : x[1],reverse = True) x_y.sort(key = lambda x : x[0]) for i in range(len(x_y)): print(x_y[i][1],end = ' ') ================================================ FILE: README.md ================================================ # HackerEarth Solutions Contains hackerearth solutions in python3 ================================================ FILE: Rain Sound.py ================================================ t = int(input()) for _ in range(t): l,r,s = map(int,input().split()) if s > r: print(-1,-1) else: mi = int(((l-1)/s) + 1) ma = r//s if mi>ma: print(-1,-1) else: print(mi,ma) ================================================ FILE: Rectangles to squares.py ================================================ # Write your code here import math m,n = map(int,input().split()) x = math.gcd(m,n) if x>1: print("Yes") else: print("No") ================================================ FILE: Reversed Linked List.py ================================================ class node: def __init__(self,data): self.data = data self.next = None class linkedList: def __init__(self): self.head = None def printl(self): t =self.head while t: print(t.data,end = ' ') t = t.next def atLast(self,new_data): new_node = node(new_data) if self.head is None: self.head = new_node return temp = self.head while temp.next: temp = temp.next temp.next = new_node new_node.next = None def addElement(self,lis): lis = lis[::-1] for i in range(len(lis)): nl.atLast(lis[i]) def reverse(self): temp = self.head lis = [] while temp: #print(temp.data) if int(temp.data)%2==0: lis.append(temp.data) temp = temp.next else: if len(lis)>0: nl.addElement(lis) lis = [] nl.atLast(temp.data) temp = temp.next if len(lis)>0: nl.addElement(lis) l = linkedList() nl = linkedList() t = int(input()) arr = list(map(int,input().split())) for i in range(len(arr)): l.atLast(arr[i]) l.reverse() nl.printl() ================================================ FILE: Robotic moves.py ================================================ # We only need to calculate for x axis since y axis part will be zero. #For any more query feel free to mail me. t = int(input()) for i in range(t): n = int(input()) print(n*(n+1)) ================================================ FILE: Roy and Profile Picture.py ================================================ l = int(input()) t = int(input()) for _ in range(t): m,n = map(int,input().split()) if m < l or n=2: p= temp.pop(0) q = temp.pop(0) new_array = l[p-1:q] print(len(set(new_array))) i+=1 else: pass ================================================ FILE: Seating Arrangement.py ================================================ def find(a): ch = a%12 if ch==1: print(a+11,'WS') elif ch==0: print(a-11,'WS') elif ch==6: print(a+1,'WS') elif ch==7: print(a-1,'WS') elif ch==2: print(a+9,'MS') elif ch==3: print(a+7,'AS') elif ch==4: print(a+5,'AS') elif ch==5: print(a+3,'MS') elif ch==8: print(a-3,'MS') elif ch==9: print(a-5,'AS') elif ch==10: print(a-7,'AS') else: print(a-9,'MS') n = int(input()) for i in range(n): a = int(input()) find(a) ================================================ FILE: Seven-Segment Display.py ================================================ # Write your code here d = {'0': 6, '1': 2, '2': 5, '3': 5, '4':4, '5':5, '6':6, '7':3, '8':7, '9':6} t = int(input()) for _ in range(t): n = input() no_of_matchstick = 0 for i in n: no_of_matchstick += d[i] #Case 1 Max number when the no_of Matchstick is even if no_of_matchstick%2 == 0: x = int(no_of_matchstick/2) number = '' for i in range(x): number = number + '1' else: x = int((no_of_matchstick-3)/2) number = '7' for i in range(x): number = number + '1' print(number) ================================================ FILE: Simon cannot sleep.py ================================================ import math h,m = map(int,input().split(":")) temp = h*60*60 + m*60 a = 0 d = 3927.272727272727 ans = temp/d + 1 print(math.floor(ans)) ================================================ FILE: Simple Search.py ================================================ n= int(input()) l = list(map(int,input().split())) k = int(input()) for i in range(n): if l[i]==k: print(i) ================================================ FILE: SnackDown Contest.py ================================================ t = int(input()) for _ in range(t): n = int(input()) p = list(map(int,input().split())) q = list(map(int,input().split())) p= p[1:] q = q[1:] z = p+q z1 = list(set(z)) if len(z1)==n: print('YES') else: print('NO') ================================================ FILE: Speed.py ================================================ testCase = int(input()) for _ in range(testCase): n = int(input()) l = list(map(int,input().split())) c=1 ma = l[0] for i in range(1,n): if l[i]<=ma: #print(l[i]) c+=1 ma = l[i] print(c) ================================================ FILE: Split Houses.py ================================================ n = int(input()) s = input() consecutive_house = [] temp = 0 arr = ['']*n for i,j in enumerate(s): if j == 'H': arr[i] = 'H' temp += 1 if i == n-1: consecutive_house.append(temp) else: arr[i] = 'B' if temp > 0: consecutive_house.append(temp) temp = 0 if len(consecutive_house) == 0 or max(consecutive_house) == 1: print('YES') print(''.join(arr)) else: print('NO') ================================================ FILE: Stack and Queue .py ================================================ n , k = map(int,input().split()) l = list(map(int,input().split())) s = 0 for i in range(k): s += l[i] m = s #print(m) for i in range(k-1): #print("sds",i) s = s - l[k-i-1] + l[n-1-i] if s>=m: m=s print(m) ================================================ FILE: Statistics.py ================================================ # Write your code here n = int(input()) arr = [] for i in range(n): l = input().split() arr.append(l) from collections import Counter,OrderedDict d = Counter(OrderedDict()) for i in range(len(arr)): x = str(arr[i][1]) #print(x,type(x)) if d[x]: d[x]+=1 else: d[x]=1 temp = max(d.values()) for k,v in d.items(): if v==temp: print(k) break print(d['football']) ================================================ FILE: Strange Game.py ================================================ testcase = int(input()) for _ in range(testcase): le,k = map(int,input().split()) alice = list(map(int,input().split())) bob=list(map(int,input().split())) x = max(bob)+1 t=0 for i in range(le): if x-alice[i]>0: t += (x-alice[i])*k print(t) ================================================ FILE: String.py ================================================ from collections import Counter n = int(input()) st = input() p = Counter(st) print(n - max(p.values())) ================================================ FILE: Sumita and equal array.py ================================================ t = int(input()) for _ in range(t): n,x,y,z = map(int,input().split()) l = list(map(int,input().split())) for i in range(n): while l[i]%x==0: l[i] = l[i]/x while l[i]%y==0: l[i] = l[i]/y while l[i]%z==0: l[i] = l[i]/z for i in range(1,n): if l[i]!=l[i-1]: print("She can't") break else: print('She can') ================================================ FILE: Takeoff.py ================================================ t = int(input()) for i in range(t): n,p,q,r = map(int,input().split()) c=0 for i in range(1,n+1): if (i%p==0)and (i%q!=0)and (i%r!=0): c+=1 if (i%p!=0)and (i%q==0)and (i%r!=0): c+=1 if (i%p!=0)and (i%q!=0)and (i%r==0): c+=1 print(c) ================================================ FILE: Terrible Chandu.py ================================================ n = int(input()) for i in range(n): a = input() print(a[::-1]) ================================================ FILE: The Amazing Race.py ================================================ # Write your code here # To understand the question go to link : https://www.geeksforgeeks.org/largest-rectangle-under-histogram/ t = int(input()) for _ in range(t): n= int(input()) ar = list(map(int,input().split())) flist = [0]*n stack = list() i=0 while i=ar[i]: stack.append(i) #print(stack) i+=1 else: top = stack.pop() if not stack: flist[top] = top #print("flidggfd",flist[top]) else: #print("top",top) flist[top] = top-stack[-1] #print("flist",flist[i]) while stack: top = stack.pop() if not stack: flist[top] = top #print("sdfdf",flist[top]) else: #print("top",top) flist[top] = top-stack[-1] #print("flist",flist[i]) blist = [0]*n br = ar[::-1] #print(br) stack1 = list() i=0 while i=br[i]: stack1.append(i) #print(stack1) i+=1 else: top = stack1.pop() if not stack1: blist[top] = top #print("flidggfd",blist[top]) else: #print("top",top) blist[top] = top-stack1[-1] #print("flist",flist[i]) while stack1: top = stack1.pop() if not stack1: blist[top] = top #print("sdfdf",blist[top]) else: #print("top",top) blist[top] = top-stack1[-1] #print("flist",flist[i]) backlist = blist[::-1] res_list = [] for i in range(0, len(backlist)): res_list.append(backlist[i] + flist[i]) sight = [] for i in range(1,len(res_list)+1): sight.append((i*res_list[i-1])%1000000007) temp = sight.index(max(sight))+1 print(temp) ================================================ FILE: The Monk and Kundan.py ================================================ def index(ref,x): for i in range(len(ref)): if ref[i]==x: return i ref = 'abcdefghijklmnopqrstuvwxyz1234567890ABCDEFGHIJKLMNOPQRSTUVWXYZ' ref = list(ref) n = int(input()) for i in range(n): l = list(input().split()) s=0 for i in range(len(l)): for j in range(len(l[i])): t= index(ref,l[i][j]) s+=j+ t print(s*len(l)) ================================================ FILE: The best Internet Browser.py ================================================ from fractions import Fraction t = int(input()) for _ in range(t): s = input() n = len(s) c=0 for j in range(4,n-4): #print(s[i]) if (s[j] != 'a') and (s[j] != 'e') and (s[j]!= 'i') and (s[j] != 'o') and (s[j] !='u') : c+=1 print(c+4,end ='') print('/',end = '') print(n) ================================================ FILE: Thief and Warehouses.py ================================================ def solve (array, n): stack = [] i=0 max_a=0 area = 0 while i=mr_v: if v>mr_v: mr_v = v mr_k = k else: if k>mr_k: mr_k = k mr_v = v mr = max(r.keys()) if r[mr]>=mr_v: mr_k = mr ma_k= 0 ma_v=0 for k,v in a.items(): #print(k,v) if v>=ma_v: if v>ma_v: ma_v = v ma_k = k elif v==ma_v: if k>ma_k: ma_k = k ma_v = v #print(ma_k) ma = max(a.keys()) if a[ma]>=ma_v: ma_k = ma if mr_k>ma_k: print('Rupam') elif mr_k0: print("NO") else: print("YES") ================================================ FILE: e-maze-in.py ================================================ n = input() x,y = 0,0 for i in n: if i == 'L': x -= 1 elif i == 'R': x += 1 elif i == 'U': y += 1 elif i == 'D': y -= 1 print(x,y) ================================================ FILE: square sub matrix.py ================================================ t = int(input()) for _ in range(t): n,m = map(int,input().split()) matrix = [] for i in range(n): x = list(map(int,input().split())) matrix.append(x) m2 = [[0 for _ in range(m)] for _ in range(n)] ma= 0 for i in range(n): for j in range(m): if i==0 or j==0: #print(i,j) m2[i][j] = matrix[i][j] if mama: ma = m2[i][j] print(ma*ma) ================================================ FILE: super reduced strings.py ================================================ s = input() stack = [s[0]] for i in range(1,len(s)): if stack: if stack[-1]== s[i]: #print("ssds",stack) stack.pop() else: stack.append(s[i]) else: stack.append(s[i]) #stack.append(s[i]) if len(stack)>0: print(''.join(stack)) else: print('Empty String') ================================================ FILE: zoos.py ================================================ # Write your code here s = input() from collections import Counter d = Counter(s) if d['z']*2 == d['o']: print("Yes") else: print("No")