Skip to content

Commit 8c202f0

Browse files
committed
added Minimum Operations to Make Product Divisible by k
1 parent 30b6bde commit 8c202f0

File tree

1 file changed

+134
-1
lines changed

1 file changed

+134
-1
lines changed

docs/cp31/1000.md

Lines changed: 134 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
# 1000 Rated problems
22

33
1. [Binary String Transformation Problem](#binary-string-transformation-problem)
4+
1. [Minimum Operations to Make Product Divisible by k](#minimum-operations-to-make-product-divisible-by-k)
45

56

67
## Binary String Transformation Problem
@@ -144,4 +145,136 @@ void solve() {
144145
- Successfully placed: 2 characters
145146
- Must delete: 5 - 2 = **3 characters**
146147

147-
**Result:** cost = **3**
148+
**Result:** cost = **3**
149+
150+
## Minimum Operations to Make Product Divisible by k
151+
152+
### 1. Problem Summary
153+
154+
Given an array of n integers and a number k (2 ≤ k ≤ 5), find the minimum number of operations needed to make the product of all array elements divisible by k. In each operation, you can increment any element by 1.
155+
156+
**Input/Output Format:**
157+
- Input: Integer n (array size), integer k (divisor), followed by n integers
158+
- Output: Minimum number of operations required
159+
160+
**Key Constraints:**
161+
- 2 ≤ k ≤ 5 (k can only be 2, 3, 4, or 5)
162+
- 1 ≤ n ≤ 10^5
163+
- This limited range of k is crucial for the solution approach
164+
165+
### 2. Approach & Intuition
166+
167+
**Key Insight:**
168+
The product a₁ · a₂ · ... · aₙ is divisible by k if and only if the product contains all prime factors of k with sufficient multiplicity.
169+
170+
**Why This Works:**
171+
- For k = 2, 3, 5 (primes): We need at least one element divisible by k
172+
- For k = 4 = 2²: We need either one element divisible by 4, OR two elements divisible by 2
173+
174+
**Strategy:**
175+
1. Check if any element is already divisible by k → answer is 0
176+
2. For k = 4, handle special case: count even numbers to determine if we can get two factors of 2
177+
3. Otherwise, find the minimum operations needed to make any single element divisible by k
178+
179+
### 3. Algorithm Breakdown
180+
181+
**Step-by-Step:**
182+
183+
1. **Early Exit Check:** If any element is divisible by k, return 0
184+
2. **Calculate Minimum Gap:** For each element, calculate `(k - (a[i] % k)) % k` - the operations needed to make it divisible by k
185+
3. **Special Handling for k = 4:**
186+
- Count even numbers in the array
187+
- If n = 1: Use the minimum gap
188+
- If no even numbers: Compare min(2, min_gap) - we can make one number even twice
189+
- If 1 even number: Compare min(1, min_gap) - we need one more even
190+
- If ≥2 even numbers: Already divisible (shouldn't reach here due to step 1)
191+
4. **For k = 2, 3, 5:** Return the minimum gap
192+
193+
**Time Complexity:** O(n)
194+
- Single pass through the array to read input and calculate minimum gap
195+
- All operations are O(1) per element
196+
197+
**Space Complexity:** O(n)
198+
- Storing the array of n elements
199+
- Could be optimized to O(1) by not storing the array
200+
201+
### 4. Code Walkthrough
202+
203+
```cpp
204+
void solve() {
205+
int n, k, min_far = INT_MAX;
206+
cin >> n >> k;
207+
bool flag = false;
208+
int even = 0;
209+
210+
// Read array and calculate metrics
211+
for (int i = 0; i < n; i++) {
212+
cin >> a[i];
213+
if (!(a[i] & 1)) even++; // Count even numbers for k=4 case
214+
215+
// Calculate operations needed to make a[i] divisible by k
216+
min_far = min(min_far, (k - (a[i]%k))%k);
217+
218+
// Early exit flag if already divisible
219+
if (a[i] % k == 0) {
220+
flag = true;
221+
}
222+
}
223+
224+
// Already divisible
225+
if (flag) {
226+
cout << "0\n"; return;
227+
}
228+
229+
// Special case: k = 4 requires two factors of 2
230+
if (k == 4) {
231+
if (n == 1) {
232+
// Single element: must make it divisible by 4
233+
cout << min_far << "\n"; return;
234+
} else if (even == 0) {
235+
// No even numbers: either make one divisible by 4,
236+
// or make two numbers even (2 operations)
237+
cout << min(2, min_far) << "\n";
238+
} else if (even == 1) {
239+
// One even number: need one more factor of 2
240+
// Either make another number even (1 op) or make one divisible by 4
241+
cout << min(1, min_far) << "\n";
242+
} else if (even >= 2) {
243+
// Two or more even numbers: product has at least 2² = 4
244+
cout << 0 << "\n";
245+
}
246+
return;
247+
}
248+
249+
// For k = 2, 3, 5: just need one element divisible by k
250+
cout << min_far << "\n";
251+
}
252+
```
253+
254+
**Critical Sections:**
255+
256+
- `(k - (a[i]%k))%k`: Handles the case when `a[i] % k == 0` elegantly (gives 0)
257+
- `!(a[i] & 1)`: Bitwise check for even numbers (faster than `a[i] % 2 == 0`)
258+
- k = 4 logic: Recognizes that 4 = 2² needs two factors of 2, which can come from two even numbers OR one multiple of 4
259+
260+
### Test Cases
261+
262+
**Example Walkthrough:**
263+
```
264+
Input: n=3, k=4, array=[2, 3, 5]
265+
- a[0]=2: not divisible by 4, gap = (4-2)%4 = 2
266+
- a[1]=3: not divisible by 4, gap = (4-3)%4 = 1
267+
- a[2]=5: not divisible by 4, gap = (4-1)%4 = 3
268+
- min_far = 1
269+
- even = 1 (only a[0]=2)
270+
- Since k=4 and even=1: answer = min(1, 1) = 1
271+
- Incrementing a[1] once gives [2, 4, 5], product = 40 divisible by 4 ✓
272+
```
273+
274+
**Edge Cases:**
275+
276+
1. **Already divisible:** `k=3, array=[6, 9]` → Output: 0
277+
2. **Single element:** `k=4, array=[7]` → Output: 1 (make it 8)
278+
3. **k=4 with no evens:** `k=4, array=[1, 3, 5]` → Output: 2
279+
4. **k=4 with 2+ evens:** `k=4, array=[2, 4, 6]` → Output: 0
280+
5. **Prime k:** `k=5, array=[3, 7, 11]` → Output: 2 (make 3→5)

0 commit comments

Comments
 (0)