diff --git a/solution/2400-2499/2436.Minimum Split Into Subarrays With GCD Greater Than One/README.md b/solution/2400-2499/2436.Minimum Split Into Subarrays With GCD Greater Than One/README.md index 5e41c1d04f2dd..34c62dfbd1779 100644 --- a/solution/2400-2499/2436.Minimum Split Into Subarrays With GCD Greater Than One/README.md +++ b/solution/2400-2499/2436.Minimum Split Into Subarrays With GCD Greater Than One/README.md @@ -187,6 +187,33 @@ function gcd(a: number, b: number): number { } ``` +#### Rust + +```rust +impl Solution { + pub fn minimum_splits(nums: Vec) -> i32 { + let mut ans = 1; + let mut g = 0; + for &x in &nums { + g = Self::gcd(g, x); + if g == 1 { + ans += 1; + g = x; + } + } + ans + } + + fn gcd(a: i32, b: i32) -> i32 { + if b == 0 { + a + } else { + Self::gcd(b, a % b) + } + } +} +``` + diff --git a/solution/2400-2499/2436.Minimum Split Into Subarrays With GCD Greater Than One/README_EN.md b/solution/2400-2499/2436.Minimum Split Into Subarrays With GCD Greater Than One/README_EN.md index 265626b0a1d8d..7932dffee9b58 100644 --- a/solution/2400-2499/2436.Minimum Split Into Subarrays With GCD Greater Than One/README_EN.md +++ b/solution/2400-2499/2436.Minimum Split Into Subarrays With GCD Greater Than One/README_EN.md @@ -183,6 +183,33 @@ function gcd(a: number, b: number): number { } ``` +#### Rust + +```rust +impl Solution { + pub fn minimum_splits(nums: Vec) -> i32 { + let mut ans = 1; + let mut g = 0; + for &x in &nums { + g = Self::gcd(g, x); + if g == 1 { + ans += 1; + g = x; + } + } + ans + } + + fn gcd(a: i32, b: i32) -> i32 { + if b == 0 { + a + } else { + Self::gcd(b, a % b) + } + } +} +``` + diff --git a/solution/2400-2499/2436.Minimum Split Into Subarrays With GCD Greater Than One/Solution.rs b/solution/2400-2499/2436.Minimum Split Into Subarrays With GCD Greater Than One/Solution.rs new file mode 100644 index 0000000000000..83319cebeb72a --- /dev/null +++ b/solution/2400-2499/2436.Minimum Split Into Subarrays With GCD Greater Than One/Solution.rs @@ -0,0 +1,22 @@ +impl Solution { + pub fn minimum_splits(nums: Vec) -> i32 { + let mut ans = 1; + let mut g = 0; + for &x in &nums { + g = Self::gcd(g, x); + if g == 1 { + ans += 1; + g = x; + } + } + ans + } + + fn gcd(a: i32, b: i32) -> i32 { + if b == 0 { + a + } else { + Self::gcd(b, a % b) + } + } +}