Minimum salary hike for each employee such that no employee feels unfair | Javascript Program to find the minimum hike of each employee such that no adjacent employee feels unfair ; As hikes are positive integers , keeping minimum value ; Pass - 1 : compare with previous employee ; Pass - 2 : compare with Next employee ; Driver Code ; Function Call ; result -> [ 2 , 1 , 3 , 2 , 1 , 2 ] ; Function Call ; result -> [ 1 , 2 , 3 , 1 ] ; Function Call ; result -> [ 1 , 2 ]
function findMinimumHike ( ratings , employees ) { let hikes = new Array ( employees ) ; for ( let i = 0 ; i < employees ; i ++ ) hikes [ i ] = 1 ; for ( let i = 1 ; i < employees ; i ++ ) { if ( ratings [ i - 1 ] < ratings [ i ] && hikes [ i - 1 ] >= hikes [ i ] ) hikes [ i ] = hikes [ i - 1 ] + 1 ; } for ( let i = employees - 2 ; i >= 0 ; i -- ) { if ( ratings [ i ] > ratings [ i + 1 ] && hikes [ i + 1 ] >= hikes [ i ] ) hikes [ i ] = hikes [ i + 1 ] + 1 ; } return hikes ; } let data = [ 5 , 3 , 4 , 2 , 1 , 6 ] ; let result = findMinimumHike ( data , data . length ) ; document . write ( " " + ( result ) . join ( " " ) + " " ) ; data = [ 1 , 3 , 5 , 4 ] ; result = findMinimumHike ( data , data . length ) ; document . write ( " " + ( result ) . join ( " " ) + " " ) ; data = [ 1 , 4 ] ; result = findMinimumHike ( data , data . length ) ; document . write ( " " + ( result ) . join ( " " ) + " " ) ;