FastPrepPlus Mult Array

Plus Mult Array

IBM logoIBM● EasyINTERNNEW GRADOA
Learn

Problem statement

Given an integer array A, split its elements by zero-based index into an even-index subsequence A[0], A[2], A[4], ... and an odd-index subsequence A[1], A[3], A[5], ....

Evaluate each subsequence from left to right. Multiply its first two values, add the third value, multiply by the fourth value, add the fifth value, and continue alternating addition and multiplication. Let R_even be the even-index result modulo 2, and let R_odd be the odd-index result modulo 2. Normalize each remainder to either 0 or 1, including when the evaluated expression is negative.

Return the array classification according to these rules:

  • Return "ODD" when R_odd > R_even.
  • Return "EVEN" when R_even > R_odd.
  • Return "NEUTRAL" when R_even = R_odd.

Function

plusMultArray(A: int[]) → String

Examples

Example 1

A = [12,3,5,7,13,12]return = "NEUTRAL"

For the even indices, R_even = (12 × 5 + 13) mod 2 = 73 mod 2 = 1. For the odd indices, R_odd = (3 × 7 + 12) mod 2 = 33 mod 2 = 1. The two remainders are equal, so the result is "NEUTRAL".

Example 2

A = [0,1,0,1,0,1,0,1,0,1]return = "ODD"

The even-index subsequence is [0,0,0,0,0], whose alternating result is 0. The odd-index subsequence is [1,1,1,1,1], whose result is (((1 × 1) + 1) × 1) + 1 = 3, so R_odd = 1. Therefore the classification is "ODD".

Constraints

  • 10 ≤ A.length ≤ 10^5
  • -10^9 ≤ A[i] ≤ 10^9

More IBM problems

See IBM hiring insights
public String plusMultArray(int[] A) {
  // Write your code here
}
A[12,3,5,7,13,12]
expected"NEUTRAL"
Checking account…