459. Maximum Consistent Activity Streak
Maximum Consistent Activity Streak

Atlassian records daily activity as a list of integers named activity, ordered by day. A consistent activity streak is a contiguous part of this list containing at least two days, with the same difference between every pair of neighboring values.

Methods

Part 1: Without Changes

public int longestConsistentStreak(List<Integer> activity)

  • activity: The activity values for consecutive days.
  • Return the maximum length of a consistent streak without replacing any value.

Part 2: At Most One Change

public int longestConsistentStreakWithOneChange(List<Integer> activity)

  • activity: The activity values for consecutive days.
  • Return the maximum possible length of a consistent streak after replacing at most one value with any integer, including a negative integer.
  • You may leave every value unchanged.

Rules

  • A streak must contain consecutive days; values cannot be skipped or reordered.
  • The common difference may be positive, zero, or negative.
  • Every pair of consecutive values forms a valid streak of length 2.
  • Each method evaluates its input independently and leaves the supplied list unchanged.
  • Return only the maximum length. Multiple optimal streaks or replacements produce the same integer result.

Constraints

  • 2 ≤ activity.size() ≤ 100,000
  • 0 ≤ i < activity.size()
  • -1,000,000,000 ≤ activity.get(i) ≤ 1,000,000,000
  • The list and its elements must be non-null.
  • The value bounds apply only to the input; a replacement may be any integer.

Examples

Example calls label the argument as activity = [...] for readability.

Example 1: Repairing a Streak

Call: longestConsistentStreak(activity = [4, 9, 14, 31, 24, 29])

Output: 3

The streak [4, 9, 14] has a common difference of 5.

Call: longestConsistentStreakWithOneChange(activity = [4, 9, 14, 31, 24, 29])

Output: 6

Replace 31 with 19. The entire list becomes [4, 9, 14, 19, 24, 29], with a common difference of 5.

Example 2: No Change Needed

Call: longestConsistentStreak(activity = [8, 8, 8, 8])

Output: 4

Call: longestConsistentStreakWithOneChange(activity = [8, 8, 8, 8])

Output: 4

The entire list already has a common difference of 0.

Example 3: A Negative Replacement

Call: longestConsistentStreak(activity = [9, 2, 7, 12])

Output: 3

The streak [2, 7, 12] has a common difference of 5.

Call: longestConsistentStreakWithOneChange(activity = [9, 2, 7, 12])

Output: 4

Replace the first value, 9, with -3. The entire list becomes [-3, 2, 7, 12].

Example 4: One Change Is Not Enough for the Entire List

Call: longestConsistentStreak(activity = [2, 5, 9, 12])

Output: 2

No three consecutive values have a constant difference.

Call: longestConsistentStreakWithOneChange(activity = [2, 5, 9, 12])

Output: 3

Replace 2 with 1 to obtain the streak [1, 5, 9], with a common difference of 4. No single replacement can make all four values consistent.



Please use Laptop/Desktop or any other large screen to add/edit code.