Skip to content Skip to sidebar Skip to footer

Code To Count How Many Integers Are Strictly Larger Than All The Integers To Their Right Using Python

Given the list of integers, X, https://www.google.com/url?q=https://docs.google.com/document/d/1TjeNYpZ_PbdBISlJPF-_WqULBc1WpuYthLClovjB3Rs/edit?usp%3Dsharing&sa=D&ust=1594

Solution 1:

Here's a solution that does that in O(N), where N is the size of the list.

l = [12,4,4,2,2,3]

def max_so_far(l):
    m = 0
    for i in l[::-1]: 
        m = max(m, i)
        yield m

sum([x>y for x,y in zip(l[-2::-1], max_so_far(l))])

Solution 2:

How about this code?

mylist = list(map(int,input().split()))
count = 0

for i,item in enumerate(mylist): 
    if i == len(mylist) - 1: #here we excluded the last element for a check
        break

    check = max(mylist[i+1:])
    if check < item:
        count = count + 1    

print(count)

Solution 3:

I am understating this problem in two way:

    counter = 0
    for i in range(len(list_of_number)):
        for j in range(i, len(list_of_number)):
           if list_of_number[j] > list_of_number[i]:
               counter += 1

Here, after it gets the first value, it scans all the list. The code that follow it will check only the number's right neighbour

    counter = 0
    for i in range(len(list_of_number)-1):
        if list_of_numbers[i+1] > list_of_numbers[i]:
            counter +=1

Solution 4:

How about this?

def counter(L):
    return sum([1 for i in range(len(L)-1) if L[i] < L[i+1]])

Post a Comment for "Code To Count How Many Integers Are Strictly Larger Than All The Integers To Their Right Using Python"