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 2
Explanation:
The distribution of minimum hike for each employee must be:
1 + 2 + 3 + 2 = 7
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 implementation of the above approach:
C++
// 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; } |
2 1 3 2 1 2
Time Complexity: O(N)
Auxillary Space: O(N)
Got what you were looking for? Learn more and become self sufficient. Start learning Data Sructures & Algorithms with the help of the most trusted DSA Self Paced course, and that too at the most student-friendly price.
Recommended Posts:
- Hike Interview Experience | Set 5
- Maximum and minimum of an array using minimum number of comparisons
- Remove minimum numbers from the array to get minimum OR value
- Minimum sum by choosing minimum of pairs from array
- Flip minimum signs of array elements to get minimum sum of positive elements possible
- Minimum LCM and GCD possible among all possible sub-arrays
- Minimum Possible value of |ai + aj - k| for given array and k.
- Minimum possible sum of array B such that AiBi = AjBj for all 1 ≤ i < j ≤ N
- Minimum value of "max + min" in a subarray
- Transform N to Minimum possible value
- Minimum XOR Value Pair
- Minimum sum of multiplications of n numbers
- Sum of minimum elements of all subarrays
- Find the minimum value of X for an expression
- Minimum Sum Path in a Triangle
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.

