BackmediumArraysPhonePe

Longest Uniform Subarray Solution

Problem Statement

Given a sequence of integers nums, determine the length of the longest contiguous segment where all elements are identical. If the sequence is empty, return 0.

Example 1
Input
[1, 1, 1, 2, 2, 2, 2, 3, 3, 3, 3, 3]
Output
3

Explanation: Step-by-step: with input [1, 1, 1, 2, 2, 2, 2, 3, 3, 3, 3, 3], we initialize a variable `maxLen` to 1 and a variable `currLen` to 1. We then iterate through the sequence, incrementing `currLen` for each identical element and updating `maxLen` if `currLen` is greater than `maxLen`. Finally, we return `maxLen`, which is 3.

Example 2
Input
[1, 2, 3, 4, 5]
Output
1

Explanation: Step-by-step: with input [1, 2, 3, 4, 5], we initialize a variable `maxLen` to 1 and a variable `currLen` to 1. We then iterate through the sequence, incrementing `currLen` for each identical element and updating `maxLen` if `currLen` is greater than `maxLen`. Since all elements are distinct, `currLen` remains 1, and we return `maxLen`, which is 1.

Constraints

  • 1 <= seq length <= 10^5
  • -10^9 <= seq elements <= 10^9
  • seq is a list of integers
  • seq can be empty
  • seq elements are not guaranteed to be distinct
Live Compiler1 Free Run Available
Loading Editor...
Test Cases & Output
Click "Run" to test your 1 free compile trial!

🚀 Practice this problem

Run code, get AI hints & track streak

Sign Up Free

Longest Uniform Subarray — Problem Statement & Solution Guide

ArraysMediumsubarray
TimeO(n)
|
SpaceO(1)

Problem Description

Given a sequence of integers nums, determine the length of the longest contiguous segment where all elements are identical. If the sequence is empty, return 0.

Examples

Example 1

Input

[1, 1, 1, 2, 2, 2, 2, 3, 3, 3, 3, 3]

Output

3

Explanation: Step-by-step: with input [1, 1, 1, 2, 2, 2, 2, 3, 3, 3, 3, 3], we initialize a variable `maxLen` to 1 and a variable `currLen` to 1. We then iterate through the sequence, incrementing `currLen` for each identical element and updating `maxLen` if `currLen` is greater than `maxLen`. Finally, we return `maxLen`, which is 3.

Example 2

Input

[1, 2, 3, 4, 5]

Output

1

Explanation: Step-by-step: with input [1, 2, 3, 4, 5], we initialize a variable `maxLen` to 1 and a variable `currLen` to 1. We then iterate through the sequence, incrementing `currLen` for each identical element and updating `maxLen` if `currLen` is greater than `maxLen`. Since all elements are distinct, `currLen` remains 1, and we return `maxLen`, which is 1.

Constraints

  • 1 <= seq length <= 10^5
  • -10^9 <= seq elements <= 10^9
  • seq is a list of integers
  • seq can be empty
  • seq elements are not guaranteed to be distinct

Optimal Approach & Strategy

The optimal approach involves scanning the sequence once and keeping track of the current uniform subarray length. When a different element is encountered, the length of the current uniform subarray is compared with the maximum length found so far and updated if necessary. This approach has a time complexity of O(n) and a space complexity of O(1), where n is the length of the sequence.

Brute Force Approach

The brute force approach involves iterating over the sequence and comparing each element with its adjacent elements to identify uniform subarrays. This can be achieved by using nested loops to generate all possible subarrays and checking each one for uniformity. However, this approach is inefficient for large sequences due to its high time complexity.

Verified Code Solutions

JavaScript Solution
Time: O(n)
function solution(nums) {
      if (nums.length === 0) return 0;
      let maxLen = 1;
      let currLen = 1;
      for (let i = 1; i < nums.length; i++) {
         if (nums[i] === nums[i - 1]) {
            currLen++;
            maxLen = Math.max(maxLen, currLen);
         } else {
            currLen = 1;
         }
      }
      return maxLen;
}

Asked in Top Tech Interviews

PhonePe

Solve in Interative Editor

Ready to test your code? Open our built-in compiler, run custom test suites, and see detailed complexity analysis reports instantly.