Quick actions

cmd+k|ctrl+k

Navigation

Languages

KIIT CAAS Find Majority element

Snippet info

Language

Python

Visibility

public

Author

fcb.satadrud10s

Created

2023-07-03T14:32:36.502308Z

Updated

2023-07-18T11:06:57.709522Z

from typing import List
# def find(arr : List[int], ele : int) -> int:
#     majorityInd = 0
#     cnt = 0
#     for i in range(len(arr)):
#         if arr[majorityInd] == arr[i]:
#             cnt += 1
#         else:
#             cnt -= 1
#         if cnt == 0
#             majorityInd = i
#             cnt = i
   
# Kth largest element
def find(arr, K):
    max_ele = arr[0] 
    maxElementInd = 0
    for i in range(len(arr)):
        if (arr[i] // K) > max_ele:
            max_ele = arr[i] // K
            maxElementInd = i
    return arr[maxElementInd] # use an array when the i/p size is fixed for faster access to elements. When i/p size isn't ficed, it's recommended to use Linked List
arr = [2,5,2,2,6,2,6]
print(find(arr, 2))

            
INFO