# Help me in solving COURSEREG problem

### My issue

i can’t understand how this code is executed to give this random output , and can’t get why it isn’t working as it should aussi

### My code

``````#include <iostream>
using namespace std;

int main() {
int t;
cin>>t;
int arr;
bool flag=0;
for(int i=0 ; i<t ; ++i){
cin>>arr[i];
int diff = arr-arr;
if(diff>=arr){
cout<<"Yes"<<endl;}
else{cout<<"No"<<endl;}
}

}

``````

Problem Link: COURSEREG Problem - CodeChef

There are few things causing problems in your code.

1. Remember that the variable i simply keeps track of the test-case number and has nothing to do with the values in arr. Therefore you shouldn’t use arr[i] to store the input. This can cause real (not just wrong output) problems if t \gt 3 since i will be defined for 0 \le i \lt t, but arr[i] will not be defined past 3.
2. Each test-case, three values are given, namely n, m, k. However, your code only takes in one number.

Here is a working solution based on yours (since the variable `flag` is not required, I have omitted it):

``````#include <iostream>
using namespace std;

int main() {
int t; cin >> t;
int arr;
for (int i = 0; i < t; ++i) {
cin >> arr >> arr >> arr; // n, m, k respectively
int diff = arr - arr;
if (diff >= arr) {
cout << "YES" << '\n';
} else {
cout << "NO" << '\n';
}
}
}
``````
1 Like

cool
but i can’t distinguish when arr[i] into the loop is supposed to store values of array input and when it should not can you please explain it more?

I don’t understand your question quite well, so I’ll try my best to answer.

The problem with your code is the one line: `cin >> arr[i]`.

What is the purpose of arr? To store the values of n, m, k. Hence you initialized arr with a capacity of 3 elements namely arr, arr, arr. arr can have a maximum of 3 elements.

What is the purpose of the for loop? To keep track of the test-cases. Basically, a test-case is a sub-problem inside the problem. i is the number of the test-case you are currently on. The problem says that 1 \le t \le 1000, which means i is in the same range. In the problematic code, you say arr[i]. But arr is defined only up till arr. That means if there are say 5 test-cases, then when i > 2 (let’s take an example of 3), arr[i] or arr isn’t defined since that would check for the 4th element of arr, which simply does not exist. Remember as we defined, arr cannot have more than 3 elements.

The second reason that line is wrong: Each test-case gives a new value for n, m, k. Therefore, you should take 3 inputs each test-case. But here you are only doing it once. So the first test-case i = 0, you put the input into arr. But that’s it. You aren’t taking input of m, k. But since you initialized the array, arr and arr are 0, so the program technically works. When i = 1 (next test-case), you set arr as the 2nd test-case’s n, but the value of arr is the same as n from the previous test-case. But that’s wrong since that old n is irrelevant and you still aren’t taking the rest of the input. The moment i > 2, arr[i] isn’t defined and your code falls to the problem outlined above.

Hence its best to keep these things in mind:

1. Never uses the iterator for test-cases in the problem.
2. Make sure you are taking the complete input for each test-case. Your code failed this since its not updating values based on new input e.g. the 1st test-case’s n is still being used in the 2nd test-case.
3. Adding on to point 2, data from the previous test-case shouldn’t be used in subsequent test-cases.
4. Instead of using an array to store values, which is fine, try using specific variables. That way it may make you see that you aren’t updating variables.
5. I prefer the following method for keeping track of test-cases instead of a for loop:
``````#include <bits/stdc++.h>
using namespace std;

int main() {
int t; cin >> t;
while (t--) { // This basically says while t > 0, keep iterating (-- is the decrement operator)
// the while (t--) is fulfilling the same purpose as your for loop.
// do whatever
}
}
``````

Points 4 and 5 are preferential but they may make you more diligent in making sure your code is correct.

Let me know if this helps, I can try to explain again. 1 Like

Another thing I’d suggest is using a debugger. If you can try this:

Create a problem input where there are more than 3 test-cases. Then look at what happens each step with respect to the values in arr. This will show you that:

1. Once the test-case number i is over 2, you’ll have an error displayed.
2. You’ll see that previous test-case inputs are still being stored even after you’ve moved onto the next case.

this is very helping tbh i’ve searched before and left at loss
i wonder if you can provide mentorship through working with arrays stuff aka: hints for problems ? cuz i’m really stuck and confused

mb we can connect on discord if you agreed

Feel free to ask your question on this website. I’ll be able to see and reply to them and other users can also give you feedback (which can definitely be better than my answers). If you want, you can mark my answer as solved (if it helped you) although I’m not sure how this works. Thanks!

1 Like