-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path1524-NumberOfSubArraysWithOddSum.go
More file actions
82 lines (70 loc) · 2.36 KB
/
1524-NumberOfSubArraysWithOddSum.go
File metadata and controls
82 lines (70 loc) · 2.36 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
package main
// 1524. Number of Sub-arrays With Odd Sum
// Given an array of integers arr, return the number of subarrays with an odd sum.
// Since the answer can be very large, return it modulo 10^9 + 7.
// Example 1:
// Input: arr = [1,3,5]
// Output: 4
// Explanation: All subarrays 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 subarrays 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
// Constraints:
// 1 <= arr.length <= 10^5
// 1 <= arr[i] <= 100
import "fmt"
func numOfSubarrays(arr []int) int {
index, tmp := 0, []int{ 1, 0 }
for _, v := range arr {
index = ((index + v) % 2 + 2) % 2
tmp[index]++
}
return (tmp[0] * tmp[1]) % 1_000_000_007
}
func numOfSubarrays1(arr []int) int {
sum, odd, even := 0, 0, 1 // empty list is even number
for _, v := range arr {
sum += v
if sum & 1 == 1 {
odd++
} else {
even++
}
}
return (odd * even) % 1_000_000_007
}
func main() {
// Example 1:
// Input: arr = [1,3,5]
// Output: 4
// Explanation: All subarrays 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.
fmt.Println(numOfSubarrays([]int{1,3,5})) // 4
// Example 2:
// Input: arr = [2,4,6]
// Output: 0
// Explanation: All subarrays 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.
fmt.Println(numOfSubarrays([]int{2,4,6})) // 0
// Example 3:
// Input: arr = [1,2,3,4,5,6,7]
// Output: 16
fmt.Println(numOfSubarrays([]int{1,2,3,4,5,6,7})) // 16
fmt.Println(numOfSubarrays([]int{1,2,3,4,5,6,7,8,9})) // 25
fmt.Println(numOfSubarrays([]int{9,8,7,6,5,4,3,2,1})) // 25
fmt.Println(numOfSubarrays1([]int{1,3,5})) // 4
fmt.Println(numOfSubarrays1([]int{2,4,6})) // 0
fmt.Println(numOfSubarrays1([]int{1,2,3,4,5,6,7})) // 16
fmt.Println(numOfSubarrays1([]int{1,2,3,4,5,6,7,8,9})) // 25
fmt.Println(numOfSubarrays1([]int{9,8,7,6,5,4,3,2,1})) // 25
}