Search code examples
c++intcmath

c++ getting a negative value when adding two absolute (positive) values


I am getting a strange problem. I am getting a negative value when adding two absolute (positive) values. I am trying to solve this exercise from codingame (link: https://www.codingame.com/ide/puzzle/network-cabling).

I tried debugging and I got this:

X[i]: 19715507  X[j]: 938059973   // This one is good
temp: 918344466   

Y[i]: 470868309  Y[j]: -816049599 //Something is wrong here
temp: -2089704922

This code is probably not a good solution to the exercise and I will need to improve it, but I still can't figure it out why the value is negative.

Please help.

Thank you.

Code:

int main()
{

    int N;
    int X[100000];
    int Y[100000];

    cin >> N; cin.ignore();
    for (int i = 0; i < N; i++) {   //reading all input

        cin >> X[i] >> Y[i]; cin.ignore();
    }

    int ilgis=0;   //ilgis means length

    for(int i=0; i<N-1; i++){       //"min" is set to a very high value 
                                    //so it could find a lower value later
                                    // "not the best solution"

        int min=99999999999999999999;   //shortest distance between houses
        for(int j=0; j<N; j++){    // here I am trying to find the shortest 
                                   // distance from one house to another

            if(j!=i){     //I can't count distance from the same house because 
                          // it would be 0

                int temp=0;
                                    //counting the distance differently if the 
                                    //value is negative or positive

                if(X[i]<=0&&X[j]<=0) temp+=abs(abs(X[i])-abs(X[j]));
                else if(X[i]<=0&&X[j]>=0) temp+=abs(X[i])+abs(X[j]);
                else if(X[i]>=0&&X[j]>=0) temp+=abs(X[i]-X[j]);
                else if(X[i]>=0&&X[j]<=0) temp+=(X[i])+abs(X[j]);

                                       //same with y axis

                if(Y[i]<=0&&Y[j]<=0) temp+=abs(abs(Y[i])-abs(Y[j]));
                else if(Y[i]<=0&&Y[j]>=0) temp+=abs(Y[i])+abs(Y[j]);
                else if(Y[i]>=0&&Y[j]>=0) temp+=abs(Y[i]-Y[j]);
                else if(Y[i]>=0&&Y[j]<=0) temp+=(Y[i])+abs(Y[j]); 

                if(min>temp) min=temp;  
                }
            }                   //if i found the shortesst distance between 
                                //houses I add that value to overall distance
                                // and continue until all houses are checked
        ilgis+=min;

        }
    cout<<ilgis<<endl;
}

Input of the exercise:

 8
-28189131 593661218
102460950 1038903636
938059973 -816049599
-334087877 -290840615
842560881 -116496866
-416604701 690825290
19715507 470868309
846505116 -694479954

Solution

  • The maximum value for int is 2147483647 (which is 2^31-1, assuming int has 32 bits width). You can check it with

    std::numeric_limits<int>::max();
    

    You might want to min with this value here. And also use long long as your type for calculations here.