Example input gives correct output; submission says wrong answer

Hi, I am attempting a practice contest. Am I allowed to discuss beginner practice contests here?

Anyways, here is my solution: Solution

I have no idea what case could be causing this to fail; every case I attempt seems to work perfectly.

Please help!

Thanks

If you can view other people’s solutions, it’s fine to ask :slight_smile:

Your code has an out-of-bounds access with the following test input:

1
2 2
1 2
[simon@simon-laptop][18:18:46]
[~/devel/hackerrank/otherpeoples]>./compile-latest-cpp.sh 
Compiling mattjr747-QUALPREL.cpp
Executing command:
  g++ -std=c++17 mattjr747-QUALPREL.cpp -O3 -g3 -Wall -Wextra -Wconversion -DONLINE_JUDGE -D_GLIBCXX_DEBUG    -fsanitize=undefined -ftrapv
Successful
[simon@simon-laptop][18:19:11]
[~/devel/hackerrank/otherpeoples]>echo "1
2 2
1 2" | ./a.out
/usr/include/c++/9/debug/vector:427:
In function:
    std::__debug::vector<_Tp, _Allocator>::reference 
    std::__debug::vector<_Tp, 
    _Allocator>::operator[](std::__debug::vector<_Tp, 
    _Allocator>::size_type) [with _Tp = int; _Allocator = 
    std::allocator<int>; std::__debug::vector<_Tp, _Allocator>::reference = 
    int&; std::__debug::vector<_Tp, _Allocator>::size_type = long unsigned 
    int]

Error: attempt to subscript container with out-of-bounds index 2, but 
container only holds 2 elements.

Objects involved in the operation:
    sequence "this" @ 0x0x7ffc4b5b37f0 {
      type = std::__debug::vector<int, std::allocator<int> >;
    }
Aborted (core dumped)
1 Like

Ah, makes sense. Thank you so much for the quick and helpful reply!

1 Like

I actually have on more question. I can see the issue in my code. But I tried this test case in the codechef website’s IDE (with my original code), and it still runs fine and gives me the correct answer.

Any ideas why it isn’t seeing the error?

[EDIT] It seems that my vector is giving me a value of 0 even when I give it an index out of range. Which makes me think vector handles out of range index’s? But if that is the case, I don’t see where I would have gotten the error originally.

Anyways, I submitted with my fix and got the correct answer. Just don’t understand why I don’t get the run time error you did (with my original code).

An out-of-bounds access with std::vector::operator[] is just Undefined Behaviour by default, so anything can happen. (Contrast with vector::at, which will throw an exception).

The _GLIBCXX_DEBUG flag enables the gcc C++ Standard Library Debug mode and will assert on out-of- bounds access via std::vector::operator[].

1 Like

Makes sense. Wow I definitely need to use my own IDE. Thanks again.

1 Like