You are not logged in. Please login at www.codechef.com to post your questions!

×

help in binary search problem.

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

slugger's gravatar image

1★slugger
544
accept rate: 25%

edited 26 Sep '17, 17:11

toggle preview
Preview

Follow this question

By Email:

Once you sign in you will be able to subscribe for any updates here

By RSS:

Answers

Answers and Comments

Markdown Basics

  • *italic* or _italic_
  • **bold** or __bold__
  • link:[text](http://url.com/ "title")
  • 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