Click here to Skip to main content
15,867,879 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
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;
    }

    /** PRINT ARRAY
    for (int i =0;  i < array_size; i++){
        cout << array_members[i];
    } **/
}

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
Posted
Updated 13-Dec-22 22:13pm
v4
Comments
jeron1 7-Dec-22 12:06pm    
You don't need an array size variable for use in the compute() method, you have a vector, use it's size() method.
lock&_lock 7-Dec-22 12:20pm    
Thanks for suggestion, I have modified my code and put both array_size and max_value as local variables in the function input()
jeron1 7-Dec-22 12:44pm    
Before accessing elements of the vector using [] brackets, make sure that whatever is within those brackets is valid. I'm talking about the first couple line of the compute() method.
Richard MacCutchan 7-Dec-22 12:37pm    
The error message does not match the source code. The source you have posted here has the following at line 44:
        if (array_members[i] < array_smallest) 

And in theoriginal message it says line 45 which is
            array_smallest = array_members[i];

Neither of which, match the error messages. So I can only assume that the source code for the test on Hackerrank is different.
lock&_lock 7-Dec-22 12:48pm    
@Richard As for line 44/45, I have modified the code following @jeron1 suggestion to use size(). I also want to update that @k5054 suggestion was on point, the "array_size" input was unecessary for the submission, and somehow it reported back as segmentation error. It was my mistake, thank you very much for your time & assistance. I will immediately excuse myself and disappear out of embarrasment.

C++
while (array_size < 0 && array_size > 6);

The value of array_size cannot be less than zero, and greater than six, at the same time. It should be either one or the other, thus:
C++
while (array_size < 0 || array_size > 6); // less than zero OR greater than six.


Other than that it is difficult to see why that would SEGV at line 45. It may well depend on the value of the inputs to the program.
 
Share this answer
 
v2
Comments
lock&_lock 7-Dec-22 11:56am    
@Richard thank you for pointing it out, I totally missed it ! I have fixed it and I still have the segmentation error. My code failed exactly at 2 input cases when I run it on the website : 1 2 3 4 5 and 7 69 2 221 8974. I don't think these 2 inputs should be problematic, but I'm stuck.
Richard MacCutchan 7-Dec-22 12:11pm    
I have tried a number of tests with many different values and cannot get it to fail. Can you show us the inputs you are using, and the error message, if different from above?
lock&_lock 7-Dec-22 12:34pm    
@Richard I have updated my question with additional report on the error, that's all of the error information Hackerrank provides. All inputs are only by the website, the website will run the submission against some inputs. User input is not possible.
Richard MacCutchan 7-Dec-22 12:47pm    
Well I have just run the test with their inputs, and it gives the correct answer. But, as I mentioned above, neither of the error message match the code that you posted. So something is wrong somewhere.
I'm with Richard, in that I see no reason for a SEGV at line 45. However, the problem statement says that the input is one line consisting of 5 integers. Your program, as presented here, seems to expect 2 lines, the first being the size of the array and then the second being the data. This seems to be corroborated by the given sample, although the sample produces no output. I'm not familiar with HackerRank, but in my experience these sites simply run your code and report success, failure, or a time limit violation. I've never seen a runtime error reported, nor any clue as to the input data used, so I don't know how you know what the test case your program is failing on. But maybe HackerRank provides that for you on a failure?
In any case, I'd start by removing the code that reads in the array size, and resubmit.
 
Share this answer
 
Comments
Richard MacCutchan 7-Dec-22 12:48pm    
I did as you suggested and still cannot get it to fail. I suspect the code posted here is not what is being run on HackerRank.
lock&_lock 7-Dec-22 12:50pm    
@k5054 Your suggestion is on point, this was my mistake that got me stuck for 2 days before posting it here. It got submitted just fine now. I will immediately excuse myself and disappear out of embarrasment, thank you.
k5054 7-Dec-22 12:52pm    
If I had a $ for every time I'd done something similar ...
lock&_lock 7-Dec-22 12:55pm    
@Richard It got submitted fine just now. I just removed the array size input part. I would never mess with posting different things and waste people's time. Again, I apologize.
your cin has something wrong.first one ,you "cin" just work once but you want to read size times you gived.you should check your logical about your code.
below is just some bad-code ,but can be running.

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;
  for (int i = 0; i < array_size; i++) {
    getline(cin, input);
    istringstream iss(input);
    iss >> data;
    if (data > 0 && data < max_value) {
      array_members.push_back(data);
    } else
      cerr << "Data out of bounds" << endl;
  }
  // for (int i = 0; i < array_size; i++) {
  //   cout << "value is  " << array_members[i];
  // }
}
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900