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!!!