Number of sub arrays with odd sum
Last Updated :
07 Jan, 2024
Given an array, find the number of subarrays whose sum is odd.
Examples:
Input : arr[] = {5, 4, 4, 5, 1, 3}
Output : 12
There are possible subarrays with odd
sum. The subarrays are
1) {5} Sum = 5 (At index 0)
2) {5, 4} Sum = 9
3) {5, 4, 4} Sum = 13
4) {5, 4, 4, 5, 1} Sum = 19
5) {4, 4, 5} Sum = 13
6) {4, 4, 5, 1, 3} Sum = 17
7) {4, 5} Sum = 9
8) {4, 5, 1, 3} Sum = 13
9) {5} Sum = 5 (At index 3)
10) {5, 1, 3} Sum = 9
11) {1} Sum = 1
12) {3} Sum = 3
O(n2) time and O(1) space method [Brute Force]: We can simply generate all the possible sub-arrays and find whether the sum of all the elements in them is an odd or not. If it is odd then we will count that sub-array otherwise neglect it.
Implementation:
C++
#include <bits/stdc++.h>
using namespace std;
int countOddSum( int ar[], int n)
{
int result = 0;
for ( int i = 0; i <= n - 1; i++) {
int val = 0;
for ( int j = i; j <= n - 1; j++) {
val = val + ar[j];
if (val % 2 != 0)
result++;
}
}
return (result);
}
int main()
{
int arr[] = { 5, 4, 4, 5, 1, 3 };
int n = sizeof (arr) / sizeof (arr[0]);
cout << "The Number of Subarrays with odd sum is "
<< countOddSum(arr, n);
return (0);
}
|
C
#include <stdio.h>
int countOddSum( int ar[], int n)
{
int result = 0;
for ( int i = 0; i <= n - 1; i++) {
int val = 0;
for ( int j = i; j <= n - 1; j++) {
val = val + ar[j];
if (val % 2 != 0)
result++;
}
}
return (result);
}
int main()
{
int arr[] = { 5, 4, 4, 5, 1, 3 };
int n = sizeof (arr) / sizeof (arr[0]);
printf ( "The Number of Subarrays with odd sum is %d" ,
countOddSum(arr, n));
return (0);
}
|
Java
import java.io.*;
class GFG {
static int countOddSum( int ar[], int n)
{
int result = 0 ;
for ( int i = 0 ; i <= n - 1 ; i++) {
int val = 0 ;
for ( int j = i; j <= n - 1 ; j++) {
val = val + ar[j];
if (val % 2 != 0 )
result++;
}
}
return (result);
}
public static void main(String[] args)
{
int ar[] = { 5 , 4 , 4 , 5 , 1 , 3 };
int n = ar.length;
System.out.print(
"The Number of Subarrays with odd sum is " );
System.out.println(countOddSum(ar, n));
}
}
|
Python3
def countOddSum(ar, n):
result = 0
for i in range (n):
val = 0
for j in range (i, n ):
val = val + ar[j]
if (val % 2 ! = 0 ):
result + = 1
return (result)
ar = [ 5 , 4 , 4 , 5 , 1 , 3 ]
print ( "The Number of Subarrays" ,
"with odd" , end = "")
print ( " sum is " + str (countOddSum(ar, 6 )))
|
C#
using System;
class GFG
{
static int countOddSum( int []ar,
int n)
{
int result = 0;
for ( int i = 0;
i <= n - 1; i++)
{
int val = 0;
for ( int j = i;
j <= n - 1; j++)
{
val = val + ar[j];
if (val % 2 != 0)
result++;
}
}
return (result);
}
public static void Main()
{
int []ar = {5, 4, 4, 5, 1, 3};
int n = ar.Length;
Console.Write( "The Number of Subarrays" +
" with odd sum is " );
Console.WriteLine(countOddSum(ar, n));
}
}
|
Javascript
<script>
function countOddSum(ar, n)
{
let result = 0;
for (let i = 0; i <= n - 1; i++)
{
let val = 0;
for (let j = i; j <= n - 1; j++)
{
val = val + ar[j];
if (val % 2 != 0)
result++;
}
}
return (result);
}
let ar = [ 5, 4, 4, 5, 1, 3 ];
let n = ar.length;
document.write( "The Number of Subarrays" +
" with odd sum is " );
document.write(countOddSum(ar, n));
</script>
|
PHP
<?php
function countOddSum(& $ar , $n )
{
$result = 0;
for ( $i = 0; $i <= $n - 1; $i ++)
{
$val = 0;
for ( $j = $i ;
$j <= $n - 1; $j ++)
{
$val = $val + $ar [ $j ];
if ( $val % 2 != 0)
$result ++;
}
}
return ( $result );
}
$ar = array (5, 4, 4, 5, 1, 3);
$n = sizeof( $ar );
echo "The Number of Subarrays with odd " ;
echo "sum is " .countOddSum( $ar , $n );
?>
|
Output
The Number of Subarrays with odd sum is 12
Complexity Analysis:
- Time Complexity: O(n2)
- Auxiliary Space: O(1)
O(n) Time and O(1) Space Method [Efficient]: If we do compute the cumulative sum array in temp[] of our input array, then we can see that the sub-array starting from i and ending at j, has an even sum if temp[] if (temp[j] – temp[i]) % 2 = 0. So, instead of building a cumulative sum array, we build a cumulative sum modulo 2 array. Then calculating odd-even pairs will give the required result i.e. temp[0]*temp[1].
C++
#include <bits/stdc++.h>
using namespace std;
int countOddSum( int ar[], int n)
{
int temp[2] = { 1, 0 };
int result = 0, val = 0;
for ( int i = 0; i <= n - 1; i++) {
val = ((val + ar[i]) % 2 + 2) % 2;
temp[val]++;
}
result = (temp[0] * temp[1]);
return (result);
}
int main()
{
int ar[] = { 5, 4, 4, 5, 1, 3 };
int n = sizeof (ar) / sizeof (ar[0]);
cout << "The Number of Subarrays with odd sum is "
<< countOddSum(ar, n);
return (0);
}
|
C
#include <stdio.h>
int countOddSum( int ar[], int n)
{
int temp[2] = { 1, 0 };
int result = 0, val = 0;
for ( int i = 0; i <= n - 1; i++) {
val = ((val + ar[i]) % 2 + 2) % 2;
temp[val]++;
}
result = (temp[0] * temp[1]);
return (result);
}
int main()
{
int ar[] = { 5, 4, 4, 5, 1, 3 };
int n = sizeof (ar) / sizeof (ar[0]);
printf ( "The Number of Subarrays with odd sum is %d" ,
countOddSum(ar, n));
return (0);
}
|
Java
import java.io.*;
class GFG {
static int countOddSum( int ar[], int n)
{
int temp[] = { 1 , 0 };
int result = 0 , val = 0 ;
for ( int i = 0 ; i <= n - 1 ; i++) {
val = ((val + ar[i]) % 2 + 2 ) % 2 ;
temp[val]++;
}
result = temp[ 0 ] * temp[ 1 ];
return (result);
}
public static void main(String[] args)
{
int ar[] = { 5 , 4 , 4 , 5 , 1 , 3 };
int n = ar.length;
System.out.println(
"The Number of Subarrays with odd sum is "
+ countOddSum(ar, n));
}
}
|
Python3
def countOddSum(ar, n):
temp = [ 1 , 0 ]
result = 0
val = 0
for i in range (n):
val = ((val + ar[i]) % 2 + 2 ) % 2
temp[val] + = 1
result = (temp[ 0 ] * temp[ 1 ])
return (result)
ar = [ 5 , 4 , 4 , 5 , 1 , 3 ]
print ( "The Number of Subarrays"
" with odd sum is " +
str (countOddSum(ar, 6 )))
|
C#
using System;
class GFG
{
static int countOddSum( int [] ar,
int n)
{
int [] temp = { 1, 0 };
int result = 0, val = 0;
for ( int i = 0; i <= n - 1; i++)
{
val = ((val + ar[i]) % 2 + 2) % 2;
temp[val]++;
}
result = temp[0] * temp[1];
return (result);
}
public static void Main()
{
int [] ar = { 5, 4, 4, 5, 1, 3 };
int n = ar.Length;
Console.Write( "The Number of Subarrays" +
" with odd sum is " +
countOddSum(ar, n));
}
}
|
Javascript
<script>
function countOddSum(ar, n)
{
let temp = [1, 0];
let result = 0, val = 0;
for (let i = 0; i <= n - 1; i++)
{
val = ((val + ar[i]) % 2 + 2) % 2;
temp[val]++;
}
result = temp[0] * temp[1];
return (result);
}
let ar = [ 5, 4, 4, 5, 1, 3 ];
let n = ar.length;
document.write( "The Number of Subarrays" +
" with odd sum is " +
countOddSum(ar, n));
</script>
|
PHP
<?php
function countOddSum( $ar , $n )
{
$temp = array (1, 0);
$result = 0;
$val = 0;
for ( $i = 0; $i <= $n - 1; $i ++)
{
$val = (( $val + $ar [ $i ]) %
2 + 2) % 2;
$temp [ $val ]++;
}
$result = ( $temp [0] * $temp [1]);
return ( $result );
}
$ar = array (5, 4, 4, 5, 1, 3);
$n = sizeof( $ar );
echo "The Number of Subarrays with odd" .
" sum is " .countOddSum( $ar , $n );
?>
|
Output
The Number of Subarrays with odd sum is 12
Complexity Analysis:
- Time Complexity: O(n)
- Auxiliary Space: O(1)
Another efficient approach is to first find the number of subarrays starting at index 0 and having an odd sum. Then traverse the array and update the number of subarrays starting at index i and having an odd sum.
Implementation:
C++
#include <bits/stdc++.h>
using namespace std;
int countOddSum( int a[], int n)
{
int odd = 0, c_odd = 0, result = 0;
for ( int i = 0; i < n; i++) {
if (a[i] & 1)
odd = !odd;
if (odd)
c_odd++;
}
for ( int i = 0; i < n; i++) {
result += c_odd;
if (a[i] & 1)
c_odd = (n - i - c_odd);
}
return result;
}
int main()
{
int ar[] = { 5, 4, 4, 5, 1, 3 };
int n = sizeof (ar) / sizeof (ar[0]);
cout << "The Number of Subarrays with odd sum is "
<< countOddSum(ar, n);
return (0);
}
|
C
#include <stdio.h>
int countOddSum( int a[], int n)
{
int odd = 0, c_odd = 0, result = 0;
for ( int i = 0; i < n; i++) {
if (a[i] & 1)
odd = !odd;
if (odd)
c_odd++;
}
for ( int i = 0; i < n; i++) {
result += c_odd;
if (a[i] & 1)
c_odd = (n - i - c_odd);
}
return result;
}
int main()
{
int ar[] = { 5, 4, 4, 5, 1, 3 };
int n = sizeof (ar) / sizeof (ar[0]);
printf ( "The Number of Subarrays with odd sum is %d" ,
countOddSum(ar, n));
return (0);
}
|
Java
import java.util.*;
class GFG {
static int countOddSum( int a[], int n)
{
int c_odd = 0 , result = 0 ;
boolean odd = false ;
for ( int i = 0 ; i < n; i++) {
if (a[i] % 2 == 1 )
odd = !odd;
if (odd)
c_odd++;
}
for ( int i = 0 ; i < n; i++) {
result += c_odd;
if (a[i] % 2 == 1 )
c_odd = (n - i - c_odd);
}
return result;
}
public static void main(String[] args)
{
int ar[] = { 5 , 4 , 4 , 5 , 1 , 3 };
int n = ar.length;
System.out.print(
"The Number of Subarrays with odd sum is "
+ countOddSum(ar, n));
}
}
|
Python3
def countOddSum(a, n):
c_odd = 0 ;
result = 0 ;
odd = False ;
for i in range (n):
if (a[i] % 2 = = 1 ):
if (odd = = True ):
odd = False ;
else :
odd = True ;
if (odd):
c_odd + = 1 ;
for i in range (n):
result + = c_odd;
if (a[i] % 2 = = 1 ):
c_odd = (n - i - c_odd);
return result;
if __name__ = = '__main__' :
ar = [ 5 , 4 , 4 , 5 , 1 , 3 ];
n = len (ar);
print ( "The Number of Subarrays" +
"with odd sum is " ,
countOddSum(ar, n));
|
C#
using System;
public class GFG{
static int countOddSum( int []a, int n)
{
int c_odd = 0, result = 0;
bool odd = false ;
for ( int i = 0; i < n; i++)
{
if (a[i] % 2 == 1)
{
odd = !odd;
}
if (odd)
{
c_odd++;
}
}
for ( int i = 0; i < n; i++)
{
result += c_odd;
if (a[i] % 2 == 1)
{
c_odd = (n - i - c_odd);
}
}
return result;
}
public static void Main(String[] args)
{
int []ar = { 5, 4, 4, 5, 1, 3 };
int n = ar.Length;
Console.Write( "The Number of Subarrays " +
"with odd sum is " +
countOddSum(ar, n));
}
}
|
Javascript
<script>
function countOddSum(a, n)
{
let c_odd = 0, result = 0;
let odd = false ;
for (let i = 0; i < n; i++)
{
if (a[i] % 2 == 1)
{
odd = !odd;
}
if (odd)
{
c_odd++;
}
}
for (let i = 0; i < n; i++)
{
result += c_odd;
if (a[i] % 2 == 1)
{
c_odd = (n - i - c_odd);
}
}
return result;
}
let ar = [ 5, 4, 4, 5, 1, 3 ];
let n = ar.length;
document.write( "The Number of Subarrays " +
"with odd sum is " +
countOddSum(ar, n));
</script>
|
Output
The Number of Subarrays with odd sum is 12
Complexity Analysis:
- Time Complexity: O(n)
- Auxiliary Space: O(1)
Share your thoughts in the comments
Please Login to comment...