Skip to content

Instantly share code, notes, and snippets.

View tatsuyax25's full-sized avatar
πŸ‘¨β€πŸ’»
Focusing

Miguel Urena tatsuyax25

πŸ‘¨β€πŸ’»
Focusing
View GitHub Profile
@tatsuyax25
tatsuyax25 / countInterestingSubarrays.js
Created April 25, 2025 16:49
You are given a 0-indexed integer array nums, an integer modulo, and an integer k. Your task is to find the count of subarrays that are interesting. A subarray nums[l..r] is interesting if the following condition holds: Let cnt be the number of in
/**
* @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'
@tatsuyax25
tatsuyax25 / countCompleteSubarrays.js
Created April 24, 2025 20:58
You are given an array nums consisting of positive integers. We call a subarray of an array complete if the following condition is satisfied: The number of distinct elements in the subarray is equal to the number of distinct elements in the whole a
/**
* 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
@tatsuyax25
tatsuyax25 / countLargestGroup.js
Created April 23, 2025 16:43
You are given an integer n. Each number from 1 to n is grouped according to the sum of its digits. Return the number of groups that have the largest size.
/**
* @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
@tatsuyax25
tatsuyax25 / idealArrays.js
Created April 22, 2025 20:01
You are given two integers n and maxValue, which are used to describe an ideal array. A 0-indexed integer array arr of length n is considered ideal if the following conditions hold: Every arr[i] is a value from 1 to maxValue, for 0 <= i < n. Every
/**
* @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;
@tatsuyax25
tatsuyax25 / numberOfArrays.js
Created April 21, 2025 19:30
You are given a 0-indexed array of n integers differences, which describes the differences between each pair of consecutive integers of a hidden sequence of length (n + 1). More formally, call the hidden sequence hidden, then we have that differences
/**
* 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) {
@tatsuyax25
tatsuyax25 / numRabbits.js
Created April 20, 2025 18:12
There is a forest with an unknown number of rabbits. We asked n rabbits "How many rabbits have the same color as you?" and collected the answers in an integer array answers where answers[i] is the answer of the ith rabbit. Given the array answers, r
/**
* @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;
}
@tatsuyax25
tatsuyax25 / countAndSay.js
Created April 18, 2025 16:14
The count-and-say sequence is a sequence of digit strings defined by the recursive formula: countAndSay(1) = "1" countAndSay(n) is the run-length encoding of countAndSay(n - 1). Run-length encoding (RLE) is a string compression method that works by
/**
* 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
@tatsuyax25
tatsuyax25 / countPairs.js
Created April 17, 2025 19:13
Given a 0-indexed integer array nums of length n and an integer k, return the number of pairs (i, j) where 0 <= i < j < n, such that nums[i] == nums[j] and (i * j) is divisible by k.
/**
* @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)
@tatsuyax25
tatsuyax25 / countGood.js
Created April 16, 2025 16:24
Given an integer array nums and an integer k, return the number of good subarrays of nums. A subarray arr is good if there are at least k pairs of indices (i, j) such that i < j and arr[i] == arr[j]. A subarray is a contiguous non-empty sequence of
/**
* 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) {
@tatsuyax25
tatsuyax25 / goodTriplets.js
Created April 15, 2025 17:58
You are given two 0-indexed arrays nums1 and nums2 of length n, both of which are permutations of [0, 1, ..., n - 1]. A good triplet is a set of 3 distinct values which are present in increasing order by position both in nums1 and nums2. In other wo
/**
* @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)