Pair of roses Coding problem


I am getting correct answer for the sample input as well as some of the inputs I tried.
It is giving wrong answer on submission and I’m unable to figure out what is the problem in it. Please help me in the error

@mvermav19 your code seems fine but this for loop is not required
if (price[i] + price[j] == balance){
for (int a = 0; a < N; a++){
int min = INT_MIN;
if ((price[j] - price[i]) > min){
mini = i;
minj = j;
}
i++;
j–;}

instead you should do this
long long diff=1000000000;
while(i<j)
if(price[i] + price[j] == balance){
if(diff >= arr[j] - arr[i]){
mini=i;
minj=j;
diff=arr[j]-arr[I];
}
i++;
j–;
}
This edit should do the work


sir, i had done all those changes suggested by you. But it’s still not working.

@mvermav19 there are two more edits

  1. there is a fullstop in line Deepak should buy roses whose prices are 40 and 40**.** notice this full stop after the statement but your code miss this full stop.
    code should be
    cout << “Deepak should buy roses whose prices are " << price[mini] << " and " << price[minj] <<”."<< endl;

  2. value of diff assigned by you is little less in line int diff = 10000;
    make it long long diff=1000000000;

okay sir, got it.
It’s working completely fine now.

1 Like

great @mvermav19 please rate your doubt resolving experience on http://d.cb.lk/t/163159 thread :wink: happy coding !!

I hope I’ve cleared your doubt. I ask you to please rate your experience here
Your feedback is very important. It helps us improve our platform and hence provide you
the learning experience you deserve.

On the off chance, you still have some questions or not find the answers satisfactory, you may reopen
the doubt.