703728. Maximum value of expression

Easy

Maximum value of expression

Slug: maximum-value-of-expression2515 Difficulty: Easy Id: 703728 Topic Tags: Arrays, Mathematical, Data Structures, Algorithms Company Tags: None

Summary

The problem is about finding the maximum possible value that can be obtained by applying certain operations on an array of integers. The operations allowed are addition and multiplication between adjacent elements in the array. This problem involves basic mathematical concepts and array manipulation.

Detailed Explanation

To solve this problem, we need to find the maximum possible value that can be obtained by performing the given operations.

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

  1. Initialize maxVal as the first element of the array.
  2. Iterate through the array starting from the second element (index 1).
  3. For each element, check if multiplying it with the previous element gives a larger value than adding it to the previous element. If so, update maxVal.
  4. Return maxVal as the maximum possible value.

Here's an ASCII art diagram of the array:

[ A0 ] -> [ A1 ] -> ... -> [ An-1 ] -> [ An ]
  ^         ^        ...          ^
  |         |        ...          |
  +--------+

Time complexity: O(n), where n is the size of the array.

Space complexity: O(1), as we only need a constant amount of space to store maxVal.

Optimized Solutions

Java

View on GeeksforGeeks
int maximumValue(int[] arr) {
    int maxVal = arr[0];
    for (int i = 1; i < arr.length; i++) {
        if (arr[i] * maxVal > arr[i] + maxVal) {
            maxVal *= arr[i];
        } else {
            maxVal += arr[i];
        }
    }
    return maxVal;
}

DevExCode

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

© 2026 DevExCode. All rights reserved.