From 7e36667e30230560d69d9a9778a6a7c7c408e78f Mon Sep 17 00:00:00 2001 From: Varun Verma Date: Wed, 16 Oct 2024 19:39:09 +0530 Subject: [PATCH 1/3] Create 0219-contains-duplicate-II.cs --- csharp/0219-contains-duplicate-II.cs | 36 ++++++++++++++++++++++++++++ 1 file changed, 36 insertions(+) create mode 100644 csharp/0219-contains-duplicate-II.cs diff --git a/csharp/0219-contains-duplicate-II.cs b/csharp/0219-contains-duplicate-II.cs new file mode 100644 index 000000000..01ba33a45 --- /dev/null +++ b/csharp/0219-contains-duplicate-II.cs @@ -0,0 +1,36 @@ +/* + +Approach: +1. We will use a HashSet to store the elements. +2. We will iterate through the array and add the elements to the HashSet. +3. If the element is already present in the HashSet, we will return true. +4. If the difference between the current index and the previous index is greater than k, we will remove the element at the previous index. +5. If we reach the end of the array, we will return false. + +Time Complexity: O(n) +Space Complexity: O(n) + +*/ +public class Solution { + public bool ContainsNearbyDuplicate(int[] nums, int k) { + HashSet hs = new HashSet(); + int i,j; + i = j = 0; + while(j Date: Wed, 16 Oct 2024 19:41:58 +0530 Subject: [PATCH 2/3] Delete csharp/0219-contains-duplicate-II.cs --- csharp/0219-contains-duplicate-II.cs | 36 ---------------------------- 1 file changed, 36 deletions(-) delete mode 100644 csharp/0219-contains-duplicate-II.cs diff --git a/csharp/0219-contains-duplicate-II.cs b/csharp/0219-contains-duplicate-II.cs deleted file mode 100644 index 01ba33a45..000000000 --- a/csharp/0219-contains-duplicate-II.cs +++ /dev/null @@ -1,36 +0,0 @@ -/* - -Approach: -1. We will use a HashSet to store the elements. -2. We will iterate through the array and add the elements to the HashSet. -3. If the element is already present in the HashSet, we will return true. -4. If the difference between the current index and the previous index is greater than k, we will remove the element at the previous index. -5. If we reach the end of the array, we will return false. - -Time Complexity: O(n) -Space Complexity: O(n) - -*/ -public class Solution { - public bool ContainsNearbyDuplicate(int[] nums, int k) { - HashSet hs = new HashSet(); - int i,j; - i = j = 0; - while(j Date: Wed, 16 Oct 2024 19:43:14 +0530 Subject: [PATCH 3/3] Create 1343-number-of-sub-arraysofSize-k-and-average-greaterthanor-equal-to-threshold.cs --- ...verage-greaterthanor-equal-to-threshold.cs | 44 +++++++++++++++++++ 1 file changed, 44 insertions(+) create mode 100644 csharp/1343-number-of-sub-arraysofSize-k-and-average-greaterthanor-equal-to-threshold.cs diff --git a/csharp/1343-number-of-sub-arraysofSize-k-and-average-greaterthanor-equal-to-threshold.cs b/csharp/1343-number-of-sub-arraysofSize-k-and-average-greaterthanor-equal-to-threshold.cs new file mode 100644 index 000000000..ee222b866 --- /dev/null +++ b/csharp/1343-number-of-sub-arraysofSize-k-and-average-greaterthanor-equal-to-threshold.cs @@ -0,0 +1,44 @@ +/* + +Approach: +1. We will initialize i, j, c, res, and sum to 0. +2. We will calculate the sum of the first k elements and check if the average is greater than or equal to the threshold. +3. We will increment the result if the average is greater than or equal to the threshold. +4. We will iterate through the array and calculate the sum of the next k elements. +5. We will check if the average is greater than or equal to the threshold and increment the result accordingly. +6. We will return the result. + +Time Complexity: O(n) +Space Complexity: O(1) + +*/ +public class Solution { + public int NumOfSubarrays(int[] arr, int k, int threshold) { + int i ,j, c, res, sum; // Initialize i, j, c, res, and sum to 0. + i = c = sum = res = 0; // Initialize i, c, sum, and res to 0. + j = i + k - 1; // Initialize j to i + k - 1. + + while (c <= j) { // Calculate the sum of the first k elements. + sum = sum + arr[c]; // Add the element at index c to the sum. + c++; // Increment c. + } + + res = (sum/k) >= threshold ? 1 : 0; // Check if the average is greater than or equal to the threshold and increment the result accordingly. + + while ( j < arr.Length) { // Iterate through the array and calculate the sum of the next k elements. + sum = sum - arr[i++]; // Subtract the element at index i from the sum and increment i. + j++; // Increment j. + if(j < arr.Length) { // Check if j is less than the length of the array. + sum = sum + arr[j]; // Add the element at index j to the sum. + } + else { + break; // Break the loop if j is equal to or greater than the length of the array. + } + if((sum/k) >= threshold) { // Check if the average is greater than or equal to the threshold. + res++; // Increment the result. + } + } + + return res; // Return the result. + } +}