File tree 3 files changed +52
-0
lines changed
solution/2400-2499/2447.Number of Subarrays With GCD Equal to K
3 files changed +52
-0
lines changed Original file line number Diff line number Diff line change @@ -145,7 +145,24 @@ func gcd(a, b int) int {
145
145
### ** TypeScript**
146
146
147
147
``` ts
148
+ function subarrayGCD(nums : number [], k : number ): number {
149
+ const n = nums .length ;
150
+ let ans = 0 ;
151
+ for (let i = 0 ; i < n ; i ++ ) {
152
+ let x = nums [i ];
153
+ for (let j = i ; j < n ; j ++ ) {
154
+ x = gcd (nums [j ], x );
155
+ if (x == k ) ans += 1 ;
156
+ }
157
+ }
158
+ return ans ;
159
+ };
148
160
161
+ function gcd(a : number , b : number ): number {
162
+ if (a > b ) [a , b ] = [b , a ];
163
+ if (a == 0 ) return b ;
164
+ return gcd (b % a , a );
165
+ }
149
166
```
150
167
151
168
### ** ...**
Original file line number Diff line number Diff line change @@ -131,7 +131,24 @@ func gcd(a, b int) int {
131
131
### ** TypeScript**
132
132
133
133
``` ts
134
+ function subarrayGCD(nums : number [], k : number ): number {
135
+ const n = nums .length ;
136
+ let ans = 0 ;
137
+ for (let i = 0 ; i < n ; i ++ ) {
138
+ let x = nums [i ];
139
+ for (let j = i ; j < n ; j ++ ) {
140
+ x = gcd (nums [j ], x );
141
+ if (x == k ) ans += 1 ;
142
+ }
143
+ }
144
+ return ans ;
145
+ };
134
146
147
+ function gcd(a : number , b : number ): number {
148
+ if (a > b ) [a , b ] = [b , a ];
149
+ if (a == 0 ) return b ;
150
+ return gcd (b % a , a );
151
+ }
135
152
```
136
153
137
154
### ** ...**
Original file line number Diff line number Diff line change
1
+ function subarrayGCD ( nums : number [ ] , k : number ) : number {
2
+ const n = nums . length ;
3
+ let ans = 0 ;
4
+ for ( let i = 0 ; i < n ; i ++ ) {
5
+ let x = nums [ i ] ;
6
+ for ( let j = i ; j < n ; j ++ ) {
7
+ x = gcd ( nums [ j ] , x ) ;
8
+ if ( x == k ) ans += 1 ;
9
+ }
10
+ }
11
+ return ans ;
12
+ } ;
13
+
14
+ function gcd ( a : number , b : number ) : number {
15
+ if ( a > b ) [ a , b ] = [ b , a ] ;
16
+ if ( a == 0 ) return b ;
17
+ return gcd ( b % a , a ) ;
18
+ }
You can’t perform that action at this time.
0 commit comments