From 3fbaf2922a07cefba84d09d42036181227cab6d3 Mon Sep 17 00:00:00 2001 From: Dipanita45 <132455672+Dipanita45@users.noreply.github.com> Date: Thu, 24 Apr 2025 09:11:04 +0530 Subject: [PATCH] Update --- Java/.project | 11 ++++++++++ Java/Count of Subarrays in An Array.java | 28 ++++++++++++++++++++++++ 2 files changed, 39 insertions(+) create mode 100644 Java/Count of Subarrays in An Array.java diff --git a/Java/.project b/Java/.project index 4fdb69a93e..bf31459ef4 100644 --- a/Java/.project +++ b/Java/.project @@ -14,4 +14,15 @@ org.eclipse.jdt.core.javanature + + + 1745466012044 + + 30 + + org.eclipse.core.resources.regexFilterMatcher + node_modules|\.git|__CREATED_BY_JAVA_LANGUAGE_SERVER__ + + + diff --git a/Java/Count of Subarrays in An Array.java b/Java/Count of Subarrays in An Array.java new file mode 100644 index 0000000000..5032b24ab9 --- /dev/null +++ b/Java/Count of Subarrays in An Array.java @@ -0,0 +1,28 @@ +class Solution { + public int countCompleteSubarrays(int[] nums) { + int cnt = 0; + HashSet set = new HashSet<>(); + for (int i = 0; i < nums.length; i++) { + set.add(nums[i]); + } + HashMap map = new HashMap<>(); + int i = 0, j = 0; + while (j < nums.length) { + map.put(nums[j], map.getOrDefault(nums[j], 0) + 1); + if (map.size() == set.size()) { + cnt += nums.length - j; + while (true) { + map.put(nums[i], map.get(nums[i]) - 1); + if (map.get(nums[i]) == 0) { + map.remove(nums[i++]); + break; + } + cnt += nums.length - j; + i++; + } + } + j++; + } + return cnt; + } +} \ No newline at end of file