0

I was solving this problem from Codeforces. Link

I'm using Sublime Text 3 as my code editor and Mingw as my compiler. When I used my Mingw to compile and run the code it gives my wrong answer whereas when I'm using https://ideone.com/ my answer is different and is correct.

MY CODE

using namespace std;

int main() {
    ios_base::sync_with_stdio(0);
    cin.tie(NULL);

    int arr[4];
    int ans = 0;
    for (int i = 0; i < 4; ++i)
    {
        cin >> arr[i];
    }

    for (int i = 0; i < 4; ++i)
    {
        if (arr[i] == arr[i + 1] || arr[i] == arr[i + 2] || arr[i] == arr[i + 3]) {
            ans++;
        }

    }

    cout << ans;


}
Test-Case:1 
INPUT:
4 4 4 4

MINGW OUTPUT:
4
IDEONE OUTPUT:
3
Test-Case:2
INPUT:
1 7 3 3

MINGW OUTPUT:
2
IDEONE OUTPUT:
1

Vlad from Moscow
  • 301,070
  • 26
  • 186
  • 335
Soumyajoy Das
  • 33
  • 1
  • 10
  • 3
    You have out-of-bounds accesses in the `for` loop. Change this: `int a[4];` to this: `std::array a;` and then this: `if (arr[i] == arr[i + 1] || arr[i] == arr[i + 2] || arr[i] == arr[i + 3])` to this: `if (arr.at(i) == arr.at(i + 1) || arr.at(i) == arr.at(i + 2) || arr.at(i) == arr.at(i + 3))`. Then you will see the issue, regardless of what compiler you use. – PaulMcKenzie Dec 06 '21 at 13:54
  • 5
    What do you think `arr[i + 1]` should be, when `i == 3`? – Lukas-T Dec 06 '21 at 13:55
  • @PaulMcKenzie I got this error when I replaced my code with yours. ```terminate called after throwing an instance of 'std::out_of_range' what(): array::at: __n (which is 4) >= _Nm (which is 4)``` – Soumyajoy Das Dec 06 '21 at 14:04
  • @SoumyajoyDas - That error is expected, and what we've pointed out what the problem is. The `at()` function automatically checks array boundaries, while your original code using dumb arrays does not. – PaulMcKenzie Dec 06 '21 at 14:06
  • @PaulMcKenzie But I didn't get any output. Can you please confirm whether my method of Input will work or not? Cause the array you looked like a vector? So, should I input the elements in the array with push_back or not? – Soumyajoy Das Dec 06 '21 at 14:08
  • 1
    @SoumyajoyDas *But I didn't get any output* -- Your code stopped because you are going out-of-bounds, which is what the `at()` call does. There is no other change needed -- you need to fix the logic in your code so that you are not going out-of-bounds. It has absolutely nothing to do with using vector or not. Again, look at the first two comments -- your code logic is totally wrong. – PaulMcKenzie Dec 06 '21 at 14:15

1 Answers1

2

In this for loop

for (int i = 0; i < 4; ++i)
{
    if (arr[i] == arr[i + 1] || arr[i] == arr[i + 2] || arr[i] == arr[i + 3]) 
    {
        ans++;
    }

}

there are numerous accesses outside the array bounds. So the program has undefined behavior.

To solve the problem you should select an appropriate container to store inputted values.

The task can be resolved simpler if to use the standard container std::set.

Here is a demonstration program.

#include <iostream>
#include <set>

int main() 
{
    const size_t N = 4;
    
    std::set<unsigned int> set;
    
    for ( size_t i = 0; i < N; i++ )
    {
        unsigned int value;
        std::cin >> value;
        
        set.insert( value );
    }
    
    std::cout << N - set.size() << '\n';
    
    return 0;
}

For example if to enter the following sequence of values

1 7 3 3

then the program output will be

1

For this sequence of values

4 4 4 4

the output will be

3

If to use an array then the program can look the following way.

#include <iostream>

int main() 
{
    const size_t N = 4;
    unsigned int a[N];
    
    for ( size_t i = 0; i < N; i++ )
    {
        std::cin >> a[i];
    }
    
    size_t ans = 0;
    
    for ( size_t i = 1; i < N; i++)
    {
        size_t j = 0;
        
        while ( j != i && a[j] != a[i] ) ++j;
        
        if ( j != i ) ++ans;
    }
    
    std::cout << ans << '\n';
    
    return 0;
}
Vlad from Moscow
  • 301,070
  • 26
  • 186
  • 335