Skip to content

Commit d22b30c

Browse files
committed
Add 3375. Minimum Operations to Make Array Values Equal to K
1 parent dc0738b commit d22b30c

File tree

2 files changed

+53
-0
lines changed

2 files changed

+53
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
package minimum_operations_to_make_array_values_equal_to_k
2+
3+
func minOperations(nums []int, k int) int {
4+
distinct := make(map[int]bool)
5+
6+
for _, n := range nums {
7+
if n < k {
8+
return -1
9+
} else if n > k {
10+
distinct[n] = true
11+
}
12+
}
13+
return len(distinct)
14+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
package minimum_operations_to_make_array_values_equal_to_k
2+
3+
import "testing"
4+
5+
func TestMinOperations(t *testing.T) {
6+
tests := []struct {
7+
name string
8+
nums []int
9+
k int
10+
expectedResult int
11+
} {
12+
{
13+
name: "Test Case 1",
14+
nums: []int{5,2,5,4,5},
15+
k : 2,
16+
expectedResult: 2,
17+
},
18+
{
19+
name: "Test Case 2",
20+
nums: []int{2,1,2},
21+
k : 2,
22+
expectedResult: -1,
23+
},
24+
{
25+
name: "Test Case 3",
26+
nums: []int{9,7,5,3},
27+
k : 1,
28+
expectedResult: 4,
29+
},
30+
}
31+
32+
for _, test := range tests {
33+
got := minOperations(test.nums, test.k)
34+
35+
if got != test.expectedResult {
36+
t.Errorf("minOperations(%v, %d) = %d, want = %d", test.nums, test.k, got, test.expectedResult )
37+
}
38+
}
39+
}

0 commit comments

Comments
 (0)