Learn DSADSAContests
coding75 logo
LeetCode POTDPOTDSheets
coding75 Pro
coding75 logo
Dashboard
Learn DSA
Course RoadmapFlow
DSA Topic TreeNew πŸš€
Contest SolutionsPractice
Practice Sheets
MasterclassesLive πŸ‘¨πŸ»β€πŸ’»

coding75 ProPRO

Live Classes & Placement Guidance

coding75 logo

The premier developer platform for mastering Data Structures & Algorithms, exploring contest editorials, building ATS-ready resumes, and accelerating your tech career.

Connect & Community

DSA & Contests

  • Learn DSA
  • Contest Solutions
  • LeetCode POTDDaily
  • Practice Sheets
  • MasterclassesSoon

Interview Prep

  • Portfolio Projects
  • CS Fundamentals
  • System DesignSoon
  • Interview ExperiencesSoon
  • Mock InterviewsSoon

Career & Pro

  • Jobs & Internships
  • Resume BuilderATS
  • coding75 Pro
  • Submit Feedback
Β© 2026coding75β€’crackDSAβ„’β€’Maa Lalita Edtech Private Limited. All Rights Reserved.
Privacy PolicyTerms & ConditionsContact Support
Registered Office: Kanpur 208021β€’Regional Office: Indiranagar, Bangalore, 560008
Maa Lalita Edtech Private Limited
Contests/Codeforces/Codeforces Round 1057/D. Not Alone
Prev
CodeforcesCodeforces
Expert
Codeforces Round 1057

D. Not Alone

Dynamic ProgrammingArray
Loading...
Solve on Codeforces

Problem Statement

Official algorithmic problem description and constraints.

Open on Codeforces

A circular array π‘

 of length π‘š

 is nice if every element has at least one adjacent element

βˆ—

 that is equal to it. Formally, for every 1β‰€π‘–β‰€π‘š

, at least one of the following holds: π‘

𝑖

=𝑏

(𝑖+π‘šβˆ’2)modπ‘š+1

, or π‘

𝑖

=𝑏

𝑖modπ‘š+1

, where π‘₯mod𝑦

 denotes the remainder from dividing π‘₯

 by π‘¦

.

You are given a circular array π‘Ž

 of length π‘›

. In one operation, you may increase or decrease any element of π‘Ž

 by 1

. Your task is to determine the minimum number of operations required to make array π‘Ž

 nice. More formally, find the minimum value of βˆ‘

𝑛

𝑖=1

|𝑏

𝑖

βˆ’π‘Ž

𝑖

|

 among all nice circular arrays π‘

 of length π‘›

.


βˆ—

In a circular array of length π‘š

:

  • For each index 2β‰€π‘–β‰€π‘šβˆ’1
  • , the element at index π‘–
  •  is adjacent to the elements at indices π‘–βˆ’1
  •  and π‘–+1
  • .
  • The element at index 1
  •  is adjacent to the elements at indices 2
  •  and π‘š
  • .
  • The element at index π‘š
  •  is adjacent to the elements at indices π‘šβˆ’1
  •  and 1
  • .

Input


Each test contains multiple test cases. The first line contains the number of test cases π‘‘

 (1≀𝑑≀10

4

). The description of the test cases follows.

The first line of each test case contains a single integer π‘›

 (3≀𝑛≀2β‹…10

5

) β€” the length of the circular array π‘Ž

.

The second line of each test case contains π‘›

 integers π‘Ž

1

,π‘Ž

2

,…,π‘Ž

𝑛

 (1β‰€π‘Ž

𝑖

≀10

9

) β€” the elements of the circular array π‘Ž

.

It is guaranteed that the sum of π‘›

 over all test cases does not exceed 2β‹…10

5

.

Output


For each test case, output a single integer representing the minimum number of operations required to make array π‘Ž

 nice.

Example

Input

Copy

4
5
1 1 1 1 1
4
2 100 99 3
5
2 2 5 9 5
6
1 1 1 2 1 2

Output

Copy

0
2
4
1

Note


In the first test case, all elements of π‘Ž

 are equal. Therefore, the circular array π‘Ž

 is already nice and no operations are required.

In the second test case, we can perform the following sequence of operations:

  • Increase π‘Ž
  • 1
  •  by 1
  • . Now, π‘Ž=[3,100,99,3]
  • .
  • Decrease π‘Ž
  • 2
  •  by 1
  • . Now, π‘Ž=[3,99,99,3]
  • .

After these operations, every element has at least one adjacent element with the same value:

  • π‘Ž
  • 1
  •  is equal to π‘Ž
  • 4
  • .
  • π‘Ž
  • 2
  •  is equal to π‘Ž
  • 3
  • .
  • π‘Ž
  • 3
  •  is equal to π‘Ž
  • 2
  • .
  • π‘Ž
  • 4
  •  is equal to π‘Ž
  • 1
  • .

In the third test case, the circular array π‘Ž

 can become nice by decreasing π‘Ž

4

 four times. This results in π‘Ž=[2,2,5,5,5]

 which is nice as every element has at least one adjacent element with the same value.


Solutions & Walkthrough

Detailed video explanations, mathematical intuition, and clean C++ implementation code.

Connecting secure classroom stream...
Back to Codeforces Round 1057
Previous Problem