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