Program to Find if a given Integer X appears more than N/2 times in a Sorted Array of N Integers

Method-1 

a=list(map(int,input().split()))
x=int(input())
if a[len(a)//2]==x:
    print("Yes")
else:
    print("NO")
    

Method-2

def find(a,x):
    l=len(a)
    if l%2==0:
        ind=(l//2)+1
        
    else:
        ind=l//2
    for i in range(ind):
        if a[i]==x and a[i+l//2]==x:
            return 1
a=list(map(int,input().split()))
x=int(input())
if find(a,x):
    print("Yes")
    


Input: 1 2 3 3 3 3 4 5 3 

 Output Yes

No comments:

Post a Comment