703045. Sum of alternate product

Basic

Sum of Alternate Product (Minimize sum of alternate product)

Summary

Given an array of integers, find the minimum sum of products that can be obtained by selecting every other element from the array and multiplying them together. The array is of size n. This problem involves arrays and basic algorithms, making it a fundamental exercise in computer science.

Detailed Explanation

To solve this problem, we'll follow these steps:

  1. Initialize two variables: min_sum to store the minimum sum of products, and current_product to keep track of the product of selected elements.
  2. Iterate over the array from left to right, selecting every other element (i.e., 0th, 2nd, 4th, ... indices).
  3. For each selected element, multiply it with current_product.
  4. Update min_sum if the current product is smaller.
  5. Return min_sum.

Here's a step-by-step breakdown of the algorithm:

min_sum = infinity
current_product = 1

for i from 0 to n-1 (step 2):
    if i % 2 == 0:
        current_product *= arr\[i]
        min_sum = min(min_sum, current_product)

return min_sum

The time complexity of this algorithm is O(n), as we need to iterate over the array once. The space complexity is O(1), as we only use a constant amount of space to store min_sum and current_product.

Optimized Solutions

Java

View on GeeksforGeeks
public int minSumOfAlternateProduct(int[] arr) {
    int n = arr.length;
    long minSum = Long.MAX_VALUE;
    long currentProduct = 1;

    for (int i = 0; i < n; i += 2) {
        if (i % 2 == 0) {
            currentProduct *= arr[i];
            minSum = Math.min(minSum, currentProduct);
        }
    }

    return (int) minSum;
}

DevExCode

Master coding interviews with Leetcode, system design, TechBit, QuickLearn, DevTips, Tech Battles, and career services.

© 2026 DevExCode. All rights reserved.