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
For this lesson, your algorithm should run in O(n^2) time and use O(n) extra space.
(There exist faster solutions which we will discuss in future lessons)
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.
nums
.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
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 analysis, substring problems in strings, and more. A common pitfall is not handling the repeated elements correctly, which can lead to incorrect subarray lengths.
To solve this problem, we can use a sliding window approach with a nested loop to check for the longest subarray without repeating elements. This approach ensures that we check all possible subarrays and keep track of the maximum length found.
A naive solution would involve checking all possible subarrays and verifying if they contain unique elements. This approach is not optimal as it has a time complexity of O(n^3).
We can optimize the solution by using a sliding window approach with a set to keep track of unique elements. This approach reduces the time complexity to O(n^2) as required.
Here is a step-by-step breakdown of the optimized algorithm:
maxLength
to store the maximum length of the subarray found.import java.util.HashSet;
import java.util.Set;
public class LongestSubarrayWithoutRepeating {
public static int lengthOfLongestSubarray(int[] nums) {
int maxLength = 0;
// Outer loop to set the starting point of the subarray
for (int i = 0; i < nums.length; i++) {
Set<Integer> uniqueElements = new HashSet<>();
// Inner loop to set the ending point of the subarray
for (int j = i; j < nums.length; j++) {
// If the element is already in the set, break the loop
if (uniqueElements.contains(nums[j])) {
break;
}
// Add the element to the set
uniqueElements.add(nums[j]);
// Update the maximum length
maxLength = Math.max(maxLength, j - i + 1);
}
}
return maxLength;
}
public static void main(String[] args) {
int[] nums = {2, 5, 6, 2, 3, 1, 5, 6};
System.out.println("Length of the longest subarray without repeating integers: " + lengthOfLongestSubarray(nums));
}
}
The time complexity of this approach is O(n^2) because of the nested loops. The space complexity is O(n) due to the set used to store unique elements.
Consider the following edge cases:
To test the solution comprehensively, consider the following test cases:
nums = []
(Empty array)nums = [1, 2, 3, 4, 5]
(All unique elements)nums = [1, 1, 1, 1]
(All identical elements)nums = [2, 5, 6, 2, 3, 1, 5, 6]
(Mixed elements)When approaching such problems, consider the following tips:
In this blog post, we discussed how to find the length of the longest subarray without repeating integers using a sliding window approach with a set. 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.
For further reading and practice, consider the following resources: