Skip to content

Instantly share code, notes, and snippets.

@praveeng1618
Last active October 17, 2019 18:53
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save praveeng1618/f2554980dadc21cc3b2d2398e6bd38e6 to your computer and use it in GitHub Desktop.
Save praveeng1618/f2554980dadc21cc3b2d2398e6bd38e6 to your computer and use it in GitHub Desktop.
Given a sorted array, A, with possibly duplicated elements, find the indices of the first and last occurrences of a target element, x. Return -1 if the target is not found.
flist=[]
slist=[]
count = 0
A=[int(x) for x in input().split()]
target = int(input())
for x in range(len(A)) :
if target == A[x] :
flist.append(x)
count+=1
if count == 0:
print("[-1,-1]")
else:
slist.append(flist[0])
slist.append(flist[-1])
print(slist)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment