Infosys Interview Question
Software Engineer / Developersstart ad the end of the array and set current = a[n-1]
iterate backwards
while a[i] > current : decrement
if(a[i]< current : current = a[i]
Dont tell me infosys asks such questions.. it is a dumb company and if you get there, you ll be working with a bunch of technically incompetent people
n is the number of elemnets.
c=a[n-1];
i=n-2;
while(i!=-1){
if(a[i]>c)
{
a[i]=c;
}
if(a[i]<c)
{
c=a[i];
}
i--;
}
Either the question is wrongly asked or formatted because if the only operation can be decrement then no logical operations can be done as well. I guess the intention was without swap operation what would be the ideal way to sort it, which is quite straightforward.
Just retrieve the last element from the list and go on reducing all numbers until it equals to that. If at any moment of time the number is lower than the limit to which it has to reduced, then the new limit is set as the present number. Iterate through the loop solution in O(n).
1) break array in decreasing order sets
- ragu-ram July 27, 20102) find minimum in each set.
{5,4,2} {7,9,6}