Welcome to collectivesolver - Programming & Software Q&A with code examples. A website with trusted programming answers. All programs are tested and work.

Contact: aviboots(AT)netvision.net.il

Buy a domain name - Register cheap domain names from $0.99 - Namecheap

Scalable Hosting That Grows With You

Secure & Reliable Web Hosting, Free Domain, Free SSL, 1-Click WordPress Install, Expert 24/7 Support

Semrush - keyword research tool

Boost your online presence with premium web hosting and servers

Disclosure: My content contains affiliate links.

39,913 questions

51,845 answers

573 users

How to search for an element in circular sorted integer array with Python

1 Answer

0 votes
def searchCircularSortedArray(lst,  element) :
    low = 0
    high = len(lst) - 1
    
    while (low <= high) :
        mid = int((low + high) / 2)
        if (element == lst[mid]) :
            return mid
        if (lst[mid] <= lst[high]) :
            if (element > lst[mid] and element <= lst[high]) :
                low = mid + 1
            else :
                high = mid - 1
        else :
            if (element >= lst[low] and element < lst[mid]) :
                high = mid - 1
            else :
                low = mid + 1
    return -1
    
lst = [6, 9, 10, 13, 2, 3, 5, 6, 8]
element = 5

index = searchCircularSortedArray(lst, element)

if (index != -1) :
    print("index = " + str(index), end ="")
else :
    print("Element not found", end ="")


     
     
     
'''
run:
      
index = 6
 
'''

 



answered Nov 24, 2023 by avibootz
...