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 / 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)
@tatsuyax25
tatsuyax25 / countGoodTriplets.js
Created April 14, 2025 16:18
Given an array of integers arr, and three integers a, b and c. You need to find the number of good triplets. A triplet (arr[i], arr[j], arr[k]) is good if the following conditions are true: 0 <= i < j < k < arr.length |arr[i] - arr[j]| <= a |arr[j]
/**
* @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;
@tatsuyax25
tatsuyax25 / countGoodNumbers.js
Created April 13, 2025 19:30
A digit string is good if the digits (0-indexed) at even indices are even and the digits at odd indices are prime (2, 3, 5, or 7). For example, "2582" is good because the digits (2 and 8) at even positions are even and the digits (5 and 2) at odd po
/**
* @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