Break statement affecting Code for CNOT

#include <iostream>
#include <stdbool.h>
using namespace std;

int main() {
    int t;
    cin >> t;
    while(t--){
        int x,y,k,n;
        cin >> x >> y >> k >> n;
        int left = x-y;
        bool buy=false;
        int page, prc;
        for(int i=0;i<n;i++){
            cin >> page >> prc;
            if(page >= left && prc <= k ){
                buy = true;
                // break;
            }
        }
        if(buy==true) cout << "LuckyChef\n";
        else cout << "UnluckyChef\n";
    }
	return 0;
}

For the Problem CNOT(Chef and Notebooks), this code passes all the test but when I uncomment the break statement it gives wrong answer, but why?, please help on this.
Thank you

@achal_t
plzz send the link to the problem

this is the problem link

Hey! I’ve noticed your impressive 4-star ranking on CodeChef. Could you share the resources or courses you used for DSA? I’m eager to learn and your guidance would mean a lot. Thanks a bunch!

@achal_t In the for loop, you’re having a line

cin >> page >> prc;

This line takes the number of page, cost of notebook as input.
If you break this loop then the code will no longer look into the details of the rest of the notebooks.

While reading the inputs for the next testcases, you’re program will read the remaining details of the previous notebooks (because they were left out). This will result in taking wrong values as input, and hence wrong answer.

1 Like