why im getting run time error and you one should use over flow condition here

class Solution {
public:
int threeSumClosest(vector& nums, int target) {
sort(nums.begin(), nums.end());

// To store the closest sum

//not using INT_MAX to avoid overflowing condition
int closestSum = INT_MAX;
int n=nums.size();
// Fix the smallest number among
// the three integers
for (int i = 0; i < n - 2; i++) {

    // Two pointers initially pointing at
    // the last and the element
    // next to the fixed element
    int ptr1 = i + 1, ptr2 = n - 1;

    // While there could be more pairs to check
    while (ptr1 < ptr2) {
        // Calculate the sum of the current triplet
        int sum = nums[i] + nums[ptr1] + nums[ptr2];
          // if sum is equal to x, return sum as
          if (sum == target)
          return sum;
        // If the sum is more closer than
        // the current closest sum
        if (abs(target - sum) < abs(target - closestSum)) {
            closestSum = sum;
        }

        // If sum is greater then x then decrement
        // the second pointer to get a smaller sum
        if (sum > target) {
            ptr2--;
        }

        // Else increment the first pointer
        // to get a larger sum
        else {
            ptr1++;
        }
    }
}
return closestSum;

    
}

};

Comments (0)