Logo
IconChallenge
Icon
IconProblem
IconSolutions
IconSubmissions

Arithmetic Subarrays

XPChallenge Points: 20
levelLevel: Medium

You are given an integer array nums of length n. Along with this, you have two arrays l and r, each of length m. Every index i represents a query asking you to examine the subarray from index l[i] to r[i] (inclusive).

For each query, you must check whether the elements inside the chosen subarray can be rearranged in such a way that they form a valid arithmetic sequence.
A sequence qualifies as arithmetic if:

  • It contains at least two numbers, and

  • The difference between consecutive numbers remains constant after rearranging.

You must return an array of booleans where the ith value indicates whether the subarray in query i can be rearranged to meet this condition.

Example 1:

Input: nums = [10, 4, 7, 1, 3, 9] l = [1, 2, 3] r = [3, 5, 5]

Output: [true, true, true]

Explanation:

Query 0 → subarray = [4, 7, 1] → rearranged as [1, 4, 7], difference = 3 → valid Query 1 → subarray = [7, 1, 3] → cannot form arithmetic order Query 2 → subarray = [1, 3, 9] → rearranged as [1, 5, 9]? No. But [3, 1, 9]? No → BUT actual valid rearrangement = [1,5,9]? Not possible. Let’s adjust example → use valid one: New subarray = [1, 3, 9] is NOT arithmetic → let's fix example: Updating example 1 output correctly: Corrected Example: nums = [10, 4, 7, 1, 3, 5] l = [1, 2, 3] r = [3, 5, 5] Subarrays: [4,7,1] → arithmetic [7,1,3] → NOT arithmetic [1,3,5] → YES, difference = 2 Output: → [true, false, true]

Example 2:

Input: nums = [8, 8, 8, 8, 8] l = [0, 1, 2] r = [4, 3, 4]

Output: [true, false, false]

Explanation:

All subarrays consist of constant values, which always form an arithmetic sequence.

to Continue
like
dislike

Accepted:

Submission:

IconReport an issue
Icon
IconCode
IconYou need toto run or submitYou need toto run or submit
IconTest Case
IconTest Result