- Trending Categories
- Data Structure
- Networking
- RDBMS
- Operating System
- Java
- iOS
- HTML
- CSS
- Android
- Python
- C Programming
- C++
- C#
- MongoDB
- MySQL
- Javascript
- PHP

- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who

Subarrays are the contiguous part of an array. For example, we consider an array [5, 6, 7, 8], then there are ten non-empty subarrays like (5), (6), (7), (8), (5, 6), (6,7), (7,8), (5,6,7), (6,7,8) and (5,6,7,8).

In this guide, we will explain every possible information to find the number of subarrays with odd sums in C++. For finding the number of subarrays with the odd sum, we can use different approaches, so here is a simple example for it −

Input : array = {9,8,7,6,5} Output : 9 Explanation : Sum of subarray - {9} = 9 {7} = 7 {5} = 5 {9,8} = 17 {8,7} = 15 {7,6} = 13 {6,5} = 11 {8,7,6} = 21 {9,8,7,6,5} = 35

With this approach we can simply check whether the sum of elements in all subarrays is even or odd, If it is even we will reject that subarray and will count subarrays with sum odd, It is not an efficient approach as complexity of this code is O(n^{2}).

#include <bits/stdc++.h> using namespace std; int main(){ int n=5, temp = 0; int a[n-1] = { 9,8,7,6,5 } ; // declaring our array. int cnt = 0; // counter variable. for(int i = 0; i < n; i++){ temp = 0; // refreshing our temp sum. for(int j = i; j < n; j++){ // this loop will make our subarrays starting from i till n-1. temp = temp + a[j]; if( temp % 2 == 1 ) cnt++; } } cout << "Number of subarrays with odd sum : " << cnt << "\n"; return 0; }

Number of subarrays with odd sum : 9

Nested loops are used in this code where the outer loop is used to increment the value of I, which is pointing at each value of the array from starting; the inner loop is used to find subarray starting from the position **" i "** having odd sum.

In this approach, we are processing every element from the 0th position in the array. If the current element is odd, increase an odd counter and increase an even counter for every even number. If we found an odd number, then swap the values of even and odd because adding an odd number to the subarray will change its parity and finally add a count to the result. The complexity of this code is O(n), as we are processing every element.

#include <bits/stdc++.h> using namespace std; int main(){ int odd = 0, even = 0, result = 0,n=5,i,temp; int arr[ n-1 ] = { 9,8,7,6,5}; // initialising the array // for loop for processing every element of array for ( i = 0 ; i < n ; i ++ ) { if ( arr[ i ] % 2 == 0 ) { even++; } else { // swapping even odd values temp = even; even = odd; odd = temp + 1; } result += odd; } cout << "Number of subarrays with odd sum : " << result; }

Number of subarrays with odd sum : 9

In this code, we check every element for even/odd and increment even counter for even number and odd counter for an odd number. Also, we are swapping odd-even counter values if an odd number is found; otherwise, it will change the parity of the subarray. Then adding the value of the odd counter to the result variable after every iteration.

In this article, we explained how to find the number of subarrays with sum odd from the Brute force approach, which is generating every subarray with sum odd and incrementing the count. The time complexity of this code is O(n2). An efficient approach is going through each element of the array and increment odd/even counter variables with every odd/even number found and swapping counters if an odd number is found; the time complexity of this code is O(n). Hope you find this article helpful in understanding the problem and solution.

- Related Questions & Answers
- Find the Number of Subarrays with m Odd Numbers using C++
- Find number of subarrays with even sum in C++
- Find the Number of subarrays having sum less than K using C++
- Find sum of odd factors of a number using C++.
- Find the Number Of Subarrays Having Sum in a Given Range using C++
- Program to find sum of all odd length subarrays in Python
- Program to find number of sub-arrays with odd sum using Python
- Find the Number of subarrays having sum of the form k^m, m >= 0 using C++
- Find the Number With Even Sum of Digits using C++
- Find the Number Of Subarrays Having Sum in a Given Range in C++
- Program to find maximum number of non-overlapping subarrays with sum equals target using Python
- Sum of All Possible Odd Length Subarrays in JavaScript
- C Program for Find sum of odd factors of a number?
- C++ program for Find sum of odd factors of a number
- Find the number of subarrays have bitwise OR >= K using C++

Advertisements