| | | 1 | | // -------------------------------------------------------------------------------- |
| | | 2 | | // Copyright (C) 2026 Eugene Eremeev (also known as Yevhenii Yeriemeieiv). |
| | | 3 | | // All Rights Reserved. |
| | | 4 | | // -------------------------------------------------------------------------------- |
| | | 5 | | // This software is the confidential and proprietary information of Eugene Eremeev |
| | | 6 | | // (also known as Yevhenii Yeriemeieiv) ("Confidential Information"). You shall not |
| | | 7 | | // disclose such Confidential Information and shall use it only in accordance with |
| | | 8 | | // the terms of the license agreement you entered into with Eugene Eremeev (also |
| | | 9 | | // known as Yevhenii Yeriemeieiv). |
| | | 10 | | // -------------------------------------------------------------------------------- |
| | | 11 | | |
| | | 12 | | namespace LeetCode.Algorithms.NumberOfSubArraysWithOddSum; |
| | | 13 | | |
| | | 14 | | /// <inheritdoc /> |
| | | 15 | | public sealed class NumberOfSubArraysWithOddSumBruteForce : INumberOfSubArraysWithOddSum |
| | | 16 | | { |
| | | 17 | | private const int Mod = (int)(1e9 + 7); |
| | | 18 | | |
| | | 19 | | /// <summary> |
| | | 20 | | /// Time complexity - O(n^2) |
| | | 21 | | /// Space complexity - O(1) |
| | | 22 | | /// </summary> |
| | | 23 | | /// <param name="arr"></param> |
| | | 24 | | /// <returns></returns> |
| | | 25 | | public int NumOfSubarrays(int[] arr) |
| | 3 | 26 | | { |
| | 3 | 27 | | var count = 0; |
| | | 28 | | |
| | 32 | 29 | | for (var i = 0; i < arr.Length; i++) |
| | 13 | 30 | | { |
| | 13 | 31 | | var currentSum = 0; |
| | | 32 | | |
| | 106 | 33 | | for (var j = i; j < arr.Length; j++) |
| | 40 | 34 | | { |
| | 40 | 35 | | currentSum += arr[j]; |
| | | 36 | | |
| | 40 | 37 | | if (currentSum % 2 != 0) |
| | 20 | 38 | | { |
| | 20 | 39 | | count++; |
| | 20 | 40 | | } |
| | 40 | 41 | | } |
| | 13 | 42 | | } |
| | | 43 | | |
| | 3 | 44 | | return count % Mod; |
| | 3 | 45 | | } |
| | | 46 | | } |