Good Arrays

You are given an array ‘A’ of length ‘N’, you have to choose an element from any index in this array and delete it. After deleting the element you will get a new array of length ‘N’-1. Your task is to find the number of such arrays of length ‘N’-1 which are good.

Note :

An array is called good if the sum of elements in odd indexes is equal to the sum of elements in even indexes.

For Example :

In array A= [1 2 4 3 6], if we delete A[4]=6, we will get new array B= [1 2 4 3], where B[0] + B[2] = B[1] + B[3] = 5, which means array B is good.

Input Format :

The first line of the input contains ‘T’ denoting the number of test cases.

The first line of each test case contains the three integers N, length of the array.

The second line of each test case contains N space-separated integers of the array A. 

Output Format :

For each test case, print an integer denoting the number of good arrays that can be formed.

Note :

You do not need to print anything, it has already been taken care of. Just implement the given function

Constraints :

1 <= T <= 5
1 <= N <= 3000
-5000 <= A[i] <= 5000

Where 'A[i]' denotes the 'ith' element of the given array.

Time Limit:  1sec
CodingNinjas
author
2y

Algorithm :

1) Create a variable ‘res’ =0, which will store the number of good arrays that can be formed.
2) Create four variables ‘oddRightSum’ = 0, ‘evenRightSum’ = 0, ‘oddLeftSum’ = 0, ‘evenLeftSum’...read more

CodingNinjas
author
2y
Brute Force

Explanation:

In this approach, we will delete all index values one by one and make a new array after deleting. Then in the new array, we just check the sum of elements in even and odd index...read more

CodingNinjas
author
2y
Efficient

Explanation:

  • If for each index ‘i’ we somehow knew what is the sum of the even index element in the left part (i.e. even index in 0…’i’-1) and what is the sum of the odd index element in the ...read more
Add answer anonymously...
Adobe Member Technical Staff Interview Questions
Stay ahead in your career. Get AmbitionBox app
qr-code
Helping over 1 Crore job seekers every month in choosing their right fit company
65 L+

Reviews

4 L+

Interviews

4 Cr+

Salaries

1 Cr+

Users/Month

Contribute to help millions
Get AmbitionBox app

Made with ❤️ in India. Trademarks belong to their respective owners. All rights reserved © 2024 Info Edge (India) Ltd.

Follow us
  • Youtube
  • Instagram
  • LinkedIn
  • Facebook
  • Twitter