Skip to content
Related Articles

Related Articles

Minimum salary hike for each employee such that no employee feels unfair
  • Difficulty Level : Medium
  • Last Updated : 19 Jan, 2021
GeeksforGeeks - Summer Carnival Banner

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 the 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;
}

Java




// Java program for the
// above approach
import java.util.*;
class GFG{
   
static final int INF = 10000009;
 
// Function that print minimum hike
static 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 Code
public 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




# 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   

C#




// C# program for the
// above approach
using System;
using System.Collections.Generic;
class GFG{
   
static readonly int INF = 10000009;
 
// Function that print minimum hike
static 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 Code
public 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
Output
2 1 3 2 1 2 

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

Approach 2: (Using 2 Passes)

  1. Initially assign the smallest possible hike to each employee i.e. 1
  2. Now we can’t decrease the current employee hike based on the hike of adjacent employees, always looks for increasing cases.
  3. Pass1 (from left -> right) compare with previous employee hike and increase the current employee hike if required.
  4. 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++




// 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 Code
int 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




// 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));
    }
}

Python3




# Python Program to find the minimum hike of each employee
# such that no adjacent employee feels unfair
def 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 Code
data = [ 5, 3, 4, 2, 1, 6 ]
 
# Function Call
result=findMinimumHike(data, len(data))
 
# result -> [2, 1, 3, 2, 1, 2]
print(result)
data = [1, 3, 5, 4 ]
 
# Function Call
result=findMinimumHike(data, len(data))
 
# result -> [1, 2, 3, 1]
print(result)
data = [1, 4]
 
# Function Call
result=findMinimumHike(data, len(data))
 
# result -> [1, 2]
print(result)
 
# This code is contributed by avanitrachhadiya2155

C#




// C# program to find the minimum hike
// of each employee such that no
// adjacent employee feels unfair
using 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 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]
    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
Output
[2, 1, 3, 2, 1, 2]
[1, 2, 3, 1]
[1, 2]

Time Complexity: O(N) 
Auxiliary 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
Recommended Articles
Page :