Minimum operations
Practice
2.8 (6 votes)
Basic programming
Basics of implementation
Problem
57% Success 899 Attempts 30 Points 2s Time Limit 256MB Memory 1024 KB Max Code

You are given an array A of size N. You can perform the following operation on array A:

  • Select two indices i and j such that 1 ≤  i, j ≤  N. (note that i and j can be equal)
  • Assign Ai = Ai + 2
  • Then assign Aj = Aj - 1

You need to make all the elements of the array equal to zero.

Task

Determine the minimum number of operations required to make all the elements of A equal to zero. Else, print -1 if it is not possible to do so.

Function description

Complete the function solve() provided in the editor. This function takes the following parameters and returns the required answer:

  • N: Represents the size of the array A
  • A: Represents the elements of array A.

Input format

Note: This is the input format that you must use to provide custom input (available above the Compile and Test button).

  • The first line contains T, denoting the number of test cases. T also specifies the number of times you have to run the solve() function on a different set of inputs.
  • For each test case:
    • The first line contains N, denoting the size of array A.
    • The second line contains space-separated values, denoting the elements of array A.

Output format For each test case, print the output on a new line. Either the minimum number of operations required to make all the elements of A equal to zero or print -1 if it is impossible to do so.

Constraints

\(1 \leq T \leq 1000 \)

\(1 \le N \le 10^5\)

\(-10^9\le A_i \le 10^9\space \forall \text{ 1}\le i\le N\)

\(\sum N_i \le 10^5 \space \forall \text{ 1}\le i\le T\) .

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:30
54 votes
Tags:
ApprovedImplementationMediumOpen
Points:30
6 votes
Tags:
AlgorithmsBasic ProgrammingBasics of ImplementationImplementationMedium
Points:30
11 votes
Tags:
Basic ProgrammingBasics of ImplementationImplementaionImplementationMedium