702661. Maximum value

Easy

Maximum Value

Slug: maximum-value5946 Difficulty: Easy Id: 702661 Topic Tags: Arrays, Dynamic Programming, Data Structures, Algorithms Company Tags: None

Summary

The problem is to find the maximum value in a given array. The key concept involved is dynamic programming, which allows us to solve this problem efficiently.

Detailed Explanation

To solve this problem, we can use dynamic programming. We initialize our maximum value to the first element of the array. Then, for each subsequent element in the array, we check if it's greater than our current maximum. If it is, we update our maximum value. This process continues until we've checked all elements in the array.

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

  1. Initialize max_value to the first element of the array.
  2. Iterate through the remaining elements in the array.
  3. For each element, check if it's greater than max_value.
  4. If it is, update max_value.
  5. Return max_value.

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

Space complexity: O(1), since we only use a constant amount of space to store our maximum value.

Optimized Solutions

Java

View on GeeksforGeeks
public int findMax(int[] arr) {
    int max = arr[0];
    for (int i = 1; i < arr.length; i++) {
        if (arr[i] > max) {
            max = arr[i];
        }
    }
    return max;
}

DevExCode

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

© 2026 DevExCode. All rights reserved.