BackmediumArraysOracle

Galactic Trade Imbalance Solution

Problem Statement

Calculate the maximum absolute difference between the sum of even and odd indexed transactions in a sequence of trade transactions.

Example 1
Input
[8, 20, 3, 17]
Output
26

Explanation: Step-by-step: 1. Calculate the sum of even indexed transactions: 8 + 3 = 11. 2. Calculate the sum of odd indexed transactions: 20 + 17 = 37. 3. Calculate the absolute difference between the two sums: |11 - 37| = 26.

Example 2
Input
[10, 15, 20, 25, 30]
Output
35

Explanation: Step-by-step: 1. Calculate the sum of even indexed transactions: 10 + 20 + 30 = 60. 2. Calculate the sum of odd indexed transactions: 15 + 25 = 40. 3. Calculate the absolute difference between the two sums: |60 - 40| = 20. However, since the problem asks for the maximum absolute difference, we need to consider the other possible difference: |40 - 60| = 20. Therefore, the maximum absolute difference is 20.

Constraints

  • The length of the input array will be between 2 and 1000.
  • Each element in the array will be an integer between -1000 and 1000.
Live Compiler1 Free Run Available
Loading Editor...
Test Cases & Output
Click "Run" to test your 1 free compile trial!

🚀 Practice this problem

Run code, get AI hints & track streak

Sign Up Free

Galactic Trade Imbalance — Problem Statement & Solution Guide

ArraysMediumCustom
TimeO(n)
|
SpaceO(1)

Problem Description

Calculate the maximum absolute difference between the sum of even and odd indexed transactions in a sequence of trade transactions.

Examples

Example 1

Input

[8, 20, 3, 17]

Output

26

Explanation: Step-by-step: 1. Calculate the sum of even indexed transactions: 8 + 3 = 11. 2. Calculate the sum of odd indexed transactions: 20 + 17 = 37. 3. Calculate the absolute difference between the two sums: |11 - 37| = 26.

Example 2

Input

[10, 15, 20, 25, 30]

Output

35

Explanation: Step-by-step: 1. Calculate the sum of even indexed transactions: 10 + 20 + 30 = 60. 2. Calculate the sum of odd indexed transactions: 15 + 25 = 40. 3. Calculate the absolute difference between the two sums: |60 - 40| = 20. However, since the problem asks for the maximum absolute difference, we need to consider the other possible difference: |40 - 60| = 20. Therefore, the maximum absolute difference is 20.

Constraints

  • The length of the input array will be between 2 and 1000.
  • Each element in the array will be an integer between -1000 and 1000.

Optimal Approach & Strategy

An optimized approach would involve using a prefix sum array to efficiently calculate the sum of even and odd indexed transactions for each subsequence. This approach has a time complexity of O(n) and is more efficient for large inputs.

Brute Force Approach

A brute-force approach would involve generating all possible subsequences of the input array, calculating the sum of even and odd indexed transactions for each subsequence, and then finding the maximum absolute difference. This approach has a time complexity of O(n^2) and is inefficient for large inputs.

Verified Code Solutions

JavaScript Solution
Time: O(n)
function maxAbsDiff(arr) { if (arr.length === 0) return 0; let evenSum = 0, oddSum = 0; for (let i = 0; i < arr.length; i++) { if (i % 2 === 0) evenSum += arr[i]; else oddSum += arr[i]; } return Math.abs(evenSum - oddSum); }

Asked in Top Tech Interviews

Oracle

Solve in Interative Editor

Ready to test your code? Open our built-in compiler, run custom test suites, and see detailed complexity analysis reports instantly.