Skip to content

Commit bc6e553

Browse files
authored
Merge pull request codeharborhub#3240 from ImmidiSivani/leetcode-3191
added solution to 3191
2 parents 8a99150 + c2573ef commit bc6e553

File tree

1 file changed

+151
-0
lines changed

1 file changed

+151
-0
lines changed
Lines changed: 151 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,151 @@
1+
---
2+
id: minimum-operations-to-make-binary-array-elements-equal-to-one-I
3+
title: Minimum Operations to Make Binary Array Elements Equal to One I
4+
sidebar_label: 3191-Minimum Operations to Make Binary Array Elements Equal to One I
5+
tags:
6+
- Array
7+
- Sliding Window
8+
- Prefix Sum
9+
10+
description: "This is a solution to the Minimum Operations to Make Binary Array Elements Equal to One I in leetcode"
11+
---
12+
13+
## Problem Description
14+
15+
You are given a binary array nums.
16+
17+
You can do the following operation on the array any number of times (possibly zero):
18+
19+
- Choose any 3 consecutive elements from the array and flip all of them.
20+
Flipping an element means changing its value from 0 to 1, and from 1 to 0.
21+
22+
Return the minimum number of operations required to make all elements in nums equal to 1. If it is impossible, return -1.
23+
24+
25+
26+
### Examples
27+
28+
**Example 1:**
29+
30+
```
31+
Input: nums = [0,1,1,1,0,0]
32+
Output: 3
33+
Explanation:
34+
We can do the following operations:
35+
Choose the elements at indices 0, 1 and 2. The resulting array is nums = [1,0,0,1,0,0].
36+
Choose the elements at indices 1, 2 and 3. The resulting array is nums = [1,1,1,0,0,0].
37+
Choose the elements at indices 3, 4 and 5. The resulting array is nums = [1,1,1,1,1,1].
38+
39+
```
40+
**Example 2:**
41+
```
42+
Input: nums = [0,1,1,1]
43+
44+
Output: -1
45+
46+
Explanation:
47+
It is impossible to make all elements equal to 1.
48+
49+
50+
```
51+
## Complexity Analysis
52+
53+
*** Time Complexity:** $O(n)$
54+
55+
*** Space Complexity:** $O(1)$
56+
57+
### Constraints
58+
59+
- `3 <= nums.length <= 105`
60+
- `3 <= nums.length <= 105`
61+
62+
63+
64+
### Solution
65+
## Approach
66+
The approach involves iterating through the array and flipping segments of three consecutive elements whenever a 0 is encountered, starting from the beginning. For each 0 at index "i", the elements at "i", "i+1", and "i+2" are flipped (0 to 1, and 1 to 0), and an operation counter is incremented. After processing up to the third last element, the array is checked from the third last to the last element to ensure no 0s remain. If any 0s are found, return -1, indicating it is impossible to make all elements 1; otherwise, return the counter representing the number of operations performed.
67+
68+
## Code in Different Languages
69+
70+
<Tabs>
71+
<TabItem value="cpp" label="C++">
72+
<SolutionAuthor name="@ImmidiSivani"/>
73+
74+
```cpp
75+
class Solution {
76+
public:
77+
int minOperations(std::vector<int>& nums) {
78+
int c = 0;
79+
for (int i = 0; i <= nums.size() - 3; ++i) {
80+
if (nums[i] == 0) {
81+
nums[i] = 1 - nums[i];
82+
nums[i + 1] = 1 - nums[i + 1];
83+
nums[i + 2] = 1 - nums[i + 2];
84+
c++;
85+
}
86+
}
87+
for (int i = nums.size() - 3; i < nums.size(); ++i) {
88+
if (nums[i] == 0)
89+
return -1;
90+
}
91+
return c;
92+
}
93+
};
94+
95+
96+
```
97+
</TabItem>
98+
<TabItem value="java" label="Java">
99+
<SolutionAuthor name="@ImmidiSivani"/>
100+
101+
```java
102+
class Solution {
103+
public int minOperations(int[] nums) {
104+
int c=0;
105+
for(int i=0;i<=nums.length-3;++i){
106+
if(nums[i]==0){
107+
nums[i]=1-nums[i];
108+
nums[i+1]=1-nums[i+1];
109+
nums[i+2]=1-nums[i+2];
110+
c++;
111+
}
112+
}
113+
for(int i=nums.length-3;i<nums.length;++i){
114+
if(nums[i]==0)
115+
return -1;
116+
}
117+
return c;
118+
}
119+
}
120+
```
121+
</TabItem>
122+
<TabItem value="python" label="Python">
123+
<SolutionAuthor name="@ImmidiSivani"/>
124+
125+
```python
126+
class Solution:
127+
def minOperations(self, nums: list[int]) -> int:
128+
c = 0
129+
for i in range(len(nums) - 2):
130+
if nums[i] == 0:
131+
nums[i] = 1 - nums[i]
132+
nums[i + 1] = 1 - nums[i + 1]
133+
nums[i + 2] = 1 - nums[i + 2]
134+
c += 1
135+
136+
for i in range(len(nums) - 3, len(nums)):
137+
if nums[i] == 0:
138+
return -1
139+
140+
return c
141+
142+
143+
```
144+
</TabItem>
145+
</Tabs>
146+
147+
## References
148+
149+
- **LeetCode Problem**: [Minimum Operations to Make Binary Array Elements Equal to One I](https://leetcode.com/problems/minimum-operations-to-make-binary-array-elements-equal-to-one-i/description/)
150+
151+
- **Solution Link**: [Minimum Operations to Make Binary Array Elements Equal to One I](https://leetcode.com/problems/minimum-operations-to-make-binary-array-elements-equal-to-one-i/post-solution/?submissionId=1296819334)

0 commit comments

Comments
 (0)