Skip to content

Commit 5697d4b

Browse files
chore: add LeetCode daily solution
1 parent 44ba784 commit 5697d4b

File tree

5 files changed

+113
-0
lines changed

5 files changed

+113
-0
lines changed
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
# Minimum Number of Operations to Make All Array Elements Equal to 1 (Medium)
2+
3+
**Problem ID:** 2654
4+
**Date:** 2025-11-12
5+
**Link:** https://leetcode.com/problems/minimum-number-of-operations-to-make-all-array-elements-equal-to-1/
6+
7+
## Approach
8+
9+
To solve the problem of minimizing the number of operations required to make all elements of the array equal to 1, we can follow a systematic approach:
10+
11+
### Problem Breakdown
12+
1. **Understanding GCD Operations**: The operation allows us to replace either `nums[i]` or `nums[i+1]` with their greatest common divisor (GCD). This means we can reduce the values in the array, but we need to ensure that we can eventually reach the value 1 for all elements.
13+
14+
2. **Feasibility Check**: Before attempting to compute the number of operations, we must check if it's possible to make all elements equal to 1. This can be determined by calculating the GCD of the entire array:
15+
- If `gcd(nums) != 1`, it is impossible to reduce all elements to 1, and we should return -1.
16+
17+
3. **Finding Minimum Operations**: If it is feasible to make all elements equal to 1, we need to determine the minimum number of operations:
18+
- We can leverage the fact that we can replace any two adjacent elements with their GCD. The goal is to propagate the value 1 through the array.
19+
- We can iterate through the array and track the positions where we can achieve a GCD of 1. Specifically, we can look for the first occurrence of 1 and the last occurrence of 1 in the array, as these positions will dictate how many operations are needed to convert the rest of the elements to 1.
20+
21+
### Approach
22+
1. **Calculate GCD of the Array**: Use a helper function to compute the GCD of the entire array. If the result is greater than 1, return -1.
23+
24+
2. **Locate Positions of 1**: Traverse the array to find the first and last indices where the value is 1. If there are no 1s, we will need to convert elements to 1 by propagating from adjacent elements.
25+
26+
3. **Count Operations**:
27+
- If there are existing 1s, the number of operations needed can be calculated based on the distance to the nearest 1 from both ends of the array.
28+
- If there are no 1s, we will need to perform operations on adjacent pairs until we create a 1. The maximum number of operations will be determined by the length of the array minus the number of contiguous segments we can create with GCD operations.
29+
30+
### Data Structures
31+
- A simple array to hold the input values is sufficient.
32+
- Basic integer variables to track indices and counts.
33+
34+
### Complexity
35+
- **Time Complexity**: O(n) for calculating the GCD and O(n) for finding the first and last occurrences of 1, leading to an overall complexity of O(n).
36+
- **Space Complexity**: O(1) since we are using a constant amount of extra space.
37+
38+
By following this structured approach, we can efficiently determine the minimum number of operations required to make all elements of the array equal to 1 or conclude that it is impossible.
Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
import java.util.Arrays;
2+
3+
class Solution {
4+
public int minOperations(int[] nums) {
5+
if (Arrays.stream(nums).allMatch(num -> num == 1)) {
6+
return 0;
7+
}
8+
9+
int gcd = nums[0];
10+
for (int num : nums) {
11+
gcd = gcd(gcd, num);
12+
if (gcd == 1) {
13+
break;
14+
}
15+
}
16+
17+
if (gcd > 1) {
18+
return -1;
19+
}
20+
21+
int operations = 0;
22+
for (int i = 0; i < nums.length - 1; i++) {
23+
if (gcd(nums[i], nums[i + 1]) == 1) {
24+
operations++;
25+
}
26+
}
27+
28+
return operations + (nums.length - 1);
29+
}
30+
31+
private int gcd(int a, int b) {
32+
while (b != 0) {
33+
int temp = b;
34+
b = a % b;
35+
a = temp;
36+
}
37+
return a;
38+
}
39+
}
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
function minOperations(nums) {
2+
const gcd = (a, b) => b === 0 ? a : gcd(b, a % b);
3+
4+
let overallGCD = nums[0];
5+
for (let num of nums) {
6+
overallGCD = gcd(overallGCD, num);
7+
}
8+
9+
if (overallGCD !== 1) return -1;
10+
11+
let operations = 0;
12+
for (let i = 0; i < nums.length - 1; i++) {
13+
if (gcd(nums[i], nums[i + 1]) === 1) {
14+
operations += 1;
15+
}
16+
}
17+
18+
return operations + (nums.length - 1);
19+
}
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
from math import gcd
2+
from functools import reduce
3+
4+
class Solution:
5+
def minOperations(self, nums: List[int]) -> int:
6+
if reduce(gcd, nums) != 1:
7+
return -1
8+
9+
n = len(nums)
10+
operations = 0
11+
12+
for i in range(n - 1):
13+
if gcd(nums[i], nums[i + 1]) == 1:
14+
operations += 1
15+
16+
return n - 1 if operations == 0 else operations + (n - 1)

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -276,3 +276,4 @@ Through completing the Blind 75 and NeetCode 150, you will have mastered:
276276
- 2025-11-09 — [Count Operations to Obtain Zero](https://leetcode.com/problems/count-operations-to-obtain-zero/) (Easy) → `Easy/2025-11-09-2169-Count-Operations-to-Obtain-Zero`
277277
- 2025-11-10 — [Minimum Operations to Convert All Elements to Zero](https://leetcode.com/problems/minimum-operations-to-convert-all-elements-to-zero/) (Medium) → `Medium/2025-11-10-3542-Minimum-Operations-to-Convert-All-Elements-to-Zero`
278278
- 2025-11-11 — [Ones and Zeroes](https://leetcode.com/problems/ones-and-zeroes/) (Medium) → `Medium/2025-11-11-474-Ones-and-Zeroes`
279+
- 2025-11-12 — [Minimum Number of Operations to Make All Array Elements Equal to 1](https://leetcode.com/problems/minimum-number-of-operations-to-make-all-array-elements-equal-to-1/) (Medium) → `Medium/2025-11-12-2654-Minimum-Number-of-Operations-to-Make-All-Array-Elements-Equal-to-1`

0 commit comments

Comments
 (0)