Sliding Window Technique - Part 14

A bit of vintage coding, no GAI involved. This is a sliding window problem, it is a bit tricky because you have to maintain not only the numbers in the candidate subarray but also the sum of pairs in that subarray, so two different data structures (I used hash tables but you can use other dictionary data structures) to keep this information. You need to update both of them accordingly when moving either the right pointer or the left one. Complexity is N^2 (N==1000) however the usage of untyped hash tables is slowing down the constant significantly, there are other ways especially given that the numbers in the array are small-bounded. Code is down below, cheers, ACC.


Longest Subarray With Restricted Pair Sums - LeetCode

You are given an integer array nums.

A nums[l..r] is valid if there are no three distinct indices i, j, and k such that l <= i, j, k <= r and:

  • nums[i] + nums[j] == nums[k]

Return the maximum length of a valid subarray of nums.

 

Example 1:

Input: nums = [2,3,5,3,2,1]

Output: 3

Explanation:

Consider the subarray [3, 5, 3]. The pairs of elements at distinct indices have the following sums:

  • 3 + 5 = 8
  • 3 + 3 = 6, using the two different occurrences of 3
  • 5 + 3 = 8

None of these sums is an element at the remaining index, so the subarray is valid.

Every subarray of length 4 contains 2, 3, and 5 at distinct indices, where 2 + 3 = 5. Therefore, no longer valid subarray exists, and the answer is 3.

Example 2:

Input: nums = [3,4,5,6]

Output: 4

Explanation:

The sums obtained from every pair of elements at distinct indices are 7, 8, 9, 9, 10, and 11. None of these values appears at the remaining index, so the entire array is valid.

 

Constraints:

  • 1 <= nums.length <= 1000
  • 1 <= nums[i] <= 500

public class Solution {
    public int MaxSubarray(int[] nums)
    {
        int retVal = 0;
        int left = 0;
        int right = 0;

        Hashtable sumOfPairsInSubarray = new Hashtable();

        Hashtable numbersInSubarray = new Hashtable();
        numbersInSubarray.Add(nums[left], 1);
        retVal = 1;
        right++;
        while (right < nums.Length)
        {
            bool validSubArray = true;
            if (sumOfPairsInSubarray.Contains(nums[right]))
            {
                validSubArray = false;
            }
            else
            {
                for (int i = left; i < right; i++)
                {
                    int sum = nums[i] + nums[right];
                    if (numbersInSubarray.Contains(sum) || sum == nums[right])
                    {
                        validSubArray = false;
                        break;
                    }
                }
            }
            if (validSubArray)
            {
                retVal = Math.Max(retVal, right - left + 1);

                if (!numbersInSubarray.Contains(nums[right])) numbersInSubarray.Add(nums[right], 0);
                numbersInSubarray[nums[right]] = (int)numbersInSubarray[nums[right]] + 1;

                for (int i = left; i < right; i++)
                {
                    int sum = nums[i] + nums[right];
                    if (!sumOfPairsInSubarray.Contains(sum)) sumOfPairsInSubarray.Add(sum, 0);
                    sumOfPairsInSubarray[sum] = (int)sumOfPairsInSubarray[sum] + 1;
                }

                right++;
            }
            else
            {
                numbersInSubarray[nums[left]] = (int)numbersInSubarray[nums[left]] - 1;
                if ((int)numbersInSubarray[nums[left]] == 0)
                    numbersInSubarray.Remove(nums[left]);

                for (int i = left + 1; i < right; i++)
                {
                    int sum = nums[left] + nums[i];
                    if (sumOfPairsInSubarray.Contains(sum))
                    {
                        sumOfPairsInSubarray[sum] = (int)sumOfPairsInSubarray[sum] - 1;
                        if ((int)sumOfPairsInSubarray[sum] == 0)
                            sumOfPairsInSubarray.Remove(sum);
                    }
                }

                left++;
            }
        }

        return retVal;
    }
}

Comments

Popular posts from this blog

Standard Priority Queue IX: Vowels Frequency

Binary Search to Find Next Greater Element V

Simulation: Build Generations