Header Ads Widget

Program to find the perfect city


 Program to find the perfect city

1. A city[i] is 'magical' if city[i] and city[i + 1] have no common divisor other than 1.

 2. A city[i] is 'good' if the percentage of zombies in the city[i]) is more than percentage of zombies in city[i+1].

Find a city that is perfect, where perfect means both good and magical.

If there are more than one perfect cities, output the left-most city index. Also, the minimum number of cities in a country is 2 and there will be at least one perfect city.

Input Specification :

input1:  An array representing the percentage of zombies in each city

input2: Number of cities in the country . 

Output Specification:  Return the favorable city index ' i '.

Examples 1:-

input1:[1,1,3,6,7,3]

input2:6


Output:-





Program to find the perfect city

The objective of the code is to  find perfect city . perfect city is the city which is both good and magical both .

Python Code:-

def gcd(a,b):
if(b==0):
return abs(a)
else:
return gcd(b,a%b)
def find(input1,input2):
for i in range(input2-1):
a=gcd(input1[i],input1[i+1])
if a==1 and input1[i]>input1[i+1]:
return i
l=list(map(int,input().split()))
n=int(input())
print(find(l,n))

Output:

4 1 3 2
4
0

Wipro :-

Infytq :-

Key Points;-

Hackerrank:-


C-tutorial:-

See more:-