Menu Close

Find the smallest element in the list that is larger than the given target.

Given a list of sorted characters letters containing only lowercase letters, and given a target letter target, find the smallest element in the list that is larger than the given target. Letters also wrap around. For example, if the target is target=’z’ and letters = [‘a’,’b’], the answer is ‘a’.

string=list(map(str,input('Enter the list of Characters: ').split(' ')))
target=input("Enter the Target Character: ")
flag=0
for i in string:
    if i>target:
        print(i)
        flag=1
        break
if flag==0: print(string[0])

Input_1:
Enter the list of Characters: a b
Enter the Target Character: z
Output: a

Input_2:
Enter the list of Characters: c f j
Enter the Target Character: a
Output: c

Input_3:
Enter the list of Characters: a f k o p
Enter the Target Character: h
Output: k

Morae Q!