×

# help in binary search problem.

 0 Can someone explain me what am i doing wrong in applying binary search in this problem. My solution: def binary(aa,val): lo=0 hi=len(aa)-1 while(lo<=hi): mid=(hi+lo)//2 if(val<=aa[mid] and mid==0): return mid+1 if(val<=aa[mid] and val>=aa[mid-1] and mid!=0): return mid+1 elif(val>a[mid]): lo=mid+1 else: hi=mid-1 return 0 n=int(input()) a=list(map(int,input().split())) m=int(input()) q=list(map(int,input().split())) aa=[] sa=0 for i in a: sa+=i aa.append(sa) for i in range(m): print(binary(aa,q[i]))  asked 26 Sep '17, 17:11 1★slugger 54●4 accept rate: 25%
 toggle preview community wiki:
Preview

By Email:

Markdown Basics

• *italic* or _italic_
• **bold** or __bold__
• image?![alt text](/path/img.jpg "title")
• numbered list: 1. Foo 2. Bar
• to add a line break simply add two spaces to where you would like the new line to be.
• basic HTML tags are also supported
• mathemetical formulas in Latex between \$ symbol

Question tags:

×1,038
×672

question asked: 26 Sep '17, 17:11

question was seen: 196 times

last updated: 26 Sep '17, 17:11