KIIT CAAS Find Majority element

Run Settings
LanguagePython
Language Version
Run Command
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))
Editor Settings
Theme
Key bindings
Full width
Lines