Friday, February 1, 2019

Binary Search

  • Problem Description

    Write a program to implement binary search algorithm
  • CODING ARENA
  • a=int(input())
    x=[]
    y=[]
    flag=0
    start=0
    end=0
    for i in range(a):
        b=int(input())
        x.append(b)
    b=int(input())
    for i in range(b):
        c=int(input())
        y.append(c)
    for i in range(a):
            if(y[0]==x[i]):
                start=i
              
            if(y[2]==x[i]):
                end=i
                flag=1
    if(flag==1):
        print("Sequence found between index",start,"and",end+1)
    else:
        print("Not found")
                
  • Test Case 1

    Input (stdin)
    20
    
    1
    
    9
    
    18
    
    24
    
    27
    
    35
    
    38
    
    41
    
    49
    
    53
    
    55
    
    66
    
    67
    
    72
    
    75
    
    77
    
    81
    
    89
    
    90
    
    97
    
    3
    
    35
    
    38
    
    41
    
    
    Expected Output
    Sequence found between index 5 and 8
  • Test Case 2

    Input (stdin)
    20
    
    1
    
    9
    
    18
    
    24
    
    27
    
    35
    
    38
    
    41
    
    49
    
    53
    
    55
    
    66
    
    67
    
    72
    
    75
    
    77
    
    81
    
    89
    
    90
    
    97
    
    3
    
    35
    
    38
    
    40
    
    
    Expected Output
    Not found

No comments:

Post a Comment