Saturday, September 1, 2018

Birthday Chocolate : Algorithms - Implementation

#The solution in Python3 is as follows:


def solve(n, a, d, m):
    count = 0
    for i in range(0,n):
          total = sum(a[i:m+i])
          if total == d:
                count+=1
         return count
n=int(input())
a=list(map(int,input().split()))
d,m=map(int,input().split())
print(solve(n,a,d,m))

#Keep visiting for more solutions of HackerRank problems in Python 3.Thank you!!!

Monday, August 13, 2018

Game of Coins : Basic Programming - Implementation - Basics of Implementation

#The solution in Python3 is as follows:


ALICE = 'Alice'
BOB = 'Bob'
# Write your code here
def lolo(N):
    return ALICE
   

def init():
    T = input()
    for num_testcases in range(int(T)):
           A = int(input())
           x = lolo(A)
           print(x)
init()

#Keep visiting for more solutions of HackerEarth problems in Python 3.Thank you!!!

Sock Merchant : Algorithms - Implementation

#The solution in Python3 is as follows:


n=int(input())
a=list(map(int,input().split()))
b=set(a)
c=0
for i in b:
    if a.count(i)//2>=1:
        c+=(a.count(i)//2)
print(c)

#Keep visiting for more solutions of HackerRank problems in Python 3.Thank you!!!

Monday, August 6, 2018

Bon Appétit : Algorithms - Implementation

#The solution in Python3 is as follows:


n,k=map(int,input().split())
a=list(map(int,input().split()))
b=int(input())
bac=(sum(a)-a[k])//2
if bac==b:
       print("Bon Appetit")
elif bac<b:
       print(b-bac)

#Keep visiting for more solutions of HackerRank problems in Python 3.Thank you!!!

Drawing Book : Algorithms - Implementation

#The solution in Python3 is as follows:


n=int(input())
p=int(input())
if (n%2!=0 and p%2!=0) or (n%2!=0 and p%2==0) or (n%2==0 and p%2==0):
        print(min(p//2,(n-p)//2))
elif n%2==0 and p%2!=0:
        print(min(p//2,((n-p)//2)+1))

#Keep visiting for more solutions of HackerRank problems in Python 3.Thank you!!!

Wednesday, August 1, 2018

Viral Advertising : Algorithms - Implementation

#The solution in Python3 is as follows:


m = [2]
for i in range(int(input())-1):
    m.append(int(3*m[i]/2))
print(sum(m))

#Keep visiting for more solutions of HackerRank problems in Python 3.Thank you!!!

Sunday, July 29, 2018

Last Occurrence : Algorithms - Searching - Linear Search

#The solution in Python3 is as follows:


n,m=map(int,input().split())
a=list(map(int,input().split()))
if m in a:
    b=len(a) - a[::-1].index(m) - 1
    print(b+1)
else:
    print(-1)

#Keep visiting for more solutions of HackerEarth problems in Python 3.Thank you!!!

Cats and a Mouse : Algorithms - Implementation

#The solution in Python3 is as follows:


for _ in range(int(input())):
    x,y,z=map(int,input().split())
    if x==y:
        print("Mouse C")
    elif x==z:print("Cat A")
    elif y==z:print("Cat B")
    elif (x>z and y<z) or (x<z and y>z):
        if abs(x-z)==abs(y-z):
            print("Mouse C")
        elif min(abs(x-z),abs(y-z))==abs(x-z):
            print("Cat A")
        else:
            print("Cat B")

    elif (x<z and y<z and x<y) or (x<z and y<z and x>y):
        a=max(x,y)
        if a==y:
            print("Cat B")
        else:
            print("Cat A")
    elif (x>z and y>z and y>x) or (x>z and y>z and y<x):
        b=min(x,y)
        if b==x:
            print("Cat A")
        else:
            print("Cat B")

#Keep visiting for more solutions of HackerRank problems in Python 3.Thank you!!!

Monday, July 23, 2018

Counting Valleys : Algorithms - Implementation

#The solution in Python3 is as follows:


def countingValleys(n, steps):
    seaLevel = valley = 0

    for step in steps:
        if step == 'U':
            seaLevel += 1
        else:
            seaLevel -= 1
       
        if step == 'U' and seaLevel == 0:
            valley += 1
   
    return valley
n=int(input())
s=input()
print(countingValleys(n,s))

#Keep visiting for more solutions of HackerRank problems in Python 3.Thank you!!!

Wednesday, July 18, 2018

Utopian Tree : Algorithms - Implementation

#The solution in Python3 is as follows:


for _ in range(int(input())):
       n=int(input())
       height=1
       i=1
       while i<=n:
            if i%2!=0:
                  height=2*height
            else:
                  height+=1
            i+=1
       print(height)


#Keep visiting for more solutions of HackerRank problems in Python 3.Thank you!!!

Tuesday, July 17, 2018

MAXGRITH - Maximum Girth : Classical

#The solution in Python3 is as follows:


for _ in range(int(input())):
n = int(input())
print ((((n+1)*2)//3) % 1000000007)


#Keep visiting for more solutions of SPOJ problems in Python 3.Thank you!!!

The Hurdle Race : Algorithms - Implementation

#The solution in Python3 is as follows:


n,k=map(int,input().split())
a=list(map(int,input().split()))
b=max(a)
if k>b:
    print(0)
else:
    print(b-k)

#Keep visiting for more solutions of HackerRank problems in Python 3.Thank you!!!

Monday, July 16, 2018

Designer PDF Viewer : Algorithms - Implementation

#The solution in Python3 is as follows:


a=list(map(int,input().split()))
s=input()
b=[]
alfa = "abcdefghijklmnopqrstuvwxyz"
rdict = dict([ (x[1],x[0]) for x in enumerate(alfa) ])
for i in s:
      b.append(a[rdict[i]])
print(len(b)*max(b))

#Keep visiting for more solutions of HackerRank problems in Python 3.Thank you!!!

Angry Professor : Algorithms - Implementation

#The solution in Python3 is as follows:


for _ in range(int(input())):
       n,k=map(int,input().split())
       a=list(map(int,input().split()))
       s=sum(1 for number in a if number<=0)    #Count the no. of  -ve numbers.
       if s>=k:
             print("NO")
        else:
             print("YES")

#Keep visiting for more solutions of HackerRank problems in Python 3.Thank you!!!

Soft Sort : Basic Programming - Implementation - Basics of Implementation

#The solution in Python3 is as follows:


if __name__ == '__main__':
    N = [0] * 1000000
    n = 1
    for i in range(1, 1000001):
        n = (n * i) % 1000000007
        N[i - 1] = n


    T = int(input())
    for t in range(0, T):
        n = int(input())
        print(3*(N[n - 1] + 1) % 1000000007)

#Keep visiting for more solutions of HackerEarth problems in Python 3.Thank you!!!

Thursday, July 12, 2018

Little Jhool and psychic powers : Basic Programming - Implementation - Basics of Implementation

#The solution in Python3 is as follows:


b=input()
a='000000'
c='111111'
if a in b or c in b:
    print("Sorry, sorry!")
else:
    print("Good luck!")

#Keep visiting for more solutions of HackerEarth problems in Python 3.Thank you!!!

Roy and Wobbly Numbers : Practice - Math

#The solution in Python3 is as follows:


import math
for _ in range(int(input())):
    n,k=list(map(int,input().split()))
    odd=math.ceil(k/9)
    even=k%9
    if even<=odd:
            if even!=0:
                even-=1
            else:
                even=9
                if k==81:
                    even-=1
       
    if k>81:
        print(-1)
    else:
        l=[]
        for i in range(n):
            if i%2==0:
                l.append(str(odd))
            else:
                l.append(str(even))
        print("".join(l))

#Keep visiting for more solutions of HackerRank problems in Python 3.Thank you!!!

Wednesday, July 11, 2018

Beautiful Days at the Movies : Algorithms-Implementation

#The solution in Python is as follows:


i,j,k=map(int,input().split())
l=i
beauty=0
while l<=j:
      a=l
      b=str(a)[::-1]
      c=int(b)
      if (c-a)%k==0:
               beauty+=1
      l+=1
print(beauty)

#Keep visiting for more solutions of HackerRank problems in Python 3.Thank you!!!

Sunday, July 8, 2018

EC_CONB - Even Numbers : Classical

#The solution in Python is as follows:


for _ in xrange(int(raw_input())):
        n=int(raw_input())
        if n%2==0:
                a=bin(n)[2:]
                b=a[::-1]
                print int(b,2)
        else:
                print n


#Keep visiting for more solutions of SPOJ problems in Python 3.Thank you!!!

Thursday, July 5, 2018

SYNC13C - WHAT A CO-ACCIDENT : Classical

#The solution in Python 3 is as follows:


for _ in range(int(input())):
c1,c2=map(int,input().split())
if c1%2 and c2%2:
print('Ramesh')
else:
print('Suresh')


#Keep visiting for more solutions of SPOJ problems in Python 3.Thank you!!!

Tuesday, July 3, 2018

HANGOVER - Hangover : Classical

#The solution in Python 3 is as follows:


while True:
    c=float(input())
    n=2
    if c==0:
        break
    while c>0:
        c-=1.0/n
        n+=1
    print(n-2,'card(s)')


#Keep visiting for more solutions of SPOJ problems in Python 3.Thank you!!!

Monday, July 2, 2018

MIRRORED - Mirrored Pairs : Tutorial

#The solution in Python 3 is as follows:


b=[]
while True:
     x=input()
     if x=="  ":
            print("Ready")
            for i in range(len(b)):
                    print(b[i])
            break
    if x=='pq' or x=='qp' or x=='db' or x=='bd':
           b.append("Mirrored pair")
    else:
           b.append("Ordinary pair")


#Keep visiting for more solutions of SPOJ problems in Python 3.Thank you!!!

GERGOVIA - Wine trading in Gergovia : Classical

#The solution in Python 3 is as follows:


while True:
t=0
w=0
if int(input()) == 0:
               break
for a in list(map(str,input().split())):
t+= int(a)
w+= abs(t)
print(w)


#Keep visiting for more solutions of SPOJ problems in Python 3.Thank you!!!

Sunday, July 1, 2018

VHELSING - Van Helsings gun : Classical

#The solution in Python 3 is as follows:


import math
for _ in range(int(input())):
 r=int(input())
 print('%.4f' % (8*(2-(math.sqrt(2)))*(pow(r,3))))


#Visit this link for detailed information.

#Keep visiting for more solutions of SPOJ problems in Python 3.Thank you!!!

Saturday, June 30, 2018

M00PAIR - 0 0 Pairs : Classical

#The solution in Python 3 is as follows:


import sys
A = [0, 1]
for _ in range(1000):
A.append(2*A[-2] + A[-1])
for ns in sys.stdin:
print(A[int(ns)-1])


#Keep visiting for more solutions of SPOJ problems in Python 3.Thank you!!!



Thursday, June 28, 2018

CANDY - Candy I : Classical

#The solution in Python 3 is as follows:


while True:
n = int(input())
if n == -1: break
A = [int(input()) for _ in range(n)]
s = sum(A)
if s%n:                    #check if the candies can be distributed equally
print(-1)       
else:
print(sum(abs(s//n-a) for a in A)//2)


#Keep visiting for more solutions of SPOJ problems in Python 3.Thank you!!!

Wednesday, June 27, 2018

TWOSQRS - Two squares or not two squares : Classical

#The solution in Python 3 is as follows:

def check(n):
   i=2
   while i*i<=n:
       count=0
       if n%i==0:
           while n%i==0:
               count+=1
               n//=i
           if i%4==3 and count%2!=0:
               return False
       i+=1
   return n%4!=3
for _ in range(int(input())):
    n=int(input())
    if (check(n)):
        print("Yes")
    else:
        print("No")

#Visit this link for detailed explanation

#Keep visiting for more solutions of SPOJ problems in Python 3.Thank you!!!

SNGPG - Prime Generator The Easiest Question Ever : Classical

#The solution in Python 3 is as follows:

for _ in range(int(input())):
a, b = map(int, input().split())
if a < 4:
print (min(3,b)-a+1)            #Just observe the output pattern
else:
print(0)


#Keep visiting for more solutions of SPOJ problems in Python 3.Thank you!!!

Monday, June 25, 2018

LASTDIG - The last digit : Classical

#The solution in Python 3 is as follows:

for _ in range(int(input())):
a, b = map(int, input().split())
print (pow(a, b, 10))                                   #((a^b)%10)


#Keep visiting for more solutions of SPOJ problems in Python 3.Thank you!!!

Sunday, June 24, 2018

ACPC10A - What’s Next : Classical

#The solution in Python 3 is as follows:

import sys
while 1:
    a, b, c = map(int,sys.stdin.readline().split())
    if a == 0 and b == 0 and c == 0:
        break
    elif b - a == c - b:
        print ("AP", c + (b - a))               #next term for AP sequences
    elif b/a == c/b:
        print ("GP", c * (b//a))                #next term for GP sequences


#Keep visiting for more solutions of SPOJ problems in Python 3.Thank you!!!

CODCHESS - Naya Shatranj (New Chess) : Classical

#The solution in Python 3 is as follows:

for _ in range(int(input())): print (1 - (int(input())%2))


#Keep visiting for more solutions of SPOJ problems in Python 3.Thank you!!!

Saturday, June 23, 2018

QCJ2 - Another Box Problem : Classical

#The solution in Python 3 is as follows:

from math import factorial as f
while True:
n = int(input())
if n==0:
           break
print ((f(2*n) // f(n) // f(n+1)) % 761238923)        #simple probability


#Keep visiting for more solutions of SPOJ problems in Python 3.Thank you!!!

RROOT - REAL ROOTS : Classical

#The solution in Python 3 is as follows:

for _ in range(int(input())): print ('%.6f' % (1 - (2**0.5/3)/(int(input())**0.5)))


#Keep visiting for more solutions of SPOJ problems in Python 3.Thank you!!!

Thursday, June 21, 2018

EIGHTS - Triple Fat Ladies : Classical

#The solution in Python 3 is as follows:

t=int(input())
i=0
while i<t:
k=int(input())
if k==1:
print(192)
else:
print(192+((k-1)*250))
i+=1


#Keep visiting for more solutions of SPOJ problems in Python 3.Thank you!!!

DOTAA - DOTA HEROES : Classical

#The solution in Python 3 is as follows:

for _ in range(int(input())):
    n, m, D = map(int, input().split())
    c = sum((int(input())-1)//D for _ in range(n))
    print('YES') if c >= m else print('NO')


#Keep visiting for more solutions of SPOJ problems in Python 3.Thank you!!!

Sunday, June 17, 2018

IITKWPCN - Playing With Balls : Classical

#The solution in Python 3 is as follows:

for _ in range(int(input())):
    B = int(input().split()[1])
    print ('1.000000')  if B%2 else  print('0.000000')


#Keep visiting for more solutions of SPOJ problems in Python 3.Thank you!!!

Saturday, June 16, 2018

FCTRL2 - Small factorials : Classical

#The solution in Python 3 is as follows:

t = int(input())
i=0
while i<t:
    a=int(input())
    j=1
    fact=1
    while j<=a:
        fact=fact*j            #you can also use factorial() function from math
        j+=1
    print(fact) 
    i+=1


#Keep visiting for more solutions of SPOJ problems in Python 3.Thank you!!!

IITKWPCA - Niceness of the string : Classical

#The solution in Python 3 is as follows:

for _ in range(int(input())): print (len(set(input().split())))


#Keep visiting for more solutions of SPOJ problems in Python 3.Thank you!!!

KUSAC - Kusac : Classical

#The solution in Python 3 is as follows:

from fractions import gcd
N, M = map(int, input().split())
print (M - gcd(M, N))                              #simple formula for output


#Keep visiting for more solutions of SPOJ problems in Python 3.Thank you!!!

UJ - Uncle Jack : Classical

#The solution in Python 3 is as follows:

while True:
    N, D = map(int, input().split())
    if N==0 and D==0: break
    print (pow(N, D))                                    #Output follows this pattern


#Keep visiting for more solutions of SPOJ problems in Python 3.Thank you!!!

FASHION - Fashion Shows : Classical

#The solution in Python 3 is as follows:

t=int(input())
i=0
while i<t:
    s=0
    n=int(input())
    a=list(map(int, input().split()))       #store the inputs in a list
    b=list(map(int, input().split()))
    a.sort()                                           #sort the lists in ascending order
    b.sort()
    j=0
    while j<n:
        s+=a[j]*b[j]                                #required calculation
        j+=1
    print(s)
    i+=1


#Keep visiting for more solutions of SPOJ problems in Python 3.Thank you!!!

Friday, June 15, 2018

TAP2013G - War : Classical

#The solution in Python 3 is as follows:

S = int(input())
Q = sorted(map(int, input().split()))          #sort the combat skills
N = sorted(map(int, input().split()))
b = 0
for j in range(S):
    if Q[b] < N[j]: b+=1                             #simply compare
print(b)


#Keep visiting for more solutions of SPOJ problems in Python 3.Thank you!!!

YAPP - Yet Another Permutations Problem : Classical

#The solution in Python 3 is as follows:

for _ in range(int(input())): print (pow(2, int(input())-1, 1000000007))

#No need of permutation


#Keep visiting for more solutions of SPOJ problems in Python 3.Thank you!!!

Thursday, June 14, 2018

QUADAREA - Maximal Quadrilateral Area : Classical

#The solution in Python 3 is as follows:

from math import sqrt
t=int(input())
i=0
while i<t:
    a, b, c, d = map(float, input().split())
    s = (a+b+c+d)/2
    print (round(sqrt(((s-a)*(s-b)*(s-c)*(s-d))),2))     #Brahmagupta's Formula
    i+=1


#Visit this link for details.

#Keep visiting for more solutions of SPOJ problems in Python 3.Thank you!!!

MANGOES - Real Mangoes for Ranjith : Classical

#The solution in Python 3 is as follows:

t=int(input())
i=0
while i<t:
N = int(input())
print (((N//2 + 1)**2) % N)      #Output follows this pattern
i+=1


#Keep visiting for more solutions of SPOJ problems in Python 3.Thank you!!!

ENIGMATH - PLAY WITH MATH : Classical

#The solution in Python 3 is as follows:

from fractions import gcd
t=int(input())
i=0
while i<t:
A, B = map(int, input().split())        #input a and b
g = gcd(A, B)                                   #finding GCD
print (B//g, A//g)
i+=1


#Visit this link for detailed understanding.

#Keep visiting for more solutions of SPOJ problems in Python 3.Thank you!!!

PIR - Pyramids : Classical

#The solution in Python 3 is as follows:

from math import sqrt
t=int(input())
i=0
while i<t:
    A, D, B, E, C, F = map(lambda x: int(x)**2, input().split())
    volume = sqrt((-A*B*C - A*D*E - B*D*F - C*E*F + A*C*D + B*C*D + \
                    A*B*E + B*C*E + B*D*E + C*D*E + A*B*F + A*C*F + \
                    A*D*F + C*D*F + A*E*F + B*E*F - C*C*D - C*D*D - \
                    B*B*E - B*E*E - A*A*F - A*F*F)/144.0)   
    print (round(volume,4))
    i+=1


#Visit Volume of a Pyramid for more info

#Keep visiting for more solutions of SPOJ problems in Python 3.Thank you!!!

VERODOOM - Vero Dominoes : Classical

#The solution in Python 3 is as follows:

t=int(input())
i=0
while i<t:
n = int(input())
print ((n*(n+1)*(n+2))//2)
i+=1


#Keep visiting for more solutions of SPOJ problems in Python 3.Thank you!!!

AP2 - AP - Complete The Series (Easy) : Classical

#The solution in Python 3 is as follows:

t= int(input())
i=0
while i<t:
a, b, c = map(int,input().split())
n = (c*2)//(a+b)                             #No.of terms
d = (b-a)//(n-5)                              #difference
m = a-2*d                                     #first element
print (n)
print(" ".join(str(m+d*i) for i in range(n)))
i+=1


#Keep visiting for more solutions of SPOJ problems in Python 3.Thank you!!!

Thursday, May 24, 2018

FCTRL - Factorial : Classical

#The solution in Python 3 is as follows:

def count (x):               #function to count the no. of trailing zeros
    i = 5
    zeros = 0
    while x >= i:
        zeros += x // i
        i *= 5                 
    return zeros
t=int(input())              #No. of test cases 
j=0
while j<t:
    a=int(input())
    print(count(a))
    j+=1


#Visit this link for more info.

#Keep visiting for more solutions of SPOJ problems in Python 3.Thank you!!!

STRHH - Half of the half : Basics

#The solution in Python 3 is as follows:

a = []
t = int(input())                     #No. of test cases
i=0
while i<t:
    r = input()
    a.append(r)                      #store the strings in a list
    i+=1
for j in a:
    k=0
    while(k<(len(j)//2)):          #continue loop till half of the string.
        print(j[k],end='')            #Print every second character.
        k+=2
    print()



#Keep visiting for more solutions of SPOJ problems in Python 3.Thank you!!!

Wednesday, May 23, 2018

TEST - Life,Universe and Everything : Classical

#The solution in Python 3 is as follows:

n=int(input())
while n!=42:
    print(n)
    n=int(input())


#Keep visiting for more solutions of SPOJ problems in Python 3.Thank you!!!

FCTRL2 - Small Factorials : Classical

#The solution in Python 3 is as follows:

t = int(input())                      #No.of test cases
i=0
while i<t:
    a=int(input())
    j=1
    fact=1
    while j<=a:
        fact=fact*j                    #compute the factorial
        j+=1
    print(fact) 
    i+=1


#Keep visiting for more solutions of SPOJ problems in Python 3.Thank you!!!

JULKA - Julka : Classical

#The solution in Python 3 is as follows:

t=10
i=0
while i<t:
    a=int(input())                   #No. of apples both have
    b=int(input())                   #No. of  extra apples Klaudia has
    c=(a-b)//2
    print(b+c)               
    print(c)
    i+=1


#Keep visiting for more solutions of SPOJ problems in Python 3.Thank you!!!

AE00 - Rectangles : Classical

#The Python 3 solution is as follows:

import math
a=int(input())                               #input the no. of squares
i=1
sum=0                                         #Count the no. of  pairs(m*n)of factors of a
while i<=a:                                 #such that m*n=no.of squares
    n=math.sqrt(i)                        #sqrt is done to rule out n*m case
    j=1
    while j<=n:
        if(i%j==0):                           
            sum+=1
        j+=1 
    i+=1
print(sum)                              #display the no.of pairs=no. of rectangles 


#Keep visiting for more solutions of SPOJ problems in Python 3.Thank you!!!

ONP - Transform the expression : Classical

#The Python 3 solution is as follows:

for i in range(int(input())):                               #No. of test cases
    a=input()                                                      #input the actual expression
    o=[]
    s=""
    for j in a:
        if(j== '(' ):                                               #ignore the brackets '(' and ')'
            pass
        elif(ord(j)>=97 and ord(j)<=123):         #consider the lower case letters
            s=s+str(j)
        elif(j== ')' ):
            if(len(o)!=0):
                s=s+str(o.pop())                            #o.pop() removes & returns last item in list
        else:
            o.append(j)                                        #add the operators to the list
    print(s)


#Visit this link for more details.

#Keep visiting for more solutions of SPOJ problems in Python 3.Thank you!!!!

ADDREV : Classical

#The Python 3 solution is as follows:

import sys
t = int(input())
i=0
while i<t:
    a,b = map(int,sys.stdin.readline().split())     #to take both inputs in single line
    c=str(a)                                                         #converting the number into string
    d=str(b)
    e=int(c[::-1])                                                #converting reverse of the string c to int
    f=int(d[::-1])                                               #converting reverse of the string d to int
    s=e+f                                                          #adding the two reverse numbers
    s1=str(s)
    s2=int(s1[::-1])                                           #reverse s and display it
    print(s2)                                                   
    i+=1


#Keep visiting for more solutions of SPOJ problems in Python 3.Thank you!!!