I'm doing a Hackerrank challenge to compute the maximum and minimum sum of a set of 5 numbers. For example :
Sample Input
1 2 3 4 5
Sample Output
10 14
Ouput 10 = minimum sum (exclude the biggest value), and 14 = maximum sum (exclude the smallest value). The constraint for each array input is
1 <= a[i] <= 10^9
, it specifically instructs us to use
uint64_t
for the sum output.
Here's the original website of the task. My code works on my local machine (Ubuntu 22.04), but I encountered SIGSEGV segmentation error during submission on both 2 input cases :
1 2 3 4 5
7 69 2 221 8974
Here's the error :
Program terminated with signal SIGSEGV, Segmentation fault.
#0 0x00000000004023fd in compute (array_members=...) at Solution.cpp:45
45 unsigned long array_smallest = array_members[0];
What I have tried:
Here's my code :
#include <iostream>
#include <string>
#include <vector>
#include <sstream>
using namespace std;
vector<unsigned long> array_members;
void input(){
int array_size = 0;
const unsigned long max_value = 1000000001;
do {
cin >> array_size;
} while (array_size < 0|| array_size > 6);
cin.ignore(256, '\n');
string input;
int data = 0;
getline(cin, input);
istringstream iss(input);
while (iss >> data){
if (data > 0 && data < max_value){
array_members.push_back(data);
} else cerr << "Data out of bounds" << endl;
}
}
void compute (vector<unsigned long> &array_members){
unsigned long array_smallest = array_members[0];
unsigned long array_biggest = array_members[0];
uint64_t sum = 0;
for (int i =0; i < array_members.size(); i++){
sum += array_members[i];
if (array_members[i] > array_biggest)
array_biggest = array_members[i];
if (array_members[i] < array_smallest)
array_smallest = array_members[i];
}
cout << sum-array_biggest << " " << sum-array_smallest << endl;
}
int main(){
input();
compute(array_members);
return 0;
}
This code works in my machine. What I have tried :
1. I have checked that all my variables are initialized.
2. The maximum constraint is 10^9, so I checked with GDB using 10^9 as input :
1000000000 1000000000 1000000000 1000000000 1000000000
4000000000 4000000000
Worked fine, GDB reported program exited normally. Maybe the server has a different range ? The task specifically mentioned to use
uint64_t
.
I'm not sure what else to check, any advice ?
[Additional]
Full error report :
- Sample test case 0
Compiler Message
Segmentation Fault
Error (stderr)
Reading symbols from Solution...done.
[New LWP 935423]
Core was generated by './Solution'.
Program terminated with signal SIGSEGV, Segmentation fault.
#0 0x00000000004023fb in compute (array_members=...) at Solution.cpp:44
44 unsigned long array_smallest = array_members[0];
Input (stdin)
1 2 3 4 5
Your Output (stdout)
~ no response on stdout ~
Expected Output
10 14
- Sample test case 1
Compiler Message
Segmentation Fault
Error (stderr)
Reading symbols from Solution...done.
[New LWP 2338644]
Core was generated by './Solution'.
Program terminated with signal SIGSEGV, Segmentation fault.
#0 0x00000000004023fb in compute (array_members=...) at Solution.cpp:44
44 unsigned long array_smallest = array_members[0];
Input (stdin)
7 69 2 221 8974
Your Output (stdout)
~ no response on stdout ~
Expected Output
299 9271