Minimum salary hike for each employee such that no employee feels unfair

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 + 1Type 2: Hi – 1 < Hi < Hi + 1Type 3: Hi – 1 > Hi > Hi + 1Type 4: Hi – 1 < Hi > Hi + 1
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++
Java
Python3
C#
Javascript
// C++ program for the above approach #include <bits/stdc++.h>using namespace std;#define INF 1e9 // Function that print minimum hikevoid findMinHike(vector<int> arr,                 int n){     // Insert INF at begin and    // end of array    arr.insert(arr.begin(), INF);    arr.push_back(INF);     // To store hike of each employee    vector<int> hike(n + 2, 0);     // for Type 1 employee    for (int i = 1; i <= n; i++) {         if (arr[i - 1] >= arr[i]            && arr[i] <= arr[i + 1]) {            hike[i] = 1;        }    }     // for Type 2 employee    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 Type 3 employee    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 Type 4 employee    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;        }    }     // Print the min hike for each employee    for (int i = 1; i <= n; i++) {        cout << hike[i] << " ";    }} // Driver Codeint main(){    // Given array of rating of employees    vector<int> arr = { 5, 3, 4, 2, 1, 6 };     // Function Call    findMinHike(arr, arr.size());     return 0;}
// Java program for the// above approachimport java.util.*;class GFG{   static final int INF = 10000009; // Function that print minimum hikestatic void findMinHike(Vector<Integer> arr,                        int n){  // Insert INF at begin and  // end of array  arr.add(0, INF);  arr.add(INF);   // To store hike of  // each employee  int []hike = new int[n + 2];   // for Type 1 employee  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 Type 2 employee  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 Type 3 employee  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 Type 4 employee  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;    }  }   // Print the min hike for each employee  for (int i = 1; i <= n; i++)  {    System.out.print(hike[i] + " ");  }} // Driver Codepublic static void main(String[] args){  // Given array of rating of employees  Vector<Integer> arr = new Vector<>();     arr.add(5);  arr.add(3);  arr.add(4);  arr.add(2);  arr.add(1);  arr.add(6);     // Function Call  findMinHike(arr, arr.size());}} // This code is contributed by Princi Singh
# Python3 program for the above approachINF = 1e9 # Function that print minimum hikedef findMinHike(arr,n):     # Insert INF at begin and    # end of array    arr.insert(0, INF)    arr.append(INF)     # To store hike of each employee    hike = [0] * (n + 2)     # For Type 1 employee    for i in range(1, n + 1):        if (arr[i - 1] >= arr[i] and            arr[i] <= arr[i + 1]):            hike[i] = 1     # For Type 2 employee    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 Type 3 employee    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 Type 4 employee    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                               # Print the min hike for each employee    for i in range(1, n + 1):        print(hike[i], end = " ")         # Driver Codeif __name__ == '__main__':         # Given array of rating of employees    arr = [ 5, 3, 4, 2, 1, 6 ]     # Function Call    findMinHike(arr, len(arr)) # This code is contributed by mohit kumar 29   
// C# program for the// above approachusing System;using System.Collections.Generic;class GFG{   static readonly int INF = 10000009; // Function that print minimum hikestatic void findMinHike(List<int> arr,                        int n){  // Insert INF at begin and  // end of array  arr.Insert(0, INF);  arr.Add(INF);   // To store hike of  // each employee  int []hike = new int[n + 2];   // for Type 1 employee  for (int i = 1; i <= n; i++)  {    if (arr[i - 1] >= arr[i] &&        arr[i] <= arr[i + 1])    {      hike[i] = 1;    }  }   // For Type 2 employee  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 Type 3 employee  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 Type 4 employee  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;    }  }   // Print the min hike for  // each employee  for (int i = 1; i <= n; i++)  {    Console.Write(hike[i] + " ");  }} // Driver Codepublic static void Main(String[] args){  // Given array of rating of employees  List<int> arr = new List<int>();     arr.Add(5);  arr.Add(3);  arr.Add(4);  arr.Add(2);  arr.Add(1);  arr.Add(6);     // Function Call  findMinHike(arr, arr.Count);}} // This code is contributed by gauravrajput1
<script>// Javascript program for the// above approachlet INF = 10000009; // Function that print minimum hikefunction findMinHike(arr, n){     // Insert INF at begin and  // end of array  arr.unshift(INF);  arr.push(INF);    // To store hike of  // each employee  let hike = new Array(n + 2);    // for Type 1 employee  for (let i = 1; i <= n; i++)  {    if (arr[i-1] >= arr[i] &&        arr[i] <= arr[i+1])    {      hike[i] = 1;    }  }    // For Type 2 employee  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 Type 3 employee  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 Type 4 employee  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;    }  }    // Print the min hike for each employee  for (let i = 1; i <= n; i++)  {    document.write(hike[i] + " ");  }} // Driver Code // Given array of rating of employeeslet arr = [5,3,4,2,1,6]; // Function CallfindMinHike(arr, arr.length); // This code is contributed by unknown2108</script>
2 1 3 2 1 2 
Time Complexity: O(N) Auxiliary Space: O(N)
Approach 2: (Using 2 Passes)
Initially assign the smallest possible hike to each employee i.e. 1Now 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.
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++
Java
Python3
C#
Javascript
// C++ program to find the minimum hike of each// employee such that no adjacent employee feels// unfair#include <iostream>using namespace std; void findMinimumHike(int ratings[],int employees){    int hikes[employees];         // As hikes are positive integers, keeping    // minimum value    for(int i = 0; i < employees; i++)    {        hikes[i] = 1;    }         // Pass-1: compare with previous employee    for(int i = 1; i < employees; i++)    {        if (ratings[i - 1] < ratings[i] &&              hikes[i - 1] >= hikes[i])        {            hikes[i] = hikes[i - 1] + 1;        }    }         // Pass-2: compare with Next employee    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;        }    }         // Result    cout << "[";    int i;    for(i = 0; i < employees - 1; i++)    {        cout << hikes[i] << ", ";    }    cout << hikes[i] << "]" << endl;} // Driver Codeint main(){    int data[] = { 5, 3, 4, 2, 1, 6 };         // Function Call    findMinimumHike(data, sizeof(data)/sizeof(data[0]));         int data1[] = { 1, 3, 5, 4 };         // Function Call    findMinimumHike(data1, sizeof(data1)/sizeof(data1[0]));         int data2[] = { 1, 4 };         // Function Call    findMinimumHike(data2, sizeof(data2)/sizeof(data2[0]));    return 0;} // This code is contributed by rag2127
// Java Program to find the minimum hike of each employee// such that no adjacent employee feels unfair import java.io.*;import java.util.*; class GFG {     public static int[] findMinimumHike(int[] ratings,                                        int employees)    {        int[] hikes = new int[employees];         // As hikes are positive integers, keeping minimum        // value        for (int i = 0; i < employees; i++)            hikes[i] = 1;         // Pass-1: compare with previous employee        for (int i = 1; i < employees; i++) {            if (ratings[i - 1] < ratings[i]                && hikes[i - 1] >= hikes[i])                hikes[i] = hikes[i - 1] + 1;        }         // Pass-2: compare with Next employee        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;    }     // Driver Code    public static void main(String[] args)    {         int[] data = new int[] { 5, 3, 4, 2, 1, 6 };         // Function Call        int[] result = findMinimumHike(data, data.length);        // result -> [2, 1, 3, 2, 1, 2]        System.out.println(Arrays.toString(result));         data = new int[] { 1, 3, 5, 4 };         // Function Call        result = findMinimumHike(data, data.length);        // result -> [1, 2, 3, 1]        System.out.println(Arrays.toString(result));         data = new int[] { 1, 4 };         // Function Call        result = findMinimumHike(data, data.length);        // result -> [1, 2]        System.out.println(Arrays.toString(result));    }}
# Python Program to find the minimum hike of each employee# such that no adjacent employee feels unfairdef findMinimumHike(ratings, employees):       # As hikes are positive integers, keeping minimum    # value    hikes = [1 for i in range(employees)]     # Pass-1: compare with previous employee    for i in range(1,employees):        if(ratings[i - 1] < ratings[i] and hikes[i - 1] >= hikes[i]):            hikes[i] = hikes[i - 1] + 1;                 # Pass-2: compare with Next employee    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 # Driver Codedata = [ 5, 3, 4, 2, 1, 6 ] # Function Callresult=findMinimumHike(data, len(data)) # result -> [2, 1, 3, 2, 1, 2]print(result)data = [1, 3, 5, 4 ] # Function Callresult=findMinimumHike(data, len(data)) # result -> [1, 2, 3, 1]print(result)data = [1, 4] # Function Callresult=findMinimumHike(data, len(data)) # result -> [1, 2]print(result) # This code is contributed by avanitrachhadiya2155
// C# program to find the minimum hike// of each employee such that no// adjacent employee feels unfairusing System; class GFG{ public static int[] findMinimumHike(int[] ratings,                                    int employees){    int[] hikes = new int[employees];     // As hikes are positive integers, keeping    // minimum value    for(int i = 0; i < employees; i++)        hikes[i] = 1;     // Pass-1: compare with previous employee    for(int i = 1; i < employees; i++)    {        if (ratings[i - 1] < ratings[i] &&              hikes[i - 1] >= hikes[i])            hikes[i] = hikes[i - 1] + 1;    }     // Pass-2: compare with Next employee    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;} // Driver Codepublic static void Main(String[] args){    int[] data = new int[]{ 5, 3, 4, 2, 1, 6 };     // Function Call    int[] result = findMinimumHike(data, data.Length);         // result -> [2, 1, 3, 2, 1, 2]    Console.WriteLine("[" + String.Join(",", result) + "]");     data = new int[]{ 1, 3, 5, 4 };     // Function Call    result = findMinimumHike(data, data.Length);         // result -> [1, 2, 3, 1]    Console.WriteLine("[" + String.Join(",", result) + "]");     data = new int[]{ 1, 4 };     // Function Call    result = findMinimumHike(data, data.Length);    // result -> [1, 2]    Console.WriteLine("[" + String.Join(",", result) + "]");}} // This code is contributed by Amit Katiyar
<script>// Javascript Program to find the minimum hike of each employee// such that no adjacent employee feels unfairfunction findMinimumHike(ratings, employees){    let hikes = new Array(employees);          // As hikes are positive integers, keeping minimum        // value        for (let i = 0; i < employees; i++)            hikes[i] = 1;          // Pass-1: compare with previous employee        for (let i = 1; i < employees; i++)        {            if (ratings[i - 1] < ratings[i]                && hikes[i - 1] >= hikes[i])                hikes[i] = hikes[i - 1] + 1;        }          // Pass-2: compare with Next employee        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;}  // Driver Codelet data = [5, 3, 4, 2, 1, 6]; // Function Calllet result = findMinimumHike(data, data.length);  // result -> [2, 1, 3, 2, 1, 2]document.write("["+(result).join(", ")+"]<br>"); data = [1, 3, 5, 4 ]; // Function Callresult = findMinimumHike(data, data.length); // result -> [1, 2, 3, 1]document.write("["+(result).join(", ")+"]<br>");data = [1, 4 ]; // Function Callresult = findMinimumHike(data, data.length); // result -> [1, 2]document.write("["+(result).join(", ")+"]<br>");  // This code is contributed by patel2127</script>
[2, 1, 3, 2, 1, 2]
[1, 2, 3, 1]
[1, 2]
Time Complexity: O(N) Auxiliary Space: O(N)
mohit kumar 29
aashkakapadia
nidhi_biet
work-cookie
amit143katiyar
princi singh
GauravRajput1
avanitrachhadiya2155
rag2127
unknown2108
patel2127
arorakashish0911
Amazon
Arrays
Dynamic Programming
Greedy
Amazon
Arrays
Dynamic Programming
Greedy
Writing code in comment?
Please use ide.geeksforgeeks.org,
generate link and share the link here.
Maximum and minimum of an array using minimum number of comparisons
Top 50 Array Coding Problems for Interviews
Multidimensional Arrays in Java
Stack Data Structure (Introduction and Program)
Linear Search
Program for Fibonacci numbers
0-1 Knapsack Problem | DP-10
Longest Common Subsequence | DP-4
Subset Sum Problem | DP-25
Longest Palindromic Substring | Set 1