Pooja Basia
BAN USERimport java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.List;
public class MinimizeEmpHikes {
public static void main(String[] args) {
try{
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
System.out.println("enter minimum hike:");
int x = Integer.parseInt(br.readLine()); //value of x minimum hike
System.out.println("enter no of employees:");
int n = Integer.parseInt(br.readLine());
int i,minR,count = 0;
List<Integer> min1 = new ArrayList<Integer>();
int []ratingArr = new int[n];
int []solutionArr = new int[n];
for(i=0; i<n ; i++)
{
System.out.println("enter rating of "+ (i+1) + " employee");
ratingArr[i] = Integer.parseInt(br.readLine());
}
minR = ratingArr[0];
for(i=0; i<n; i++)
{
solutionArr[i] = 0;
}
//find 1st min // todo: optimize module to find next min
for(i=0;i<n;i++)
{
if(ratingArr[i] <= minR)
{
/*if(min1.contains(ratingArr[i]))
continue;
else*/
minR = ratingArr[i];
}
}
// assigning rating.. find ith minimum element/rating..check next n next->next elements rating n hikes then assign hike
while(count < n)
{
min1.add(minR); //array of min / rating in ascending order*/
for(i=0;i<n;i++)
if(ratingArr[i] == minR) //next min element
{
//assign hike
if(min1.size() == 1)
solutionArr[i] = x;
else
{
if(i-2 >= 0)
{
if(ratingArr[i] == ratingArr[i-2])
solutionArr[i] = solutionArr[i-2]; // in this case i-2 has already been assigned a value.
else
if(i+2 < n)
solutionArr[i] = Math.max(Math.max(solutionArr[i-1],solutionArr[i-2]), Math.max(solutionArr[i+1], solutionArr[i+2])) + x;
else
if(i+1 < n)
solutionArr[i] = Math.max(Math.max(solutionArr[i-1],solutionArr[i-2]), solutionArr[i+1]) + x;
else
solutionArr[i] = Math.max(solutionArr[i-1],solutionArr[i-2]) + x;
}
else if(i-1 >=0)
{
if(i+2 < n)
solutionArr[i] = Math.max(solutionArr[i-1], Math.max(solutionArr[i+1], solutionArr[i+2])) + x;
else
if(i+1 < n)
solutionArr[i] = Math.max(solutionArr[i-1], solutionArr[i+1]) + x;
else
solutionArr[i] = solutionArr[i-1] + x;
}
else
{
if(i+2 < n)
solutionArr[i] = Math.max(solutionArr[i+1], solutionArr[i+2]) + x;
else
if(i+1 < n)
solutionArr[i] = solutionArr[i+1] + x;
else
solutionArr[i] = x;
}
}
count++;
}
minR = minR+1;
}
System.out.println("Rating\tHike");
for(i=0;i<n;i++)
System.out.println(ratingArr[i] +"\t" +solutionArr[i]);
}
catch(IOException e)
{
e.printStackTrace();
}
}
}
It is given that no two adjacent employee would have common rating. Excluding this case, I agree with the fact that two relative minimum rated employee can sit next to each other but they should not be given same hike.
- Pooja Basia August 25, 2013Let me know any such case in which code (I wrote) would fail.