Minimum salary hike for each employee such that no employee feels unfair
Last Updated :
24 Sep, 2021
There are N employees in a company, and each employee has some ratings. The employees are given a hike in their salary based on their ratings, i.e., employees with higher ratings will get a higher hike in their salary. An employee only knows the hike and rating of its neighbors i.e., on the left and right side of the employee.
Given an array arr[] of N positive integers which denotes the ratings of N employees, the task is to find the minimum hike that should be raised for each employee, such that no employee feels unfair.
Note: The hikes are positive integers only and the ratings are always greater than zero.
Example:
Input: arr[] = {1, 3, 5, 4}
Output: 1 2 3 1
Explanation:
The distribution of minimum hike for each employee must be:
1 + 2 + 3 + 1 = 6
Input: arr[] = {5, 3, 4, 2, 1, 6}
Output: 2 1 3 2 1 2
Explanation:
The distribution of minimum hike for each employee must be:
2 + 1 + 3 + 2 + 1 + 2 = 11
Approach: This problem can be solved using Greedy Approach. As employees know hike and ratings of only their neighbor only then following would be one of the conditions which will hold true on the given ratings:
- Type 1: Hi – 1 > Hi < Hi + 1
- Type 2: Hi – 1 < Hi < Hi + 1
- Type 3: Hi – 1 > Hi > Hi + 1
- Type 4: Hi – 1 < Hi > Hi + 1
For each employee based on the above-mentioned conditions set hike of each employee as:
- For Type 1: Set hike to 1.
- For Type 2: Raised his hike by Hi-1 + 1.
- For Type 3: Raised his hike by Hi+1 + 1.
- For Type 4: Raised his hike by max(Hi-1, Hi+1) + 1.
Below is the implementation of the above approach:
C++
#include <bits/stdc++.h>
using namespace std;
#define INF 1e9
void findMinHike(vector< int > arr,
int n)
{
arr.insert(arr.begin(), INF);
arr.push_back(INF);
vector< int > hike(n + 2, 0);
for ( int i = 1; i <= n; i++) {
if (arr[i - 1] >= arr[i]
&& arr[i] <= arr[i + 1]) {
hike[i] = 1;
}
}
for ( int i = 1; i <= n; i++) {
if (arr[i - 1] < arr[i]
&& arr[i] <= arr[i + 1]) {
hike[i] = hike[i - 1] + 1;
}
}
for ( int i = 1; i <= n; i++) {
if (arr[i - 1] >= arr[i]
&& arr[i] > arr[i + 1]) {
hike[i] = hike[i + 1] + 1;
}
}
for ( int i = 1; i <= n; i++) {
if (arr[i - 1] < arr[i]
&& arr[i] > arr[i + 1]) {
hike[i] = max(hike[i - 1],
hike[i + 1])
+ 1;
}
}
for ( int i = 1; i <= n; i++) {
cout << hike[i] << " " ;
}
}
int main()
{
vector< int > arr = { 5, 3, 4, 2, 1, 6 };
findMinHike(arr, arr.size());
return 0;
}
|
Java
import java.util.*;
class GFG{
static final int INF = 10000009 ;
static void findMinHike(Vector<Integer> arr,
int n)
{
arr.add( 0 , INF);
arr.add(INF);
int []hike = new int [n + 2 ];
for ( int i = 1 ; i <= n; i++)
{
if (arr.get(i - 1 ) >= arr.get(i) &&
arr.get(i) <= arr.get(i + 1 ))
{
hike[i] = 1 ;
}
}
for ( int i = 1 ; i <= n; i++)
{
if (arr.get(i - 1 ) < arr.get(i) &&
arr.get(i) <= arr.get(i + 1 ))
{
hike[i] = hike[i - 1 ] + 1 ;
}
}
for ( int i = 1 ; i <= n; i++)
{
if (arr.get(i - 1 ) >= arr.get(i) &&
arr.get(i) > arr.get(i + 1 ))
{
hike[i] = hike[i + 1 ] + 1 ;
}
}
for ( int i = 1 ; i <= n; i++)
{
if (arr.get(i - 1 ) < arr.get(i) &&
arr.get(i) > arr.get(i + 1 ))
{
hike[i] = Math.max(hike[i - 1 ],
hike[i + 1 ]) + 1 ;
}
}
for ( int i = 1 ; i <= n; i++)
{
System.out.print(hike[i] + " " );
}
}
public static void main(String[] args)
{
Vector<Integer> arr = new Vector<>();
arr.add( 5 );
arr.add( 3 );
arr.add( 4 );
arr.add( 2 );
arr.add( 1 );
arr.add( 6 );
findMinHike(arr, arr.size());
}
}
|
Python3
INF = 1e9
def findMinHike(arr,n):
arr.insert( 0 , INF)
arr.append(INF)
hike = [ 0 ] * (n + 2 )
for i in range ( 1 , n + 1 ):
if (arr[i - 1 ] > = arr[i] and
arr[i] < = arr[i + 1 ]):
hike[i] = 1
for i in range ( 1 , n + 1 ):
if (arr[i - 1 ] < arr[i] and
arr[i] < = arr[i + 1 ]):
hike[i] = hike[i - 1 ] + 1
for i in range ( 1 , n + 1 ):
if (arr[i - 1 ] > = arr[i] and
arr[i] > arr[i + 1 ]):
hike[i] = hike[i + 1 ] + 1
for i in range ( 1 , n + 1 ):
if (arr[i - 1 ] < arr[i] and
arr[i] > arr[i + 1 ]):
hike[i] = max (hike[i - 1 ],
hike[i + 1 ]) + 1
for i in range ( 1 , n + 1 ):
print (hike[i], end = " " )
if __name__ = = '__main__' :
arr = [ 5 , 3 , 4 , 2 , 1 , 6 ]
findMinHike(arr, len (arr))
|
C#
using System;
using System.Collections.Generic;
class GFG{
static readonly int INF = 10000009;
static void findMinHike(List< int > arr,
int n)
{
arr.Insert(0, INF);
arr.Add(INF);
int []hike = new int [n + 2];
for ( int i = 1; i <= n; i++)
{
if (arr[i - 1] >= arr[i] &&
arr[i] <= arr[i + 1])
{
hike[i] = 1;
}
}
for ( int i = 1; i <= n; i++)
{
if (arr[i - 1] < arr[i] &&
arr[i] <= arr[i + 1])
{
hike[i] = hike[i - 1] + 1;
}
}
for ( int i = 1; i <= n; i++)
{
if (arr[i - 1] >= arr[i] &&
arr[i] > arr[i + 1])
{
hike[i] = hike[i + 1] + 1;
}
}
for ( int i = 1; i <= n; i++)
{
if (arr[i - 1] < arr[i] &&
arr[i] > arr[i + 1])
{
hike[i] = Math.Max(hike[i - 1],
hike[i + 1]) + 1;
}
}
for ( int i = 1; i <= n; i++)
{
Console.Write(hike[i] + " " );
}
}
public static void Main(String[] args)
{
List< int > arr = new List< int >();
arr.Add(5);
arr.Add(3);
arr.Add(4);
arr.Add(2);
arr.Add(1);
arr.Add(6);
findMinHike(arr, arr.Count);
}
}
|
Javascript
<script>
let INF = 10000009;
function findMinHike(arr, n)
{
arr.unshift(INF);
arr.push(INF);
let hike = new Array(n + 2);
for (let i = 1; i <= n; i++)
{
if (arr[i-1] >= arr[i] &&
arr[i] <= arr[i+1])
{
hike[i] = 1;
}
}
for (let i = 1; i <= n; i++)
{
if (arr[i - 1] < arr[i] &&
arr[i] <= arr[i + 1])
{
hike[i] = hike[i - 1] + 1;
}
}
for (let i = 1; i <= n; i++)
{
if (arr[i-1] >= arr[i] &&
arr[i] > arr[i+1])
{
hike[i] = hike[i + 1] + 1;
}
}
for (let i = 1; i <= n; i++)
{
if (arr[i-1] < arr[i] &&
arr[i] > arr[i+1])
{
hike[i] = Math.max(hike[i - 1],
hike[i + 1]) + 1;
}
}
for (let i = 1; i <= n; i++)
{
document.write(hike[i] + " " );
}
}
let arr = [5,3,4,2,1,6];
findMinHike(arr, arr.length);
</script>
|
Time Complexity: O(N)
Auxiliary Space: O(N)
Approach 2: (Using 2 Passes)
- Initially assign the smallest possible hike to each employee i.e. 1
- Now we can’t decrease the current employee hike based on the hike of adjacent employees, always looks for increasing cases.
- Pass1 (from left -> right) compare with previous employee hike and increase the current employee hike if required.
- Pass2 (from right -> left) compare with the next employee hike and increase the current employee hike if required.
Below is the implementation of the above approach:
C++
#include <iostream>
using namespace std;
void findMinimumHike( int ratings[], int employees)
{
int hikes[employees];
for ( int i = 0; i < employees; i++)
{
hikes[i] = 1;
}
for ( int i = 1; i < employees; i++)
{
if (ratings[i - 1] < ratings[i] &&
hikes[i - 1] >= hikes[i])
{
hikes[i] = hikes[i - 1] + 1;
}
}
for ( int i = employees - 2; i >= 0; i--)
{
if (ratings[i] > ratings[i + 1] &&
hikes[i + 1] >= hikes[i])
{
hikes[i] = hikes[i + 1] + 1;
}
}
cout << "[" ;
int i;
for (i = 0; i < employees - 1; i++)
{
cout << hikes[i] << ", " ;
}
cout << hikes[i] << "]" << endl;
}
int main()
{
int data[] = { 5, 3, 4, 2, 1, 6 };
findMinimumHike(data, sizeof (data)/ sizeof (data[0]));
int data1[] = { 1, 3, 5, 4 };
findMinimumHike(data1, sizeof (data1)/ sizeof (data1[0]));
int data2[] = { 1, 4 };
findMinimumHike(data2, sizeof (data2)/ sizeof (data2[0]));
return 0;
}
|
Java
import java.io.*;
import java.util.*;
class GFG {
public static int [] findMinimumHike( int [] ratings,
int employees)
{
int [] hikes = new int [employees];
for ( int i = 0 ; i < employees; i++)
hikes[i] = 1 ;
for ( int i = 1 ; i < employees; i++) {
if (ratings[i - 1 ] < ratings[i]
&& hikes[i - 1 ] >= hikes[i])
hikes[i] = hikes[i - 1 ] + 1 ;
}
for ( int 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;
}
public static void main(String[] args)
{
int [] data = new int [] { 5 , 3 , 4 , 2 , 1 , 6 };
int [] result = findMinimumHike(data, data.length);
System.out.println(Arrays.toString(result));
data = new int [] { 1 , 3 , 5 , 4 };
result = findMinimumHike(data, data.length);
System.out.println(Arrays.toString(result));
data = new int [] { 1 , 4 };
result = findMinimumHike(data, data.length);
System.out.println(Arrays.toString(result));
}
}
|
Python3
def findMinimumHike(ratings, employees):
hikes = [ 1 for i in range (employees)]
for i in range ( 1 ,employees):
if (ratings[i - 1 ] < ratings[i] and hikes[i - 1 ] > = hikes[i]):
hikes[i] = hikes[i - 1 ] + 1 ;
for i in range (employees - 2 , - 1 , - 1 ):
if (ratings[i] > ratings[i + 1 ] and hikes[i + 1 ] > = hikes[i]):
hikes[i] = hikes[i + 1 ] + 1 ;
return hikes
data = [ 5 , 3 , 4 , 2 , 1 , 6 ]
result = findMinimumHike(data, len (data))
print (result)
data = [ 1 , 3 , 5 , 4 ]
result = findMinimumHike(data, len (data))
print (result)
data = [ 1 , 4 ]
result = findMinimumHike(data, len (data))
print (result)
|
C#
using System;
class GFG{
public static int [] findMinimumHike( int [] ratings,
int employees)
{
int [] hikes = new int [employees];
for ( int i = 0; i < employees; i++)
hikes[i] = 1;
for ( int i = 1; i < employees; i++)
{
if (ratings[i - 1] < ratings[i] &&
hikes[i - 1] >= hikes[i])
hikes[i] = hikes[i - 1] + 1;
}
for ( int 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;
}
public static void Main(String[] args)
{
int [] data = new int []{ 5, 3, 4, 2, 1, 6 };
int [] result = findMinimumHike(data, data.Length);
Console.WriteLine( "[" + String.Join( "," , result) + "]" );
data = new int []{ 1, 3, 5, 4 };
result = findMinimumHike(data, data.Length);
Console.WriteLine( "[" + String.Join( "," , result) + "]" );
data = new int []{ 1, 4 };
result = findMinimumHike(data, data.Length);
Console.WriteLine( "[" + String.Join( "," , result) + "]" );
}
}
|
Javascript
<script>
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( ", " )+ "]<br>" );
data = [1, 3, 5, 4 ];
result = findMinimumHike(data, data.length);
document.write( "[" +(result).join( ", " )+ "]<br>" );
data = [1, 4 ];
result = findMinimumHike(data, data.length);
document.write( "[" +(result).join( ", " )+ "]<br>" );
</script>
|
Output
[2, 1, 3, 2, 1, 2]
[1, 2, 3, 1]
[1, 2]
Time Complexity: O(N)
Auxiliary Space: O(N)
Share your thoughts in the comments
Please Login to comment...