2

this problem

I normally erase the default code and start solving the whole thing myself. However, hackerrank test cases show a different output than the one that appears to me whenever i run against custom input. Why does it show different number?

Keep in mind I have tried different input sizes as float, long long int, int, and double;

#include <bits/stdc++.h>

using namespace std;

int main() {
    long long int arr[5], neglect = 0, min = 9999999999999, max = 0, curr = 0; 
    for (int i = 0; i < 5; i++) {
        cin >> arr[i];
    }

    while (neglect < 5) {
        for (int i = 0; i < 5; i++) {
            if (i == neglect) {i++;}
            curr += arr[i];
        }
        if (curr < min) {min = curr;}
        if (curr > max) {max = curr;}
        //cout << curr << endl;
        //cout << curr << " " << min << " " << max << endl;
        curr = 0;
        neglect++;
    }
    cout << min << " " << max;
}

All i wanted was to solve it in a way that tries all combinations then shows the least and maximum values. I know my code isn't the optimal way to solve it, I know i could just sort the array and exclude first and last elements, but please bear with me.

input: 1 2 3 4 5 claimed output: 11 4198826

but when i debugged it with the same custom input and the commented lines it showed:

14 14 14
13 13 14
12 12 14
11 11 14
10 10 14
10 14

Doesn't that mean that my code should be working?

Abulkhair
  • 23
  • 5
  • 5
    `#include using namespace std;` - Don't ever do that. See [Why should I not #include ?](https://stackoverflow.com/q/31816095/5910058) and [Why is "using namespace std;" considered bad practice?](https://stackoverflow.com/q/1452721/5910058). – Jesper Juhl Jul 11 '20 at 18:54
  • @JesperJuhl Thank you for informing me of that. I had no idea that my coding was that bad. However, I'll try my best to reflect and improve. How do I upvote your comment though? because I googled it and apparently there should be a small triangle but I can't find that – Abulkhair Jul 11 '20 at 19:10
  • 1
    Once you gain more reputation, you'll be able to upvote comments. In any case, don't worry about it, I just wanted to point out some obvious red flags. – Jesper Juhl Jul 11 '20 at 19:18

1 Answers1

2

When you are neglecting the last element you are doing an i++ and this will add the arr[5] to the curr value, but arr[5] is out of bound of the array, thus it can give segfault or unexpected result.

I would suggest that you should not increment the i in the for loop, instead you can use continue when you want to neglect the element.

        for (int i = 0; i < 5; i++) {
            if (i == neglect) {
                continue;
            }
            curr += arr[i];
        }

Also, you can replace the outer for loop with while loop so that you don't have to increment the neglect manually.

Deepak Patankar
  • 2,175
  • 2
  • 11
  • 24
  • 1
    `continue` on it's own won't advance `neglect`. You should probably recommend that they change the while loop to a for loop advancing `neglect` as well. – Bill Lynch Jul 11 '20 at 19:21
  • Thank you very much. The thought hadn't crossed my mind since i thought it'd automatically exit the loop when "i" would equal 5. But apparently I need to study and code more. why did my code show me correct output when i debugged it though? – Abulkhair Jul 11 '20 at 19:21
  • 1
    If you are accessing the array element out of bound, then the code's behaviour is undefined, it may work or can fail sometime. You can refer to this [link](https://stackoverflow.com/questions/1239938/accessing-an-array-out-of-bounds-gives-no-error-why#:~:text=C%20or%20C%2B%2B%20will%20not%20check%20the%20bounds%20of%20an%20array%20access.&text=Indexing%20the%20array%20via%20array,will%20result%20in%20undefined%20behavior.) for a detailed explanation – Deepak Patankar Jul 11 '20 at 19:34