Start learning free

LESSON 216 OF 410 · BRUTE FORCE

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

Practice O(n3) Read the full explanation

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)


STEP 1 OF 6
    Ctrl + Enter to check

    JAVASCRIPT · BRUTE FORCE

    Longest Subarray Without Reapeating in JavaScript, explained

    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 and Assumptions

    • The array can contain both positive and negative integers.
    • The array can be of any length, including zero.

    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.

    Approach

    To solve this problem, we can start with a naive approach and then discuss more optimized solutions.

    Naive Solution

    The naive solution involves checking every possible subarray and determining if it contains all unique elements. This approach is not optimal due to its high time complexity.

    Optimized Solution

    We can use a sliding window approach to optimize the solution. This involves maintaining a window of unique elements and expanding or contracting the window as needed.

    Algorithm

    Here is a step-by-step breakdown of the sliding window approach:

    1. Initialize two pointers, start and end, both set to the beginning of the array.
    2. Use a set to keep track of unique elements in the current window.
    3. Expand the window by moving the end pointer and adding elements to the set until a duplicate is found.
    4. When a duplicate is found, move the start pointer to the right until the duplicate is removed from the window.
    5. Keep track of the maximum length of the window during this process.

    Code Implementation

    // Function to find the length of the longest subarray without repeating integers
    function longestSubarrayWithoutRepeating(nums) {
        let maxLength = 0; // Variable to store the maximum length of subarray
        let start = 0; // Start pointer of the sliding window
    
        // Iterate through the array with the end pointer
        for (let end = 0; end < nums.length; end++) {
            // Check for duplicates in the current window
            for (let i = start; i < end; i++) {
                if (nums[i] === nums[end]) {
                    // Move the start pointer to the right of the duplicate
                    start = i + 1;
                    break;
                }
            }
            // Update the maximum length of the subarray
            maxLength = Math.max(maxLength, end - start + 1);
        }
    
        return maxLength; // Return the maximum length found
    }
    
    // Example usage
    const nums = [2, 5, 6, 2, 3, 1, 5, 6];
    console.log(longestSubarrayWithoutRepeating(nums)); // Output: 5
    

    Complexity Analysis

    The time complexity of the naive approach is O(n^3) due to the nested loops checking for duplicates. The space complexity is O(1) as we are not using any extra space proportional to the input size.

    Edge Cases

    Consider the following edge cases:

    • Empty array: The output should be 0.
    • Array with all unique elements: The output should be the length of the array.
    • Array with all identical elements: The output should be 1.

    Testing

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

    • nums = [] (Output: 0)
    • nums = [1, 2, 3, 4, 5] (Output: 5)
    • nums = [1, 1, 1, 1] (Output: 1)
    • nums = [2, 5, 6, 2, 3, 1, 5, 6] (Output: 5)

    Thinking and Problem-Solving Tips

    When approaching such problems, consider the following tips:

    • Break down the problem into smaller parts and solve each part step-by-step.
    • Think about different approaches and their trade-offs.
    • Practice similar problems to improve problem-solving skills.

    Conclusion

    In this blog post, we discussed how to find the length of the longest subarray without repeating integers using a naive approach. 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:

    Keep going with JavaScriptAbout 340 step-by-step lessons, from your first line of code to coding interviews. Free to start.
    Start learning free