Skip to content

Commit dedd522

Browse files
Added 3190-find-minimum-operations-to-make-all-elements-divisible-by-three
1 parent fa129a0 commit dedd522

File tree

1 file changed

+92
-0
lines changed

1 file changed

+92
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,92 @@
1+
---
2+
id: minimum-operations-to-make-all-elements-divisible-by-three
3+
title: Find Minimum Operations to Make All Elements Divisible by Three
4+
level: medium
5+
sidebar_label: Minimum Operations to Make All Elements Divisible by Three
6+
tags:
7+
- Array
8+
- Math
9+
- Java
10+
- Python
11+
description: "This document provides solutions for the Find Minimum Operations to Make All Elements Divisible by Three problem."
12+
---
13+
14+
## Problem Statement
15+
16+
You are given an integer array `nums`. In one operation, you can add or subtract 1 from any element of `nums`.
17+
18+
Return the minimum number of operations to make all elements of `nums` divisible by 3.
19+
20+
**Example 1:**
21+
22+
Input: `nums = [1, 2, 3, 4]`
23+
Output: `3`
24+
25+
Explanation:
26+
All array elements can be made divisible by 3 using 3 operations:
27+
- Subtract 1 from 1.
28+
- Add 1 to 2.
29+
- Subtract 1 from 4.
30+
31+
**Example 2:**
32+
33+
Input: `nums = [3, 6, 9]`
34+
Output: `0`
35+
36+
**Constraints:**
37+
38+
- `1 <= nums.length <= 50`
39+
- `1 <= nums[i] <= 50`
40+
41+
## Solutions
42+
43+
### Intuition
44+
45+
To make all elements divisible by 3, we need to adjust each element by either adding or subtracting 1 until its remainder when divided by 3 is zero. Each adjustment operation (either adding or subtracting 1) counts as one operation.
46+
47+
### Approach
48+
49+
1. Iterate through each element in the array.
50+
2. For each element, check its remainder when divided by 3.
51+
3. If the remainder is 1 or 2, it means we need one operation to make it divisible by 3 (subtract 1 if the remainder is 1, or add 1 if the remainder is 2).
52+
4. Sum up the number of operations required for each element.
53+
54+
### Implementation
55+
56+
The provided code effectively implements the above approach. Here's the breakdown:
57+
58+
1. **Iteration and Remainder Check:**
59+
- Iterate through each element in the `nums` array.
60+
- Check the remainder of each element when divided by 3.
61+
- Increment the operations counter based on the remainder.
62+
63+
### Java
64+
65+
```java
66+
class Solution {
67+
public int minimumOperations(int[] nums) {
68+
int operations = 0;
69+
for (int n : nums) {
70+
if (n % 3 == 1 || n % 3 == 2) {
71+
operations++;
72+
}
73+
}
74+
return operations;
75+
}
76+
}
77+
```
78+
### Python
79+
80+
```Python
81+
class Solution:
82+
def minimumOperations(self, nums: List[int]) -> int:
83+
operations = 0
84+
for n in nums:
85+
if n % 3 == 1 or n % 3 == 2:
86+
operations += 1
87+
return operations
88+
89+
```
90+
91+
### Conclusion
92+
This implementation efficiently computes the minimum operations required to make all elements of nums divisible by 3 by iterating through the array and counting adjustments needed based on remainders.

0 commit comments

Comments
 (0)