Check Sorted Order

Check Sorted Order

Problem:

The program must accept N integers which are sorted in ascending order except one integer. But if that single integer R is reversed, the entire array will be in sorted order. The program must print the first integer that must be reversed so that the entire array will be sorted in ascending order. 

Boundary Condition(s): 

2 <= N <= 20 

Input Format: 

The first line contains N. 

The second line contains N integer values separated by a space. 

Output Format: 

The first line contains the integer value R. 

Example Input/Output 1: 

Input: 

10 71 20 30 33 

Output: 

71 

Explanation: 

When 71 is reversed the array becomes sorted as below. 

10 17 20 30 33 

Example Input/Output 2: 

Input: 

10 20 30 33 64 58 

Output: 

64 

Example Input/Output 3: 

Input: 

10 20 30 33 67 58 

Output: 

58

Program:

n = int(input())

l = list(map(int,input().split()))

for i in range(n): 

    k = l.copy()

    k[i] = int(str(k[i])[::-1])

    if k == sorted(k):

        print(l[i])

        break

Comments

Popular posts from this blog

Pronic Integers in N - InfyTQ question

Count Strong Points - Accenture

Letters at position of multiples of a number