Longest Subarray Without Repeating (O(n^3) Time Complexity, C++)


Given an input array of integers, find the length of the longest subarray without repeating integers.

Example

Input: nums = [2, 5, 6, 2, 3, 1, 5, 6]
Output: 5
Explanation: [5, 6, 2, 3, 1] or [6, 2, 3, 1, 5] are both valid and of maximum length 5

Note:

For this lesson, your algorithm should run in O(n^3) time and use O(1) extra space.
(There exist faster solutions which we will discuss in future lessons)


Problem Definition

The problem requires finding the length of the longest subarray in a given array of integers where no integer repeats. The input is an array of integers, and the output is a single integer representing the length of the longest subarray without repeating integers.

Input

An array of integers nums.

Output

An integer representing the length of the longest subarray without repeating integers.

Constraints

Example

Input: nums = [2, 5, 6, 2, 3, 1, 5, 6]
Output: 5
Explanation: [5, 6, 2, 3, 1] or [6, 2, 3, 1, 5] are both valid and of maximum length 5

Understanding the Problem

The core challenge is to identify the longest contiguous subarray where all elements are unique. This problem is significant in various applications such as data stream processing, substring problems in strings, and more.

Potential pitfalls include misunderstanding the requirement for contiguous subarrays and not handling edge cases like empty arrays or arrays with all unique elements correctly.

Approach

To solve this problem, we can start with a naive approach and then discuss why it is not optimal. We will then present a more optimized solution.

Naive Solution

The naive solution involves checking every possible subarray and verifying if it contains all unique elements. This approach is not optimal because it involves three nested loops, leading to a time complexity of O(n^3).

Optimized Solution

We can optimize the solution by using a sliding window technique with a hash set to keep track of unique elements. However, for this lesson, we will stick to the O(n^3) solution as required.

Algorithm

The algorithm involves iterating over all possible subarrays and checking if each subarray contains unique elements. If a subarray with all unique elements is found, we update the maximum length.

Step-by-Step Breakdown

  1. Initialize a variable maxLength to store the maximum length of subarrays found.
  2. Use three nested loops to generate all possible subarrays.
  3. For each subarray, use a set to check if all elements are unique.
  4. If the subarray is unique, update maxLength if the current subarray length is greater than maxLength.

Code Implementation

#include <iostream>
#include <vector>
#include <unordered_set>

using namespace std;

int longestSubarrayWithoutRepeating(vector<int>& nums) {
    int maxLength = 0;
    int n = nums.size();
    
    // Iterate over all possible subarrays
    for (int i = 0; i < n; ++i) {
        for (int j = i; j < n; ++j) {
            unordered_set<int> uniqueElements;
            bool isUnique = true;
            
            // Check if the subarray nums[i..j] has all unique elements
            for (int k = i; k <= j; ++k) {
                if (uniqueElements.find(nums[k]) != uniqueElements.end()) {
                    isUnique = false;
                    break;
                }
                uniqueElements.insert(nums[k]);
            }
            
            // Update maxLength if the subarray is unique
            if (isUnique) {
                maxLength = max(maxLength, j - i + 1);
            }
        }
    }
    
    return maxLength;
}

int main() {
    vector<int> nums = {2, 5, 6, 2, 3, 1, 5, 6};
    cout << "Length of the longest subarray without repeating integers: " << longestSubarrayWithoutRepeating(nums) << endl;
    return 0;
}

Complexity Analysis

The time complexity of the above algorithm is O(n^3) due to the three nested loops. The space complexity is O(1) extra space, as required, since we are only using a set to check for unique elements within the subarray.

Edge Cases

Potential edge cases include:

Each algorithm handles these edge cases by iterating through all possible subarrays and checking for uniqueness.

Testing

To test the solution comprehensively, consider the following test cases:

Use a testing framework like Google Test or simply run the main function with different inputs to verify the correctness of the solution.

Thinking and Problem-Solving Tips

When approaching such problems, consider the following tips:

Conclusion

In this blog post, we discussed how to find the length of the longest subarray without repeating integers using a O(n^3) time complexity algorithm. We covered the problem definition, approach, algorithm, code implementation, complexity analysis, edge cases, and testing. Understanding and solving such problems is crucial for improving problem-solving skills and preparing for coding interviews.

Additional Resources

For further reading and practice, consider the following resources: