Sort game question on hackerblocks

this is my code for the question :


this is not passing 2 test cases, may i know what is wrong in my code?

Hey @alankrit.agr99

It’s given in the question that the answer list contains only names of those employees having salary greater than or equal to a given number x.
Instead of writing:
int i=0 ;
while(arr[i].second>=x){
cout<<arr[i].first<<" "<<arr[i].second<<endl ;
i++ ;
}

You need to write:
for(int i = 0; i < n; i++){
if(arr[i].second>=x){
cout<<arr[i].first<<" "<<arr[i].second<<endl ;
}
}

Coz while writing by your way, whenever there comes a number for which arr[i].second < x, the while loop stops and even if the further numbers in the sequence have arr[i].second >= x, they won’t be printed. So you ans turns out to be wrong. Try out the edit suggested by me.

1 Like

but i have written that because i have already sorted the array in decreasing order of the salary so after i reach x, there will be no entry in the array that has a salary greater than x.

@alankrit.agr99
Yeah, you’re right,
So you just need to make a little edit in your loop , just add i < n in the condition so that it i doesn’t exceed out of the array.
int i=0 ;
while(arr[i].second>=x && i < n){
cout<<arr[i].first<<" "<<arr[i].second<<endl ;
i++ ;
}

1 Like