Amdocs Interview Question
Developer Program EngineersCountry: United States
Interview Type: Written Test
Here is the algo
1. Find average for whole array
2. Now start with first element and parse through array. Find average so far stop if equal.
3. return the index;
Code Below:
int findEqualAverage(int[] A){
int sum = 0;
for(int i=0;i<A.length;i++){
sum += A[i];
}
float average = (float)sum/(float)A.length;
sum = 0;
for(int i=0;i<A.length;i++){
sum+=A[i];
float averageSofar = ((float)sum)/(i+1);
if(averageSofar == average)
return (i+1);
}
}
Isn't this an np hard problem?
- Nick December 05, 2013