Skip to content

Instantly share code, notes, and snippets.

@tatsuyax25
Created April 9, 2025 16:30
Show Gist options
  • Save tatsuyax25/91b2d127efa422fd890565223b0d1a82 to your computer and use it in GitHub Desktop.
Save tatsuyax25/91b2d127efa422fd890565223b0d1a82 to your computer and use it in GitHub Desktop.
You are given an integer array nums and an integer k. An integer h is called valid if all values in the array that are strictly greater than h are identical. For example, if nums = [10, 8, 10, 8], a valid integer is h = 9 because all nums[i] > 9 ar
/**
* This function calculates the minimum operations required to satisfy certain conditions.
* @param {number[]} nums - Array of numbers to process.
* @param {number} k - Threshold value to compare against.
* @return {number} - Returns the size of the unique set of numbers greater than k, or -1 if any number is less than k.
*/
var minOperations = function (nums, k) {
// Check if any number in the array is less than k. If so, return -1.
for (let num of nums) {
if (num < k) return -1;
}
// Create a set to store unique numbers greater than k.
const uniqueNums = new Set();
// Iterate through the array and add numbers greater than k to the set.
for (let num of nums) {
if (num > k) uniqueNums.add(num);
}
// Return the size of the set, representing the count of unique numbers greater than k.
return uniqueNums.size;
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment