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[0], arr[1], arr[2]. 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[2]. That means if there are say 5 test-cases, then when i > 2 (let’s take an example of 3), arr[i] or arr[3] 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[0]. But that’s it. You aren’t taking input of m, k. But since you initialized the array, arr[1] and arr[2] are 0, so the program technically works. When i = 1 (next test-case), you set arr[1] as the 2nd test-case’s n, but the value of arr[0] 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:
- Never uses the iterator for test-cases in the problem.
- 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.
- Adding on to point 2, data from the previous test-case shouldn’t be used in subsequent test-cases.
- 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.
- 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. 