ENCDEC5 - Editorial

PROBLEM LINK:

Practice

Author: Anuska Sinha
Tester: Akash Kumar Bhagat Arnab Chanda

DIFFICULTY:

EASY-MED

PREREQUISITES:

Math

PROBLEM:

An array of numbers is given and an integer number (say M) is given. After performing 3 operations on the array, you will have to find the immediate greater and smaller numbers to the number provided (say M).

EXPLANATION:

Take an array … ( say [ 5, 15,1, 66, 55, 32, 40, 22, 34, 11 ] ) and take an integer M= 38
Now, perform the 1st step by swapping 5 and 15, 1 and 66, 55 and 32 and so on. ( Note that if the number of elements in the array is odd, then leave the last number of the array in it’s place)
Then in the step 2, add %3 of each element to itself.
The array after step 2 will look like [15, 7, 66, 2, 34, 56, 23, 41, 13, 35]
If the 3 and the last step, swap the 1st and last, 2nd and the 2nd last element and so on.
After this step the final array is [35, 13, 41, 23, 56, 34, 2, 66, 7, 15]
In this array, find the immediate greater and smaller number to M and print it.

In special cases where there is no greater or smaller number or both, print -1 in their places

SOLUTIONS:

Python
 import bisect
for _ in range(int(input())):
    n=int(input())
    a=[int(i) for i in input().split()]
    m=int(input())
    for i in range(n):
        a[i]+=a[i]%3
    a=sorted(a)
    x=bisect.bisect_left(a,m)
    y=bisect.bisect_right(a,m)
    #print(a)
    inc=0;dec=0
    if x-1<0:
        inc=-1
    else:
        inc=a[x-1]
        
    if y==n:
        dec=-1
    else:
        dec=a[y]

    print(inc,dec)

1 Like