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 = 6Input: 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++
// 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
// 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
# 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#
// 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 |
Javascript
<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. 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++
// 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
// 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 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#
// 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 |
Javascript
<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)
Ready to dive in? Explore our Free Demo Content and join our DSA course, trusted by over 100,000 neveropen!
