Equal Array
Practice
3.4 (22 votes)
Algorithms
Dynamic programming
Easy
Problem
50% Success 15608 Attempts 20 Points 1s Time Limit 256MB Memory 1024 KB Max Code

You are given an array \(A \) of size \(N\)

Find the minimum non negative number \(X\) such that there exist an index \(j\) and when you can replace \(A_j\) by \(A_j+X\), the sum of elements of array from index \(1 \) to \(j\)  and \(j+1\) to  \(N\) becomes equal, where \(1 \le j \le N-1\). Assume array to be 1-indexed.

If there is no possible \(X\) print \(-1\) in separate line.

Input Format

The first line contains \(T\), the number of test cases.
For each Test case :
The first line contains an integer \(N\), size of the array.
The second line contains \(N\) space-separated integers, the \(i^{th}\) of which is \(A_i\).

Input Constraints

\( 1 \le T \le 10^{ 5} \)
\( 2 \le N \le 10^{5} \)
\( 0 \le A_i \le 10^{6} \)
Sum of N all over testcases doesn't not exceed \(10^{6}\).

Output Format

For each test case, print the required answer in separate line.

Please login to use the editor

You need to be logged in to access the code editor

Loading...

Please wait while we load the editor

Loading...
Results
Custom Input
Run your code to see the output
Submissions
Please login to view your submissions
Similar Problems
Points:20
30 votes
Tags:
AlgorithmsDynamic Programming
Points:20
32 votes
Tags:
Dynamic ProgrammingEasy
Points:20
26 votes
Tags:
AlgorithmsApprovedBinary SearchData StructuresDynamic ProgrammingMediumOpenopen