Generate an array consisting of most frequent greater elements present on the right side of each array element | Function to generate an array containing the most frequent greater element on the right side of each array element ; Stores the generated array ; Traverse the array arr [ ] ; Store the result for the current index and its frequency ; Iterate over the right subarray ; Store the frequency of the current array element ; If the frequencies are equal ; Update ans to smaller of the two elements ; If count of new element is more than count of ans ; Insert answer in the array ; Print the resultant array ; Driver Code ; Given Input
def findArray ( arr , n ) : NEW_LINE INDENT v = [ ] NEW_LINE for i in range ( n ) : NEW_LINE INDENT ans = - 1 NEW_LINE old_c = 0 NEW_LINE for j in range ( i + 1 , n ) : NEW_LINE INDENT if ( arr [ j ] > arr [ i ] ) : NEW_LINE INDENT curr_c = arr [ j : n + 1 ] . count ( arr [ j ] ) NEW_LINE if ( curr_c == old_c ) : NEW_LINE INDENT if ( arr [ j ] < ans ) : NEW_LINE INDENT ans = arr [ j ] NEW_LINE DEDENT DEDENT if ( curr_c > old_c ) : NEW_LINE INDENT ans = arr [ j ] NEW_LINE old_c = curr_c NEW_LINE DEDENT DEDENT DEDENT v . append ( ans ) NEW_LINE DEDENT for i in range ( len ( v ) ) : NEW_LINE INDENT print ( v [ i ] , end = " ▁ " ) NEW_LINE DEDENT DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT arr = [ 4 , 5 , 2 , 25 , 10 , 5 , 10 , 3 , 10 , 5 ] NEW_LINE size = len ( arr ) NEW_LINE findArray ( arr , size ) NEW_LINE DEDENT