Maximum sum of consecutive odd numbers
Last Updated :
16 Sep, 2023
Given an array arr[], the task is to find the difference between maximum and minimum summations of consecutive odd elements in arr[].
Note: If there are no odd numbers present, return -1.
Examples:
Input: arr[] = {1, 2, 3, 4, 3, 5}
Output: 7
Explanation: The difference is 7 between minimum summation (1) and maximum summation (3, 5) of consecutive odd numbers.
Input: arr[] = {2, 4, 4, 4, 5, 7}
Output : 0
Explanation: There is only one pair of consecutive odd elements (5, 7), so their maximum and minimum summations is 12, 12, thus the difference between |12-12| = 0
Approach: This can be solved using the following idea:
Iterate through all the indexes & whenever an odd element occurs get the summation of consecutive elements started from that index. Store the maximum of all summations and minimum in different variables.
Below is the implementation of the above approach :
- We have to iterate through each element.
- Whenever any odd element occurs, count the summation of the consecutive odd elements starting from that element.
- Store the maximum and minimum sum in different variables and return their difference
Below is the implementation of the code:
C++
#include <iostream>
#include <climits>
using namespace std;
int maxOddSum( int arr[], int n)
{
int i = 0;
int min = INT_MAX, max = INT_MIN;
while (i < n) {
if (arr[i] % 2 == 1) {
int start = i, sum = 0;
while (start < n && arr[start] % 2 == 1) {
sum += arr[start];
start++;
}
min = std::min(min, sum);
max = std::max(max, sum);
i = start;
}
else
i++;
}
if (min == INT_MAX)
return -1;
return max - min;
}
int main()
{
int arr[] = { 1, 2, 3, 4, 3, 5 };
int n = sizeof (arr) / sizeof (arr[0]);
int ans = maxOddSum(arr, n);
cout << ans << endl;
return 0;
}
|
Java
import java.io.*;
import java.util.*;
class GFG {
public static int maxOddSum( int [] arr)
{
int n = arr.length, i = 0 ;
int min = Integer.MAX_VALUE, max
= Integer.MIN_VALUE;
while (i < n) {
if (arr[i] % 2 == 1 ) {
int start = i, sum = 0 ;
while (start < n && arr[start] % 2 == 1 ) {
sum += arr[start];
start++;
}
min = Math.min(min, sum);
max = Math.max(max, sum);
i = start;
}
else
i++;
}
if (min == Integer.MAX_VALUE)
return - 1 ;
return max - min;
}
public static void main(String[] args)
{
int [] arr = { 1 , 2 , 3 , 4 , 3 , 5 };
int ans = maxOddSum(arr);
System.out.println(ans);
}
}
|
Python3
def max_odd_sum(arr):
i = 0
minimum = float ( 'inf' )
maximum = float ( '-inf' )
n = len (arr)
while i < n:
if arr[i] % 2 = = 1 :
start = i
summation = 0
while start < n and arr[start] % 2 = = 1 :
summation + = arr[start]
start + = 1
minimum = min (minimum, summation)
maximum = max (maximum, summation)
i = start
else :
i + = 1
if minimum = = float ( 'inf' ):
return - 1
return maximum - minimum
arr = [ 1 , 2 , 3 , 4 , 3 , 5 ]
ans = max_odd_sum(arr)
print (ans)
|
C#
using System;
class Program
{
static int MaxOddSum( int [] arr, int n)
{
int i = 0;
int min = int .MaxValue, max = int .MinValue;
while (i < n)
{
if (arr[i] % 2 == 1)
{
int start = i, sum = 0;
while (start < n && arr[start] % 2 == 1)
{
sum += arr[start];
start++;
}
min = Math.Min(min, sum);
max = Math.Max(max, sum);
i = start;
}
else
{
i++;
}
}
if (min == int .MaxValue)
{
return -1;
}
return max - min;
}
static void Main()
{
int [] arr = { 1, 2, 3, 4, 3, 5 };
int n = arr.Length;
int ans = MaxOddSum(arr, n);
Console.WriteLine(ans);
}
}
|
Javascript
function maxOddSum(arr, n) {
let i = 0;
let min = Number.MAX_SAFE_INTEGER, max = Number.MIN_SAFE_INTEGER;
while (i < n) {
if (arr[i] % 2 === 1) {
let start = i, sum = 0;
while (start < n && arr[start] % 2 === 1) {
sum += arr[start];
start++;
}
min = Math.min(min, sum);
max = Math.max(max, sum);
i = start;
} else {
i++;
}
}
if (min === Number.MAX_SAFE_INTEGER) {
return -1;
}
return max - min;
}
const arr = [1, 2, 3, 4, 3, 5];
const n = arr.length;
const ans = maxOddSum(arr, n);
console.log(ans);
|
Time Complexity: O(n)
Auxiliary Space: O(1)
Share your thoughts in the comments
Please Login to comment...