From 3ec5e2f96e54a6d1fdaa3809168f5fa158ad6c09 Mon Sep 17 00:00:00 2001 From: Hao Chen Date: Wed, 7 Oct 2020 17:01:01 +0800 Subject: [PATCH] New Problem Solution "1524. Number of Sub-arrays With Odd Sum" --- README.md | 1 + .../NumberOfSubArraysWithOddSum.cpp | 71 +++++++++++++++++++ 2 files changed, 72 insertions(+) create mode 100644 algorithms/cpp/numberOfSubArraysWithOddSum/NumberOfSubArraysWithOddSum.cpp diff --git a/README.md b/README.md index a40e286..58b60d3 100644 --- a/README.md +++ b/README.md @@ -17,6 +17,7 @@ LeetCode |1541|[Minimum Insertions to Balance a Parentheses String](https://leetcode.com/problems/minimum-insertions-to-balance-a-parentheses-string/) | [C++](./algorithms/cpp/minimumInsertionsToBalanceAParenthesesString/MinimumInsertionsToBalanceAParenthesesString.cpp)|Medium| |1535|[Find the Winner of an Array Game](https://leetcode.com/problems/find-the-winner-of-an-array-game/) | [C++](./algorithms/cpp/findTheWinnerOfAnArrayGame/FindTheWinnerOfAnArrayGame.cpp)|Medium| |1525|[Number of Good Ways to Split a String](https://leetcode.com/problems/number-of-good-ways-to-split-a-string/) | [C++](./algorithms/cpp/numberOfGoodWaysToSplitAString/NumberOfGoodWaysToSplitAString.cpp)|Medium| +|1524|[Number of Sub-arrays With Odd Sum](https://leetcode.com/problems/number-of-sub-arrays-with-odd-sum/) | [C++](./algorithms/cpp/numberOfSubArraysWithOddSum/NumberOfSubArraysWithOddSum.cpp)|Medium| |1513|[Number of Substrings With Only 1s](https://leetcode.com/problems/number-of-substrings-with-only-1s/) | [C++](./algorithms/cpp/numberOfSubstringsWithOnly1s/NumberOfSubstringsWithOnly1s.cpp)|Medium| |1470|[Shuffle the Array](https://leetcode.com/problems/shuffle-the-array/) | [C++](./algorithms/cpp/shuffleTheArray/ShuffleTheArray.cpp)|Easy| |1464|[Maximum Product of Two Elements in an Array](https://leetcode.com/problems/maximum-product-of-two-elements-in-an-array/) | [C++](./algorithms/cpp/maximumProductOfTwoElementsInAnArray/MaximumProductOfTwoElementsInAnArray.cpp)|Easy| diff --git a/algorithms/cpp/numberOfSubArraysWithOddSum/NumberOfSubArraysWithOddSum.cpp b/algorithms/cpp/numberOfSubArraysWithOddSum/NumberOfSubArraysWithOddSum.cpp new file mode 100644 index 0000000..55383be --- /dev/null +++ b/algorithms/cpp/numberOfSubArraysWithOddSum/NumberOfSubArraysWithOddSum.cpp @@ -0,0 +1,71 @@ +// Source : https://leetcode.com/problems/number-of-sub-arrays-with-odd-sum/ +// Author : Hao Chen +// Date : 2020-10-07 + +/***************************************************************************************************** + * + * Given an array of integers arr. Return the number of sub-arrays with odd sum. + * + * As the answer may grow large, the answer must be computed modulo 10^9 + 7. + * + * Example 1: + * + * Input: arr = [1,3,5] + * Output: 4 + * Explanation: All sub-arrays are [[1],[1,3],[1,3,5],[3],[3,5],[5]] + * All sub-arrays sum are [1,4,9,3,8,5]. + * Odd sums are [1,9,3,5] so the answer is 4. + * + * Example 2: + * + * Input: arr = [2,4,6] + * Output: 0 + * Explanation: All sub-arrays are [[2],[2,4],[2,4,6],[4],[4,6],[6]] + * All sub-arrays sum are [2,6,12,4,10,6]. + * All sub-arrays have even sum and the answer is 0. + * + * Example 3: + * + * Input: arr = [1,2,3,4,5,6,7] + * Output: 16 + * + * Example 4: + * + * Input: arr = [100,100,99,99] + * Output: 4 + * + * Example 5: + * + * Input: arr = [7] + * Output: 1 + * + * Constraints: + * + * 1 <= arr.length <= 10^5 + * 1 <= arr[i] <= 100 + ******************************************************************************************************/ + +class Solution { +public: + int numOfSubarrays(vector& arr) { + //Walk through the array, and calculate the current sum + //if current sum is odd, then all of the sum are evil previously are valid sub-array + //if current sum is evil, then all of the sum are odd previously are valid sub-array + int odd_sum_cnt=0, evil_sum_cnt=0; + int sum = 0; + long long result=0; + for (auto a : arr) { + sum += a; + if (sum % 2 == 0) { + evil_sum_cnt++; + result += odd_sum_cnt; + }else { + odd_sum_cnt++; + result += evil_sum_cnt + 1; + } + } + + return result % 1000000007; + + } +};