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 / countSubarrays.js
Last active April 27, 2025 21:08
Given an integer array nums, return the number of subarrays of length 3 such that the sum of the first and third numbers equals exactly half of the second number.
/**
* 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;
@tatsuyax25
tatsuyax25 / countSubarrays.js
Created April 26, 2025 19:50
You are given an integer array nums and two integers minK and maxK. A fixed-bound subarray of nums is a subarray that satisfies the following conditions: The minimum value in the subarray is equal to minK. The maximum value in the subarray is equal
/**
* @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
@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)