π¨βπ»
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) |
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
/** | |
* Function to count the number of "good" subarrays in an array. | |
* A "good" subarray is defined as having at least `k` pairs of indices `(i, j)` | |
* such that `nums[i] == nums[j]` and `i < j`. | |
* | |
* @param {number[]} nums - Array of integers to be evaluated. | |
* @param {number} k - Minimum number of pairs needed to consider a subarray "good". | |
* @return {number} - The total count of "good" subarrays. | |
*/ | |
var countGood = function(nums, k) { |
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[]} nums1 | |
* @param {number[]} nums2 | |
* @return {number} | |
*/ | |
// Class for Binary Indexed Tree (BIT), also known as Fenwick Tree | |
class BIT { | |
constructor(size) { | |
this.n = size; // Size of the BIT | |
this.tree = new Array(size + 1).fill(0); // Initialize BIT array with zeros (1-based index) |
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[]} arr | |
* @param {number} a | |
* @param {number} b | |
* @param {number} c | |
* @return {number} | |
*/ | |
var countGoodTriplets = function(arr, a, b, c) { | |
// Initialize a counter to track the number of good tripets | |
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} n | |
* @return {number} | |
*/ | |
// Define the modulo value to prevent overflow in calculations | |
const MOD = 1_000_000_007; | |
function modPow(base, exp, mod) { | |
let result = 1n; // Initialize result as 1 | |
let b = BigInt(base), e = BigInt(exp), m = BigInt(mod); // Convert to BigInt for large numbers |