702864. Max Odd Sum

Basic

Max Odd Sum

Slug: max-odd-sum0651 Difficulty: Basic Id: 702864 Topic Tags: Arrays, Data Structures Company Tags: None

Summary

The problem is about finding the maximum sum of odd numbers in a given array. The algorithm needs to iterate through the array, identify and add the odd numbers, and then keep track of the maximum sum found so far.

Detailed Explanation

To solve this problem, we can start by initializing two variables: max_sum to store the maximum sum found so far, and current_sum to store the sum of odd numbers encountered. We iterate through the array, checking each element if it's odd or even. If an element is odd, we add it to current_sum. If an element is even, we reset current_sum to 0.

Here's a step-by-step breakdown:

  1. Initialize max_sum and current_sum.
  2. Iterate through the array:
    • Check each element if it's odd or even.
    • If the element is odd, add it to current_sum. If the element is even, reset current_sum to 0.
  3. Update max_sum if current_sum is greater than max_sum.
  4. Return max_sum.

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

Space complexity: O(1), as we only use a constant amount of space to store the variables.

Optimized Solutions

Java

View on GeeksforGeeks
public int maxOddSum(int[] arr) {
    int max_sum = 0;
    int current_sum = 0;

    for (int num : arr) {
        if (num % 2 != 0) { // Check if number is odd
            current_sum += num;
        } else {
            current_sum = 0;
        }
        max_sum = Math.max(max_sum, current_sum);
    }

    return max_sum;
}

DevExCode

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

© 2026 DevExCode. All rights reserved.