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 let curr_c = count ( & arr [ j ] , & arr [ n ] , arr [ j ] ) ; ; 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 ; Given Input
function findArray ( arr , n ) { let v = new Array ( ) ; for ( let i = 0 ; i < n ; i ++ ) { let ans = - 1 , old_c = 0 ; for ( let j = i + 1 ; j < n ; j ++ ) { if ( arr [ j ] > arr [ i ] ) { let curr_c = 0 ; arr . slice ( j , n ) . forEach ( ( item ) => { if ( item == arr [ j ] ) { curr_c ++ } } ) if ( curr_c == old_c ) { if ( arr [ j ] < ans ) ans = arr [ j ] ; } if ( curr_c > old_c ) { ans = arr [ j ] ; old_c = curr_c ; } } } v . push ( ans ) ; } for ( let i = 0 ; i < v . length ; i ++ ) document . write ( v [ i ] + " " ) ; } let arr = [ 4 , 5 , 2 , 25 , 10 , 5 , 10 , 3 , 10 , 5 ] ; let size = arr . length findArray ( arr , size ) ;