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

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:

  1. Type 1: Hi – 1 > Hi < Hi + 1
  2. Type 2: Hi – 1 < Hi < Hi + 1
  3. Type 3: Hi – 1 > Hi > Hi + 1
  4. 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 implementation of the above approach:

C++

filter_none

edit
close

play_arrow

link
brightness_4
code

// C++ program for the above approach
  
#include <bits/stdc++.h>
using namespace std;
#define INF 1e9
  
// Function that print minimum hike
void 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 Code
int main()
{
    // Given array of rating of employees
    vector<int> arr = { 5, 3, 4, 2, 1, 6 };
  
    // Function Call
    findMinHike(arr, arr.size());
  
    return 0;
}

chevron_right


Python3

filter_none

edit
close

play_arrow

link
brightness_4
code

# Python3 program for the above approach
INF = 1e9
  
# Function that prminimum hike
def 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 Code
if __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    

chevron_right


Output:

2 1 3 2 1 2

Time Complexity: O(N)
Auxillary Space: O(N)

Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the DSA Self Paced Course at a student-friendly price and become industry ready.




My Personal Notes arrow_drop_up

Check out this Author's contributed articles.

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.



Improved By : mohit kumar 29, This is it