From 1027291a2d97387af95d27c5a63954699f0cd608 Mon Sep 17 00:00:00 2001 From: Valentyn Kolesnikov Date: Wed, 6 May 2026 09:50:25 +0300 Subject: [PATCH] Added task 3779 --- .../Solution.java | 19 ++++++++ .../readme.md | 46 +++++++++++++++++++ .../SolutionTest.java | 23 ++++++++++ 3 files changed, 88 insertions(+) create mode 100644 src/main/java/g3701_3800/s3779_minimum_number_of_operations_to_have_distinct_elements/Solution.java create mode 100644 src/main/java/g3701_3800/s3779_minimum_number_of_operations_to_have_distinct_elements/readme.md create mode 100644 src/test/java/g3701_3800/s3779_minimum_number_of_operations_to_have_distinct_elements/SolutionTest.java diff --git a/src/main/java/g3701_3800/s3779_minimum_number_of_operations_to_have_distinct_elements/Solution.java b/src/main/java/g3701_3800/s3779_minimum_number_of_operations_to_have_distinct_elements/Solution.java new file mode 100644 index 000000000..424c409bf --- /dev/null +++ b/src/main/java/g3701_3800/s3779_minimum_number_of_operations_to_have_distinct_elements/Solution.java @@ -0,0 +1,19 @@ +package g3701_3800.s3779_minimum_number_of_operations_to_have_distinct_elements; + +// #Medium #Array #Hash_Table #Senior #Biweekly_Contest_172 +// #2026_05_06_Time_4_ms_(99.25%)_Space_132.03_MB_(83.46%) + +public class Solution { + public int minOperations(int[] nums) { + boolean[] seen = new boolean[100001]; + int i = nums.length - 1; + while (i >= 0) { + if (seen[nums[i]]) { + break; + } + seen[nums[i]] = true; + i--; + } + return (i + 3) / 3; + } +} diff --git a/src/main/java/g3701_3800/s3779_minimum_number_of_operations_to_have_distinct_elements/readme.md b/src/main/java/g3701_3800/s3779_minimum_number_of_operations_to_have_distinct_elements/readme.md new file mode 100644 index 000000000..a53319d6d --- /dev/null +++ b/src/main/java/g3701_3800/s3779_minimum_number_of_operations_to_have_distinct_elements/readme.md @@ -0,0 +1,46 @@ +3779\. Minimum Number of Operations to Have Distinct Elements + +Medium + +You are given an integer array `nums`. + +In one operation, you remove the **first three elements** of the current array. If there are fewer than three elements remaining, **all** remaining elements are removed. + +Repeat this operation until the array is empty or contains no duplicate values. + +Return an integer denoting the number of operations required. + +**Example 1:** + +**Input:** nums = [3,8,3,6,5,8] + +**Output:** 1 + +**Explanation:** + +In the first operation, we remove the first three elements. The remaining elements `[6, 5, 8]` are all distinct, so we stop. Only one operation is needed. + +**Example 2:** + +**Input:** nums = [2,2] + +**Output:** 1 + +**Explanation:** + +After one operation, the array becomes empty, which meets the stopping condition. + +**Example 3:** + +**Input:** nums = [4,3,5,1,2] + +**Output:** 0 + +**Explanation:** + +All elements in the array are distinct, therefore no operations are needed. + +**Constraints:** + +* 1 <= nums.length <= 105 +* 1 <= nums[i] <= 105 \ No newline at end of file diff --git a/src/test/java/g3701_3800/s3779_minimum_number_of_operations_to_have_distinct_elements/SolutionTest.java b/src/test/java/g3701_3800/s3779_minimum_number_of_operations_to_have_distinct_elements/SolutionTest.java new file mode 100644 index 000000000..758cedfa7 --- /dev/null +++ b/src/test/java/g3701_3800/s3779_minimum_number_of_operations_to_have_distinct_elements/SolutionTest.java @@ -0,0 +1,23 @@ +package g3701_3800.s3779_minimum_number_of_operations_to_have_distinct_elements; + +import static org.hamcrest.CoreMatchers.equalTo; +import static org.hamcrest.MatcherAssert.assertThat; + +import org.junit.jupiter.api.Test; + +class SolutionTest { + @Test + void minOperations() { + assertThat(new Solution().minOperations(new int[] {3, 8, 3, 6, 5, 8}), equalTo(1)); + } + + @Test + void minOperations2() { + assertThat(new Solution().minOperations(new int[] {2, 2}), equalTo(1)); + } + + @Test + void minOperations3() { + assertThat(new Solution().minOperations(new int[] {4, 3, 5, 1, 2}), equalTo(0)); + } +}