├── Viral_Advertising.py ├── Extra_Long_Factorials.py ├── Strange_Counter.py ├── grading_students.py ├── Designer_PDF_Viewer.py ├── Equalize_the_Array.py ├── Sock_Merchant.py ├── Beautiful_Days_at_the_Movies.py ├── Save_the_Prisoner.py ├── Counting_Valleys.py ├── Sequence_Equation.py ├── Repeated_String.py ├── Sherlock_and_Squares.c ├── Drawing_Book.py ├── Encryption.py ├── The_Hurdle_Race.py ├── Migratory_Birds.py ├── Circular_Array_Rotation.py ├── Bon_Appetit.py ├── Flatland_Space_Stations.py ├── Cut_the_sticks.py ├── Divisible_Sum_Pairs.py ├── Picking_Numbers.py ├── Find_Digits.py ├── Utopian_Tree.py ├── Beautiful_Triplets.py ├── Fair-Rations.py ├── Cats_and_a_Mouse.py ├── Minimum_Distances.py ├── Jumping_on_the_Clouds_Revisited.py ├── Manasa_and_Stones.py ├── Between_Two_Sets.py ├── Day_of_the_Programmer.py ├── Service_Lane.py ├── Angry_Professor.py ├── Bigger_is_Greater.cpp ├── Birthday_Chocolate.py ├── Non-Divisible_Subset.py ├── Chocolate_Feast.py ├── Modified_Kaprekar_Numbers.py ├── Library_Fine.py ├── Electronics_Shop.py ├── Absolute_Permutation.py ├── Grading_Students.c ├── Breaking_the_Records.py ├── Append_and_Delete.py ├── Forming_a_Magic_Square.py ├── Apple_and_Orange.py ├── Jumping_on_the_Clouds.py ├── ACM_ICPC_Team.py ├── Cavity_Map.py ├── Organizing_Containers_of_Balls.py ├── Climbing_the_Leaderboard.py ├── Kangaroo.py ├── Number_Line_Jumps.py ├── bigger_is_greater.py ├── Taum_and_Bday.java ├── The_Grid_Search.py ├── Queens_Attack_2.py ├── Almost_Sorted.py ├── Lisas_Workbook.py ├── README.md ├── Larrys_Array.java ├── Emas_Supercomputer.py ├── Happy_Ladybugs.py ├── The_Time_in_Words.py ├── Birthday_Cake_Candles.java ├── Matrix_Layer_Rotation.py └── The_Bomberman_Game.py /Viral_Advertising.py: -------------------------------------------------------------------------------- 1 | m = 2 2 | t = 2 3 | for i in range(int(input())-1): 4 | t = int(3*t/2) 5 | m += t 6 | print(m) 7 | -------------------------------------------------------------------------------- /Extra_Long_Factorials.py: -------------------------------------------------------------------------------- 1 | #!/bin/python 2 | 3 | import math 4 | 5 | 6 | n = int(raw_input().strip()) 7 | print math.factorial(n) 8 | -------------------------------------------------------------------------------- /Strange_Counter.py: -------------------------------------------------------------------------------- 1 | t = int(raw_input()) 2 | 3 | phase = 3 4 | while t > phase: 5 | t -= phase 6 | phase *= 2 7 | 8 | print phase - t + 1 9 | -------------------------------------------------------------------------------- /grading_students.py: -------------------------------------------------------------------------------- 1 | #!/bin/python3 2 | grades = [int(input()) for p in range(int(input()))] 3 | [print(g+5 - g%5 if g%5 > 2 and g>37 else g) for g in grades] 4 | 5 | 6 | -------------------------------------------------------------------------------- /Designer_PDF_Viewer.py: -------------------------------------------------------------------------------- 1 | h = [int(h_temp) for h_temp in input().strip().split(' ')] 2 | word = input().strip() 3 | l = max([h[ord(i)-97] for i in word]) 4 | print(l*len(word)) 5 | -------------------------------------------------------------------------------- /Equalize_the_Array.py: -------------------------------------------------------------------------------- 1 | from collections import Counter 2 | 3 | n, a = int(input()), list(map(int, input().split())) 4 | print(min(n - cnt for x, cnt in Counter(a).items())) 5 | -------------------------------------------------------------------------------- /Sock_Merchant.py: -------------------------------------------------------------------------------- 1 | n = int(input().strip()) 2 | c = [int(c_temp) for c_temp in input().strip().split(' ')] 3 | b = set(c) 4 | co = 0 5 | for i in b: 6 | co += c.count(i)//2 7 | print (co) 8 | -------------------------------------------------------------------------------- /Beautiful_Days_at_the_Movies.py: -------------------------------------------------------------------------------- 1 | i, j, k = [int(x) for x in input().split()] 2 | s = 0 3 | for day in range(i, j+1): 4 | if (day - int(str(day)[::-1])) % k == 0: 5 | s += 1 6 | print(s) 7 | -------------------------------------------------------------------------------- /Save_the_Prisoner.py: -------------------------------------------------------------------------------- 1 | for i in range(input()): 2 | a,b,c = map(int, raw_input().split()) 3 | d = (c + (b % a) - 1)%a 4 | if d > 0: 5 | print d 6 | else: 7 | print a 8 | -------------------------------------------------------------------------------- /Counting_Valleys.py: -------------------------------------------------------------------------------- 1 | n = int(input()) 2 | s = input() 3 | c = 0 4 | tc = 0 5 | for i in range(n): 6 | if(s[i]=="D"): c+=1 7 | else: c-= 1 8 | if(c==0 and s[i] == "U"): tc += 1 9 | print(tc) 10 | -------------------------------------------------------------------------------- /Sequence_Equation.py: -------------------------------------------------------------------------------- 1 | # Enter your code here. Read input from STDIN. Print output to STDOUT 2 | 3 | n = int(input()) 4 | l = list(map(int,input().split())) 5 | for i in range(n): 6 | print(l.index(l.index(i+1)+1)+1) 7 | -------------------------------------------------------------------------------- /Repeated_String.py: -------------------------------------------------------------------------------- 1 | #!/bin/python3 2 | 3 | import sys 4 | 5 | 6 | s = input().strip() 7 | n = int(input().strip()) 8 | c = s.count('a') 9 | c *= n//len(s) 10 | q = n%len(s) 11 | c += s[:q].count('a') 12 | print (c) 13 | -------------------------------------------------------------------------------- /Sherlock_and_Squares.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | #include 5 | 6 | main(a,b){scanf("%d");while(scanf("%d%d",&a,&b)>0)printf("%d\n",(int)(floor(sqrt(b))-ceil(sqrt(a))+1));} 7 | -------------------------------------------------------------------------------- /Drawing_Book.py: -------------------------------------------------------------------------------- 1 | #!/bin/python 2 | 3 | import sys 4 | 5 | 6 | n = int(raw_input().strip()) 7 | p = int(raw_input().strip()) 8 | p = p/2 if p%2==0 else (p-1)/2 9 | n = n/2 if n%2==0 else (n-1)/2 10 | print(n-p if n-p

k else 0) 11 | -------------------------------------------------------------------------------- /Migratory_Birds.py: -------------------------------------------------------------------------------- 1 | n = int(input().strip()) 2 | types = list(map(int, input().strip().split(' '))) 3 | m = 0 4 | v = 0 5 | for i in range(1,6): 6 | t = types.count(i) 7 | if t > m: 8 | v = i 9 | m = t 10 | print(v) 11 | -------------------------------------------------------------------------------- /Circular_Array_Rotation.py: -------------------------------------------------------------------------------- 1 | # Enter your code here. Read input from STDIN. Print output to STDOUT 2 | 3 | (N,K,Q) = tuple(map(int,raw_input().split(" "))) 4 | a = map(int, raw_input().split(" ")) 5 | for q in xrange(Q): 6 | query = input() 7 | print a[(query - K) % N] 8 | -------------------------------------------------------------------------------- /Bon_Appetit.py: -------------------------------------------------------------------------------- 1 | # Enter your code here. Read input from STDIN. Print output to STDOUT 2 | 3 | n, k = map(int, raw_input().split()) 4 | A = map(int, raw_input().split()) 5 | b = int(raw_input()) 6 | 7 | tot = sum(A) - A[k] 8 | if 2*b == tot: print "Bon Appetit" 9 | else: print b - tot / 2 10 | -------------------------------------------------------------------------------- /Flatland_Space_Stations.py: -------------------------------------------------------------------------------- 1 | import sys 2 | 3 | n,m = raw_input().strip().split(' ') 4 | n,m = [int(n),int(m)] 5 | c = sorted(map(int,raw_input().strip().split(' '))) 6 | 7 | last = 0 8 | res = c[0] 9 | for x in c: 10 | res = max(res, (x-last)/2) 11 | last = x 12 | res = max(res, (n-1-last)) 13 | print res 14 | -------------------------------------------------------------------------------- /Cut_the_sticks.py: -------------------------------------------------------------------------------- 1 | #!/bin/python 2 | 3 | import sys 4 | 5 | 6 | n = int(input().strip()) 7 | a = map(int,input().strip().split(' ')) 8 | while len(a) > 0: 9 | print len(a) 10 | m = min(a) 11 | l = [] 12 | for i in a: 13 | j = i-m 14 | if j != 0: 15 | l += [j] 16 | a = l 17 | -------------------------------------------------------------------------------- /Divisible_Sum_Pairs.py: -------------------------------------------------------------------------------- 1 | #!/bin/python 2 | 3 | import sys 4 | 5 | 6 | n,k = raw_input().strip().split(' ') 7 | n,k = [int(n),int(k)] 8 | a = map(int,raw_input().strip().split(' ')) 9 | c = 0 10 | for i in range(n-1): 11 | for j in range(i+1,n): 12 | if (a[i]+a[j]) % k == 0: 13 | c += 1 14 | print c 15 | -------------------------------------------------------------------------------- /Picking_Numbers.py: -------------------------------------------------------------------------------- 1 | from collections import Counter 2 | 3 | n = int(input().strip()) 4 | a = [int(a_temp) for a_temp in input().strip().split(' ')] 5 | counter = Counter(a) 6 | keys = list(counter.keys()) 7 | print(max([counter[keys[i]] + counter[keys[i+1]] for i in range(len(keys)-1) if keys[i+1]-keys[i]==1] +list(counter.values()))) 8 | -------------------------------------------------------------------------------- /Find_Digits.py: -------------------------------------------------------------------------------- 1 | #!/bin/python 2 | 3 | import sys 4 | 5 | def check(a): 6 | s = 0 7 | for i in str(a): 8 | i = int(i) 9 | if i != 0 and a % i == 0: 10 | s = s + 1 11 | return s 12 | 13 | t = int(raw_input().strip()) 14 | for a0 in xrange(t): 15 | n = int(raw_input().strip()) 16 | print check(n) 17 | -------------------------------------------------------------------------------- /Utopian_Tree.py: -------------------------------------------------------------------------------- 1 | #!/bin/python 2 | 3 | import sys 4 | 5 | 6 | t = int(raw_input().strip()) 7 | for a0 in xrange(t): 8 | n = int(raw_input().strip()) 9 | h = 1 10 | c = 1 11 | for i in range(n): 12 | if c == 1: 13 | h = h * 2 14 | else: 15 | h = h + 1 16 | c *= -1 17 | print h 18 | -------------------------------------------------------------------------------- /Beautiful_Triplets.py: -------------------------------------------------------------------------------- 1 | # Enter your code here. Read input from STDIN. Print output to STDOUT 2 | 3 | 4 | if __name__ == "__main__": 5 | n,d = map(int,raw_input().split()) 6 | ar = map(int,raw_input().split()) 7 | output = 0 8 | for i in ar[0:-2]: 9 | if i+d in ar and i+2*d in ar: 10 | output += 1 11 | print output 12 | -------------------------------------------------------------------------------- /Fair-Rations.py: -------------------------------------------------------------------------------- 1 | import sys 2 | 3 | 4 | N = int(raw_input().strip()) 5 | B = map(int,raw_input().strip().split(' ')) 6 | 7 | ans = 0 8 | 9 | for i in xrange(N-1): 10 | if B[i]%2 == 1: 11 | B[i] += 1 12 | B[i+1] += 1 13 | ans += 2 14 | 15 | if B[N-1] % 2 == 1: 16 | print "NO" 17 | else: 18 | print ans 19 | 20 | -------------------------------------------------------------------------------- /Cats_and_a_Mouse.py: -------------------------------------------------------------------------------- 1 | #!/bin/python 2 | 3 | import sys 4 | 5 | 6 | q = int(raw_input().strip()) 7 | for a0 in xrange(q): 8 | x,y,z = raw_input().strip().split(' ') 9 | x,y,z = [int(x),int(y),int(z)] 10 | if abs(x-z) == abs(y-z): 11 | print 'Mouse C' 12 | elif abs(x-z) math.fabs(i-j): 12 | m = math.fabs(i-j) 13 | if m == n+1: print -1 14 | else: print int(m) 15 | 16 | -------------------------------------------------------------------------------- /Jumping_on_the_Clouds_Revisited.py: -------------------------------------------------------------------------------- 1 | #!/bin/python 2 | 3 | import sys 4 | 5 | 6 | n,k = raw_input().strip().split(' ') 7 | n,k = [int(n),int(k)] 8 | c = map(int,raw_input().strip().split(' ')) 9 | ans = 99 10 | if c[0] == 1: 11 | ans -= 2 12 | i = k%n 13 | while i != 0: 14 | ans -= 1 15 | if c[i] == 1: 16 | ans -= 2 17 | i = (i+k)%n 18 | print ans 19 | 20 | -------------------------------------------------------------------------------- /Manasa_and_Stones.py: -------------------------------------------------------------------------------- 1 | # Enter your code here. Read input from STDIN. Print output to STDOUT 2 | 3 | T = int(raw_input()) 4 | for _ in range(T): 5 | N = input() 6 | a = input() 7 | b = input() 8 | 9 | val = set([0]) 10 | for i in range(2,N+1): 11 | val = set([x + a for x in val]) | set([x + b for x in val]) 12 | print ' '.join(map(str, sorted(val))) 13 | -------------------------------------------------------------------------------- /Between_Two_Sets.py: -------------------------------------------------------------------------------- 1 | 2 | n,m = input().strip().split(' ') 3 | a = [int(a_temp) for a_temp in input().strip().split(' ')] 4 | b = [int(b_temp) for b_temp in input().strip().split(' ')] 5 | c = 0 6 | for i in range(max(a),min(b)+1): 7 | for j in a: 8 | if i%j!=0: break 9 | else: 10 | for k in b: 11 | if k % i != 0: break 12 | else: c+=1 13 | print(c) 14 | -------------------------------------------------------------------------------- /Day_of_the_Programmer.py: -------------------------------------------------------------------------------- 1 | #!/bin/python3 2 | 3 | import sys 4 | 5 | def solve(year): 6 | if(year == 1918): return "26.09.1918" 7 | if (((year%4 == 0 and year%100 != 0) or year%400==0) and year>=1917) or (year<=1917 and year%4 == 0): 8 | return "12.09.%d" %(year) 9 | return "13.09.%d" %(year) 10 | year = int(input().strip()) 11 | result = solve(year) 12 | print(result) 13 | -------------------------------------------------------------------------------- /Service_Lane.py: -------------------------------------------------------------------------------- 1 | #!/bin/python 2 | 3 | import sys 4 | 5 | 6 | n,t = raw_input().strip().split(' ') 7 | n,t = [int(n),int(t)] 8 | width = map(int,raw_input().strip().split(' ')) 9 | for a0 in xrange(t): 10 | i,j = raw_input().strip().split(' ') 11 | i,j = [int(i),int(j)] 12 | m = 3 13 | for k in range(i,j+1): 14 | if m > width[k]: 15 | m = width[k] 16 | print m 17 | -------------------------------------------------------------------------------- /Angry_Professor.py: -------------------------------------------------------------------------------- 1 | #!/bin/python 2 | 3 | import sys 4 | 5 | 6 | t = int(input().strip()) 7 | for a0 in xrange(t): 8 | n,k = input().strip().split(' ') 9 | n,k = [int(n),int(k)] 10 | a = map(int,input().strip().split(' ')) 11 | s = 0 12 | for i in a: 13 | if i <= 0: 14 | s += 1 15 | if s <= k: 16 | print("YES") 17 | else: 18 | print("NO") 19 | -------------------------------------------------------------------------------- /Bigger_is_Greater.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | int main() { 4 | int k; 5 | std::cin>>k; 6 | for(int i=0;i>input; 9 | if(std::next_permutation(input.begin(),input.end())) 10 | std::cout< 0: 14 | r += 1 15 | while i < j: 16 | r += max(cnt[i],cnt[j]) 17 | i, j = i+1, j-1 18 | if i == j and cnt[i] > 0: 19 | r += 1 20 | print r 21 | -------------------------------------------------------------------------------- /Chocolate_Feast.py: -------------------------------------------------------------------------------- 1 | #!/bin/python 2 | 3 | import sys 4 | 5 | t = int(raw_input().strip()) 6 | for a0 in xrange(t): 7 | n,c,m = raw_input().strip().split(' ') 8 | n,c,m = [int(n),int(c),int(m)] 9 | chocs= n/c 10 | wrapper = chocs 11 | ex_chocs = wrapper/m 12 | while wrapper >= m: 13 | ex_chocs = wrapper / m 14 | wrapper = ex_chocs + wrapper % m 15 | chocs += ex_chocs 16 | print chocs 17 | 18 | -------------------------------------------------------------------------------- /Modified_Kaprekar_Numbers.py: -------------------------------------------------------------------------------- 1 | # Enter your code here. Read input from STDIN. Print output to STDOUT 2 | 3 | p=int(input()) 4 | q=int(input()) 5 | flag = True 6 | for i in range(p,q+1): 7 | sq = i*i 8 | s=str(sq) 9 | if sq < 10: 10 | s = '0'+s 11 | l = s[0:int(len(s)/2)] 12 | r = s[int(len(s)/2):] 13 | if int(l)+int(r) == i: 14 | print (i , end = ' ') 15 | flag = False 16 | if flag: 17 | print ("INVALID RANGE") 18 | -------------------------------------------------------------------------------- /Library_Fine.py: -------------------------------------------------------------------------------- 1 | #!/bin/python 2 | 3 | def calcFine(Ddiff, Mdiff, Ydiff): 4 | fine = 0 5 | if Ydiff > 0: fine += 10000 6 | elif Ydiff < 0: return 0 7 | elif Mdiff > 0: fine += 500 * Mdiff 8 | elif Mdiff < 0: return 0 9 | elif Ddiff > 0: fine += 15 * Ddiff 10 | else: return 0 11 | return fine 12 | 13 | Da, Ma, Ya = map(int, raw_input().split()) 14 | De, Me, Ye = map(int, raw_input().split()) 15 | print calcFine(Da - De, Ma - Me, Ya - Ye) 16 | -------------------------------------------------------------------------------- /Electronics_Shop.py: -------------------------------------------------------------------------------- 1 | def getMoneySpent(ks, ds, s): 2 | su = 0 3 | for k in ks: 4 | for d in ds: 5 | if (su < k+d <=s): 6 | su = k + d 7 | if su: return su 8 | else: return "-1" 9 | 10 | s,n,m = input().strip().split(' ') 11 | s,n,m = [int(s),int(n),int(m)] 12 | ks = list(map(int, input().strip().split(' '))) 13 | ds = list(map(int, input().strip().split(' '))) 14 | moneySpent = getMoneySpent(ks, ds, s) 15 | print (moneySpent) 16 | -------------------------------------------------------------------------------- /Absolute_Permutation.py: -------------------------------------------------------------------------------- 1 | t = int(raw_input()) 2 | ans = [] 3 | 4 | for _ in xrange(t): 5 | n,k = map(int,raw_input().split()) 6 | ans = [0 for _ in xrange(n)] 7 | for i in xrange(1,n+1): 8 | if i-k >0 and ans[i-k-1]==0: 9 | ans[i-k-1] = i 10 | elif i+k <= n and ans[i+k-1] == 0: 11 | ans[i+k-1] = i 12 | else: 13 | break 14 | if 0 in ans: 15 | print -1 16 | else: 17 | print " ".join(map(str,ans)) 18 | -------------------------------------------------------------------------------- /Grading_Students.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | #include 5 | #include 6 | #include 7 | #include 8 | 9 | int main(){ 10 | int n; 11 | scanf("%d",&n); 12 | for(int a0 = 0; a0 < n; a0++){ 13 | int grade; 14 | scanf("%d",&grade); 15 | if(grade>37 && (((grade/5)+1)*5)-grade<3) grade = ((grade/5)+1)*5; 16 | printf("%d\n",grade); 17 | } 18 | return 0; 19 | } 20 | -------------------------------------------------------------------------------- /Breaking_the_Records.py: -------------------------------------------------------------------------------- 1 | #!/bin/python3 2 | 3 | import sys 4 | 5 | def getRecord(s): 6 | mi = ma = s[0] 7 | mic = mac = 0 8 | for i in range(len(s)): 9 | if s[i] > ma: 10 | ma = s[i] 11 | mac += 1 12 | if s[i] < mi: 13 | mi = s[i] 14 | mic += 1 15 | return [mac,mic] 16 | n = int(input().strip()) 17 | s = list(map(int, input().strip().split(' '))) 18 | result = getRecord(s) 19 | print (" ".join(map(str, result))) 20 | -------------------------------------------------------------------------------- /Append_and_Delete.py: -------------------------------------------------------------------------------- 1 | #!/bin/python3 2 | 3 | import sys 4 | 5 | 6 | s = input().strip() 7 | t = input().strip() 8 | k = int(input().strip()) 9 | 10 | l= 0 11 | for i in range(min(len(s),len(t))): 12 | if s[i] != t[i]: 13 | l = i 14 | break 15 | else: 16 | l = i + 1 17 | 18 | d = len(s) - l + len(t) - l 19 | 20 | if k >= len(s) + len(t): 21 | print("Yes") 22 | elif d <= k and (d % 2) == (k % 2): 23 | print("Yes") 24 | else: 25 | print("No") 26 | -------------------------------------------------------------------------------- /Forming_a_Magic_Square.py: -------------------------------------------------------------------------------- 1 | from itertools import * 2 | 3 | X = [] 4 | X.extend(list(map(int,input().split()))) 5 | X.extend(list(map(int,input().split()))) 6 | X.extend(list(map(int,input().split()))) 7 | 8 | Ans = 81 9 | for P in permutations(range(1,10)): 10 | if sum(P[0:3]) == 15 and sum(P[3:6]) == 15 and sum(P[0::3]) == 15 and sum(P[1::3]) == 15 and P[0] + P[4] + P[8] == 15 and (P[2] + P[4] + P[6] == 15): 11 | Ans = min(Ans, sum(abs(P[i] - X[i]) for i in range(0,9))) 12 | print(Ans) 13 | -------------------------------------------------------------------------------- /Apple_and_Orange.py: -------------------------------------------------------------------------------- 1 | #!/bin/python3 2 | 3 | import sys 4 | 5 | 6 | s,t = input().strip().split(' ') 7 | s,t = [int(s),int(t)] 8 | a,b = input().strip().split(' ') 9 | a,b = [int(a),int(b)] 10 | m,n = input().strip().split(' ') 11 | m,n = [int(m),int(n)] 12 | apple = len(list(filter(lambda x: int(x) + a <= t and int(x) + a >= s , input().strip().split(' ')))) 13 | orange = len(list(filter(lambda x: int(x) + b <= t and int(x) + b >= s , input().strip().split(' ')))) 14 | print(apple) 15 | print(orange) 16 | -------------------------------------------------------------------------------- /Jumping_on_the_Clouds.py: -------------------------------------------------------------------------------- 1 | #!/bin/python 2 | 3 | import sys 4 | 5 | 6 | N = int(raw_input().strip()) 7 | A = map(int,raw_input().strip().split(' ')) 8 | #1 if bad 9 | memo = {} 10 | def dp(x): 11 | #min from x 12 | if x == N-1: return 0 13 | if x >= N: return 10000000 14 | if x in memo: return memo[x] 15 | ans = 1000000 16 | if not A[x+1]: ans = 1+min(ans, dp(x+1)) 17 | if x+2 < N and not A[x+2]: ans = 1+min(ans,dp(x+2)) 18 | memo[x] = ans 19 | return ans 20 | print dp(0) 21 | -------------------------------------------------------------------------------- /ACM_ICPC_Team.py: -------------------------------------------------------------------------------- 1 | #!/bin/python3 2 | 3 | import itertools 4 | 5 | N,M = map(int,input().strip().split()) 6 | knowledge=[] 7 | for i in range(N): 8 | knowledge.append(int(input(),2)) 9 | mx = -float('inf') 10 | teams=0 11 | for p1,p2 in itertools.combinations(range(N),2): 12 | combined_topics = bin(knowledge[p1]|knowledge[p2]).count('1') 13 | if (combined_topics==mx): 14 | teams+=1 15 | elif (combined_topics>mx): 16 | mx = combined_topics 17 | teams=1 18 | 19 | print(mx,teams,sep='\n') 20 | -------------------------------------------------------------------------------- /Cavity_Map.py: -------------------------------------------------------------------------------- 1 | #!/bin/python 2 | 3 | import sys 4 | 5 | 6 | n = int(raw_input().strip()) 7 | grid = [] 8 | grid_i = 0 9 | for grid_i in xrange(n): 10 | grid_t = str(raw_input().strip()) 11 | grid.append(grid_t) 12 | for i in range(1,len(grid)-1): 13 | for j in range(1,len(grid[0])-1): 14 | if grid[i][j] > grid[i+1][j] and grid[i][j] > grid[i][j+1] and grid[i][j] > grid[i-1][j] and grid[i][j] > grid[i][j-1]: 15 | grid[i] = grid[i][:j] + "X" + grid[i][j+1:] 16 | 17 | for k in grid: 18 | print k 19 | -------------------------------------------------------------------------------- /Organizing_Containers_of_Balls.py: -------------------------------------------------------------------------------- 1 | q = int(input().strip()) 2 | 3 | for x in range(q): 4 | 5 | n = int(input().strip()) 6 | m = [] 7 | rowsum = [0]*n 8 | colsum = [0]*n 9 | 10 | for i in range(n): 11 | m.append(list(map(int,input().strip().split(' ')))) 12 | rowsum[i] = sum(m[-1]) 13 | colsum = list(map(lambda a, b: a+b, colsum, m[-1])) 14 | 15 | rowsum.sort() 16 | colsum.sort() 17 | 18 | if rowsum == colsum: 19 | print('Possible') 20 | else: 21 | print('Impossible') 22 | -------------------------------------------------------------------------------- /Climbing_the_Leaderboard.py: -------------------------------------------------------------------------------- 1 | n = int(input().strip()) 2 | scores = [int(scores_temp) for scores_temp in input().strip().split(' ')] 3 | m = int(input().strip()) 4 | alice = [int(alice_temp) for alice_temp in input().strip().split(' ')] 5 | 6 | ranking = [scores[0]] 7 | 8 | for itm in scores: 9 | if itm != ranking[-1]: 10 | ranking.append(itm) 11 | 12 | inx = len(ranking) - 1 13 | 14 | for itm in alice: 15 | 16 | while itm > ranking[inx] and inx > -1: 17 | inx -= 1 18 | 19 | if itm == ranking[inx]: 20 | print (inx + 1) 21 | else: 22 | print (inx + 2) 23 | -------------------------------------------------------------------------------- /Kangaroo.py: -------------------------------------------------------------------------------- 1 | #!/bin/python 2 | 3 | import sys 4 | 5 | 6 | x1,v1,x2,v2 = raw_input().strip().split(' ') 7 | x1,v1,x2,v2 = [int(x1),int(v1),int(x2),int(v2)] 8 | if x1 > x2: 9 | x2,x1 = x1,x2 10 | v2,v1 = v1,v2 11 | 12 | #if x1 == 43 and v1 == 2 and x2 == 70 and v2 == 2: 13 | #print "NO" 14 | 15 | elif v2 >= v1: 16 | print "NO" 17 | 18 | else: 19 | c = 0 20 | while x1 <= x2: 21 | if x1 == x2: 22 | c = 1 23 | break 24 | x1 += v1 25 | x2 += v2 26 | if c == 1: 27 | print "YES" 28 | else: 29 | print "NO" 30 | -------------------------------------------------------------------------------- /Number_Line_Jumps.py: -------------------------------------------------------------------------------- 1 | import math 2 | import os 3 | import random 4 | import re 5 | import sys 6 | 7 | # Complete the kangaroo function below. 8 | def kangaroo(x1, v1, x2, v2): 9 | if( (v2y? ((x-y>z)? y+z : x) : x; 18 | y = y>x? ((y-x>z)? x+z : y) : y; 19 | System.out.println(b*x+w*y); 20 | } 21 | } 22 | } 23 | -------------------------------------------------------------------------------- /The_Grid_Search.py: -------------------------------------------------------------------------------- 1 | # Enter your code here. Read input from STDIN. Print output to STDOUT 2 | def solve(R, C, G, r, c, P): 3 | for i in xrange(R-r+1): 4 | for j in xrange(C-c+1): 5 | found = True 6 | for k in xrange(r): 7 | if P[k] != G[i+k][j:j+c]: 8 | found = False 9 | break 10 | if found: 11 | return "YES" 12 | return "NO" 13 | 14 | T = int(raw_input()) 15 | for _ in xrange(T): 16 | R, C = (int(x) for x in raw_input().strip().split()) 17 | G = [raw_input().strip() for i in xrange(R)] 18 | 19 | r, c = (int(x) for x in raw_input().strip().split()) 20 | P = [raw_input().strip() for i in xrange(r)] 21 | 22 | print solve(R, C, G, r, c, P) 23 | -------------------------------------------------------------------------------- /Queens_Attack_2.py: -------------------------------------------------------------------------------- 1 | #!/bin/python3 2 | 3 | import sys 4 | 5 | 6 | n,k = input().strip().split(' ') 7 | n,k = [int(n),int(k)] 8 | rQueen,cQueen = input().strip().split(' ') 9 | rQueen,cQueen = [int(rQueen),int(cQueen)] 10 | ObList = [] 11 | for a0 in range(k): 12 | rObstacle,cObstacle = input().strip().split(' ') 13 | rObstacle,cObstacle = [int(rObstacle),int(cObstacle)] 14 | # your code goes here 15 | ObList.append((rObstacle,cObstacle)) 16 | ObSet = set(ObList) 17 | Delta = [(0,1),(1,1),(1,0),(0,-1),(-1,-1),(-1,0),(1,-1),(-1,1)] 18 | Count = 0 19 | for shift in Delta: 20 | Pos = (rQueen,cQueen) 21 | while Pos[0] + shift[0] >=1 and Pos[0] + shift[0] <= n and Pos[1] + shift[1] >=1 and Pos[1] + shift[1] <= n: 22 | Pos = (Pos[0]+shift[0],Pos[1]+shift[1]) 23 | if Pos in ObSet: 24 | break 25 | Count += 1 26 | print(Count) 27 | 28 | -------------------------------------------------------------------------------- /Almost_Sorted.py: -------------------------------------------------------------------------------- 1 | # Enter your code here. Read input from STDIN. Print output to STDOUT 2 | raw_input() 3 | l = map(int,raw_input().split()) 4 | sl = sorted(l) 5 | 6 | diffcount = 0 7 | diff1 = -1 8 | diff2 = -1 9 | 10 | for i in range(len(l)): 11 | if sl[i] != l[i]: 12 | diffcount += 1 13 | if diff1 == -1: 14 | diff1 = i 15 | elif diffcount > 1: 16 | diff2 = i 17 | lastdiff = i 18 | 19 | if diffcount == 2: 20 | l[diff1], l[diff2] = l[diff2], l[diff1] 21 | if l == sl: 22 | print "yes" 23 | print "swap {} {}".format(diff1+1, diff2+1) 24 | else: 25 | print "no" 26 | elif diffcount > 2: 27 | l = l[:diff1] + l[diff1:diff2+1][::-1] + l[diff2+1:] 28 | if l == sl: 29 | print "yes" 30 | print "reverse {} {}".format(diff1+1, diff2+1) 31 | else: 32 | print "no" 33 | elif l == sl: 34 | print "yes" 35 | -------------------------------------------------------------------------------- /Lisas_Workbook.py: -------------------------------------------------------------------------------- 1 | # Enter your code here. Read input from STDIN. Print output to STDOUT 2 | def lisas_workbook(n,k,a): 3 | 4 | num_special=0 5 | cur_page=1 6 | 7 | for i in range(len(a)): 8 | 9 | num_probs_in_chapter=a[i] 10 | num_full_pages, leftover_probs = divmod(num_probs_in_chapter, k) 11 | 12 | total_pages = num_full_pages + ( 1 if leftover_probs else 0 ) 13 | problems_in_chapter=iter(range(1, a[i]+1)) 14 | 15 | for _ in range(total_pages): 16 | probs_on_page = [next(problems_in_chapter, None) for _ in range(k)] 17 | if cur_page in probs_on_page: 18 | num_special+=1 19 | cur_page+=1 20 | return num_special 21 | 22 | def splitit(a): 23 | l = [] 24 | for i in a: 25 | l += [int(i)] 26 | return l 27 | n = splitit(raw_input().split()) 28 | l = splitit(raw_input().split()) 29 | 30 | print lisas_workbook(n[0],n[1],l) 31 | 32 | 33 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # hackerrank-algo-implementation 2 | 3 | This are the solutions to ALL "implementation" problems in the "algorithms" category at "https://www.hackerrank.com" 4 | Link: https://www.hackerrank.com/domains/algorithms/implementation 5 | 6 | ### How to Find your Program 7 | The list of solutions is named according to the question name. To find your solution search in alphabetical order. 8 | 9 | ##### *"Make sure you only check the solution once you have tried the problem yourself. BE FAITHFUL."* 10 | 11 | ### Why Python? 12 | Python is a very easy-to-read language, hence, the solution can be easily understood by anyone. 13 | 14 | ### Unsolved Questions 15 | *None* (All questions in the implementation category has been solved.) 16 | 17 | ### Author 18 | 1) Email: nishant.aklecha@gmail.com 19 | 2) LinkedIn: https://www.linkedin.com/in/naklecha/ 20 | 3) HackerRank: https://www.hackerrank.com/NAklecha 21 | 4) GitHub: https://github.com/Naklecha' 22 | 23 | ##### *"Any suggestions would be appreciated"* 24 | -------------------------------------------------------------------------------- /Larrys_Array.java: -------------------------------------------------------------------------------- 1 | import java.io.*; 2 | import java.util.*; 3 | 4 | public class Solution { 5 | 6 | public static void main(String[] args) { 7 | Scanner scanner = new Scanner(System.in); 8 | int t = scanner.nextInt(); 9 | for (int i=0; i=i; j--){ 23 | while (array[j]>array[j+1]||array[j]>array[j+2]) { 24 | rotate(array,j); 25 | } 26 | } 27 | } 28 | } 29 | 30 | public static void rotate(int[] arr, int a){ 31 | int temp = arr[a]; 32 | arr[a] = arr[a+1]; 33 | arr[a+1] = arr[a+2]; 34 | arr[a+2] = temp; 35 | } 36 | } 37 | -------------------------------------------------------------------------------- /Emas_Supercomputer.py: -------------------------------------------------------------------------------- 1 | #!/bin/python 2 | import sys 3 | from copy import deepcopy 4 | n,m = map(int,raw_input().strip().split()) 5 | a = [list(raw_input().strip()) for i in range(n)] 6 | ans=0 7 | for i in range(n): 8 | for j in range(m): 9 | if a[i][j]=='B': 10 | continue 11 | a1=deepcopy(a) 12 | ct=1 13 | a1[i][j]='B' 14 | while j+ct=0 and a1[i][j+ct]=='G' and a1[i][j-ct]=='G' and i+ct=0 and a1[i+ct][j]=='G' and a1[i-ct][j]=='G': 15 | a1[i][j+ct]='B' 16 | a1[i][j-ct]='B' 17 | a1[i+ct][j]='B' 18 | a1[i-ct][j]='B' 19 | ct+=1 20 | ct-=1 21 | ctt=-1 22 | for x in range(n): 23 | for y in range(m): 24 | if a1[x][y]=='B': 25 | continue 26 | pt=1 27 | while y+pt=0 and a1[x][y+pt]=='G' and a1[x][y-pt]=='G' and x+pt=0 and a1[x+pt][y]=='G' and a1[x-pt][y]=='G': 28 | pt+=1 29 | pt-=1 30 | ctt=max(ctt,pt) 31 | if ctt>=0: 32 | ans=max(ans,(4*ct+1)*(4*ctt+1)) 33 | print ans 34 | -------------------------------------------------------------------------------- /Happy_Ladybugs.py: -------------------------------------------------------------------------------- 1 | #!/bin/python3 2 | 3 | import sys 4 | from collections import Counter 5 | 6 | Q = int(input().strip()) 7 | for a0 in range(Q): 8 | n = int(input().strip()) 9 | b = input().strip() 10 | #print(b) 11 | counts = Counter(b) 12 | #print (counts 13 | flag = False 14 | f = False; 15 | cntS = 0 16 | for k,v in counts.items(): 17 | if(k != '_' and v == 1): 18 | print ("NO") 19 | flag = True 20 | break 21 | elif(k == '_'): 22 | cntS += 1 23 | 24 | if(cntS == n and flag == False): 25 | print("YES") 26 | flag = True 27 | elif(flag == False): 28 | if(cntS == 0): 29 | i = 0 30 | while(i candles) { 16 | 17 | int maxHeightCandle = Collections.max(candles); 18 | 19 | return Collections.frequency(candles, maxHeightCandle); 20 | 21 | } 22 | 23 | } 24 | 25 | public class Solution { 26 | public static void main(String[] args) throws IOException { 27 | BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(System.in)); 28 | BufferedWriter bufferedWriter = new BufferedWriter(new FileWriter(System.getenv("OUTPUT_PATH"))); 29 | 30 | int candlesCount = Integer.parseInt(bufferedReader.readLine().trim()); 31 | 32 | List candles = Stream.of(bufferedReader.readLine().replaceAll("\\s+$", "").split(" ")) 33 | .map(Integer::parseInt) 34 | .collect(toList()); 35 | 36 | int result = Result.birthdayCakeCandles(candles); 37 | 38 | bufferedWriter.write(String.valueOf(result)); 39 | bufferedWriter.newLine(); 40 | 41 | bufferedReader.close(); 42 | bufferedWriter.close(); 43 | } 44 | } 45 | -------------------------------------------------------------------------------- /Matrix_Layer_Rotation.py: -------------------------------------------------------------------------------- 1 | rows, cols, rotations = [int(x) for x in input().strip().split(" ")] 2 | num_layers = int(min(rows, cols)/2) 3 | layers = [[] for x in range(num_layers)] 4 | data = [] 5 | for row in range(rows): 6 | data.append([int(x) for x in input().strip().split(" ")]) 7 | 8 | rows -= 1 9 | cols -= 1 10 | 11 | for current_layer in range(num_layers): 12 | i = j = current_layer 13 | while i < rows-current_layer: 14 | layers[current_layer].append(data[i][j]) 15 | i += 1 16 | while j < cols-current_layer: 17 | layers[current_layer].append(data[i][j]) 18 | j += 1 19 | while i > current_layer: 20 | layers[current_layer].append(data[i][j]) 21 | i -= 1 22 | while j > current_layer: 23 | layers[current_layer].append(data[i][j]) 24 | j -= 1 25 | 26 | new_layers = [] 27 | for layer in layers: 28 | rots = rotations%len(layer) 29 | new_layers.append(list(layer[-rots:] + layer[:-rots])) 30 | 31 | for current_layer in range(num_layers): 32 | i = j = current_layer 33 | while i < rows-current_layer: 34 | data[i][j] = new_layers[current_layer].pop(0) 35 | i += 1 36 | while j < cols-current_layer: 37 | data[i][j] = new_layers[current_layer].pop(0) 38 | j += 1 39 | while i > current_layer: 40 | data[i][j] = new_layers[current_layer].pop(0) 41 | i -= 1 42 | while j > current_layer: 43 | data[i][j] = new_layers[current_layer].pop(0) 44 | j -= 1 45 | 46 | for row in range(rows+1): 47 | for col in range(cols+1): 48 | print(data[row][col], end=" ") 49 | print() 50 | -------------------------------------------------------------------------------- /The_Bomberman_Game.py: -------------------------------------------------------------------------------- 1 | # Enter your code here. Read input from STDIN. Print output to STDOUT 2 | def bomb(r, c, grid, t): 3 | complete_grid = [ ["O"]*c for i in range(r) ] 4 | stable_grid = [ ["O"]*c for i in range(r) ] 5 | other_grid = [ ["O"]*c for i in range(r) ] 6 | for i in range(r): 7 | for j in range(c): 8 | if grid[i][j] == "O": 9 | other_grid[i][j] = "." 10 | if i>0: 11 | other_grid[i-1][j] = "." 12 | if j>0: 13 | other_grid[i][j-1] = "." 14 | if i0: 23 | stable_grid[i-1][j] = "." 24 | if j>0: 25 | stable_grid[i][j-1] = "." 26 | if i