π¨βπ»
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* Counts the number of subarrays where the middle element | |
* is exactly half the sum of the first and last elements. | |
* | |
* @param {number[]} nums - An array of numbers. | |
* @return {number} - The count of valid subarrays. | |
*/ | |
var countSubarrays = function (nums) { | |
let count = 0; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* @param {number[]} nums | |
* @param {number} minK | |
* @param {number} maxK | |
* @return {number} | |
*/ | |
var countSubarrays = function(nums, minK, maxK) { | |
// Initialize variables to keep track of indices and result | |
let minI = -1; // Index of the last occurrence of minK | |
let maxI = -1; // Index of the last occurrence of maxK |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* @param {number[]} nums | |
* @param {number} modulo | |
* @param {number} k | |
* @return {number} | |
*/ | |
var countInterestingSubarrays = function(nums, modulo, k) { | |
let res = 0; // Stores the count of interesting subarrays | |
let pre = 0; // Prefix sum tracking occurrences of nums[i] % mod === k | |
let map = new Map([[0, 1]]); // Map to store frequencies of prefix sums modulo 'mod' |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* Counts the number of subarrays that contain all distinct elements from the input array. | |
* @param {number[]} nums - The input array of numbers. | |
* @return {number} - The count of complete subarrays. | |
*/ | |
var countCompleteSubarrays = function(nums) { | |
let count = 0; // Keeps track of the number of complete subarrays | |
let set = new Set(); | |
// Determine the number of distinct elements in the array |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* @param {number} n | |
* @return {number} | |
*/ | |
var countLargestGroup = function(n) { | |
// Map to store frequency of digit sum groups | |
const countMap = {}; | |
let maxSize = 0, result = 0; | |
// Loop through numbers from 1 to n |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* @param {number} n | |
* @param {number} maxValue | |
* @return {number} | |
*/ | |
// Define the modulo constant for operations | |
const MODULO = 10n ** 9n + 7n; | |
// Define the maximum value for calculations | |
const MAX_VALUE = 10000; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* This function calculates the number of valid arrays that can be reconstructed | |
* from a given list of differences within a specified range. | |
* | |
* @param {number[]} differences - An array of differences between consecutive elements. | |
* @param {number} lower - The lower bound of the valid range. | |
* @param {number} upper - The upper bound of the valid range. | |
* @return {number} - The number of valid arrays that can be reconstructed. | |
*/ | |
var numberOfArrays = function(differences, lower, upper) { |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* @param {number[]} answers | |
* @return {number} | |
*/ | |
var numRabbits = function(answers) { | |
// Step 1: Create a frequency map to count occurrences of each answer | |
const countMap = {}; | |
for (const answer of answers) { | |
countMap[answer] = (countMap[answer] || 0) + 1; | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* Generates the nth element of the count-and-say sequence. | |
* | |
* @param {number} n - The position in the sequence (1-based index). | |
* @return {string} - The nth element of the count-and-say sequence. | |
*/ | |
var countAndSay = function(n) { | |
// Validate the input: must be between 1 and 30 (inclusive) | |
if (n < 1 || n > 30 || n == null) { | |
return 'ERROR'; // Return an error if input is invalid |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* @param {number[]} nums | |
* @param {number} k | |
* @return {number} | |
*/ | |
var countPairs = function(nums, k) { | |
// Initialize a counter to keep track of pairs that satisfy the conditions | |
let count = 0; | |
// Loop through all possible values of i (starting from index 0) |
NewerOlder