LeetCode #2441 Largest Positive Integer That Exists With Its Negative Solution & Explanation

LeetCode Problem

Given an integer array nums that does not contain any zeros, find the largest positive integer k such that -k also exists in the array.

Return the positive integer k. If there is no such integer, return -1.

Example 1:

Input: nums = [-1,2,-3,3]
Output: 3
Explanation: 3 is the only valid k we can find in the array.

Example 2:

Input: nums = [-1,10,6,7,-7,1]
Output: 7
Explanation: Both 1 and 7 have their corresponding negative values in the array. 7 has a larger value.

Example 3:

Input: nums = [-10,8,6,7,-2,-3]
Output: -1
Explanation: There is no a single valid k, we return -1.

Constraints:

  • 1 <= nums.length <= 1000
  • -1000 <= nums[i] <= 1000
  • nums[i] != 0

Solution

In our solution, we first sort the input array, which ensures that negative and positive numbers are adjacent. Then, we use two pointers to simultaneously traverse the array, searching for a pair of elements whose absolute values are equal but with opposite signs, indicating the existence of -k and k.

If such a pair is found, we return one of the elements. This approach optimizes the search process, enabling efficient identification of the largest positive integer k meeting the specified conditions.

C# Solution

Solution1 – Sort + two pointers

public class Solution {
    public int FindMaxK(int[] nums) {
        Array.Sort(nums);
        int i = 0;
        int j = nums.Length-1;

        while(i<j){
            var num1 = nums[i];
            var num2 = nums[j];

            if(num1>=0 || num2<=0){
                break;
            }

            if(num1*-1==num2){
                return num2;
            }
            else{
                if(Math.Abs(num1)>num2){
                    i+=1;
                }
                else{
                    j-=1;
                }

            }

        }
        return -1;
    }
}

Time Complexity : O(n log n)

Java Solution

Solution1 – Sort + two pointers

class Solution {
    public int findMaxK(int[] nums) {
        Arrays.sort(nums);
        int i = 0;
        int j = nums.length-1;

        while(i<j){
            int num1 = nums[i];
            int num2 = nums[j];

            if(num1>=0 || num2<=0){
                break;
            }

            if(num1*-1==num2){
                return num2;
            }
            else{
                if(Math.abs(num1)>num2){
                    i+=1;
                }
                else{
                    j-=1;
                }
            }

        }
        return -1;
    }
}
Klook.com

Conclusion

🧡If my solution helps, that is my honor!

🧡You can support me by sharing my posts, thanks a lot

If you got any problem about the explanation, please feel free to let me know

The problem link : Largest Positive Integer That Exists With Its Negative – LeetCode

Similar Questions(using two pointers)

Some Random LeetCode posts

Leave a Reply

Your email address will not be published. Required fields are marked *