2025-10-21
3346. Maximum Frequency of an Element After Performing Operations I
Topic: Array, Binary Search, Sliding Window, Sorting, Prefix Sum
Difficulty: Medium
Problem:
You are given an integer array
You must perform an operation
• Select an index
• Add an integer in the range
Return the maximum possible frequency of any element in
Example 1:
Input: nums = 1,4,5, k = 1, numOperations = 2
Output: 2
Explanation:
We can achieve a maximum frequency of two by:
• Adding 0 to
• Adding -1 to
Example 2:
Input: nums = 5,11,20,20, k = 5, numOperations = 1
Output: 2
Explanation:
We can achieve a maximum frequency of two by:
• Adding 0 to
Constraints:
•
•
•
•
  3346. Maximum Frequency of an Element After Performing Operations I
Topic: Array, Binary Search, Sliding Window, Sorting, Prefix Sum
Difficulty: Medium
Problem:
You are given an integer array
nums and two integers k and numOperations.You must perform an operation
numOperations times on nums, where in each operation you:• Select an index
i that was not selected in any previous operations.• Add an integer in the range
[-k, k] to nums[i].Return the maximum possible frequency of any element in
nums after performing the operations.Example 1:
Input: nums = 1,4,5, k = 1, numOperations = 2
Output: 2
Explanation:
We can achieve a maximum frequency of two by:
• Adding 0 to
nums[1]. nums becomes [1, 4, 5].• Adding -1 to
nums[2]. nums becomes [1, 4, 4].Example 2:
Input: nums = 5,11,20,20, k = 5, numOperations = 1
Output: 2
Explanation:
We can achieve a maximum frequency of two by:
• Adding 0 to
nums[1].Constraints:
•
1 <= nums.length <= 10^5•
1 <= nums[i] <= 10^5•
0 <= k <= 10^5•
0 <= numOperations <= nums.length2025-10-22
3347. Maximum Frequency of an Element After Performing Operations II
Topic: Array, Binary Search, Sliding Window, Sorting, Prefix Sum
Difficulty: Hard
Problem:
You are given an integer array
You must perform an operation
• Select an index
• Add an integer in the range
Return the maximum possible frequency of any element in
Example 1:
Input: nums = 1,4,5, k = 1, numOperations = 2
Output: 2
Explanation:
We can achieve a maximum frequency of two by:
• Adding 0 to
• Adding -1 to
Example 2:
Input: nums = 5,11,20,20, k = 5, numOperations = 1
Output: 2
Explanation:
We can achieve a maximum frequency of two by:
• Adding 0 to
Constraints:
•
•
•
•
  3347. Maximum Frequency of an Element After Performing Operations II
Topic: Array, Binary Search, Sliding Window, Sorting, Prefix Sum
Difficulty: Hard
Problem:
You are given an integer array
nums and two integers k and numOperations.You must perform an operation
numOperations times on nums, where in each operation you:• Select an index
i that was not selected in any previous operations.• Add an integer in the range
[-k, k] to nums[i].Return the maximum possible frequency of any element in
nums after performing the operations.Example 1:
Input: nums = 1,4,5, k = 1, numOperations = 2
Output: 2
Explanation:
We can achieve a maximum frequency of two by:
• Adding 0 to
nums[1], after which nums becomes [1, 4, 5].• Adding -1 to
nums[2], after which nums becomes [1, 4, 4].Example 2:
Input: nums = 5,11,20,20, k = 5, numOperations = 1
Output: 2
Explanation:
We can achieve a maximum frequency of two by:
• Adding 0 to
nums[1].Constraints:
•
1 <= nums.length <= 10^5•
1 <= nums[i] <= 10^9•
0 <= k <= 10^9•
0 <= numOperations <= nums.length2025-10-23
3461. Check If Digits Are Equal in String After Operations I
Topic: Math, String, Simulation, Combinatorics, Number Theory
Difficulty: Easy
Problem:
You are given a string
• For each pair of consecutive digits in
• Replace
Return
Example 1:
Input: s = "3902"
Output: true
Explanation:
• Initially,
• First operation:
•
•
•
•
• Second operation:
•
•
•
• Since the digits in
Example 2:
Input: s = "34789"
Output: false
Explanation:
• Initially,
• After the first operation,
• After the second operation,
• After the third operation,
• Since
Constraints:
•
•
  3461. Check If Digits Are Equal in String After Operations I
Topic: Math, String, Simulation, Combinatorics, Number Theory
Difficulty: Easy
Problem:
You are given a string
s consisting of digits. Perform the following operation repeatedly until the string has exactly two digits:• For each pair of consecutive digits in
s, starting from the first digit, calculate a new digit as the sum of the two digits modulo 10.• Replace
s with the sequence of newly calculated digits, maintaining the order in which they are computed.Return
true if the final two digits in s are the same; otherwise, return false.Example 1:
Input: s = "3902"
Output: true
Explanation:
• Initially,
s = "3902"• First operation:
•
(s[0] + s[1]) % 10 = (3 + 9) % 10 = 2•
(s[1] + s[2]) % 10 = (9 + 0) % 10 = 9•
(s[2] + s[3]) % 10 = (0 + 2) % 10 = 2•
s becomes "292"• Second operation:
•
(s[0] + s[1]) % 10 = (2 + 9) % 10 = 1•
(s[1] + s[2]) % 10 = (9 + 2) % 10 = 1•
s becomes "11"• Since the digits in
"11" are the same, the output is true.Example 2:
Input: s = "34789"
Output: false
Explanation:
• Initially,
s = "34789".• After the first operation,
s = "7157".• After the second operation,
s = "862".• After the third operation,
s = "48".• Since
'4' != '8', the output is false.Constraints:
•
3 <= s.length <= 100•
s consists of only digits.2025-10-24
2048. Next Greater Numerically Balanced Number
Topic: Hash Table, Math, Backtracking, Counting, Enumeration
Difficulty: Medium
Problem:
An integer
Given an integer
Example 1:
Example 2:
Example 3:
Constraints:
•
  2048. Next Greater Numerically Balanced Number
Topic: Hash Table, Math, Backtracking, Counting, Enumeration
Difficulty: Medium
Problem:
An integer
x is numerically balanced if for every digit d in the number x, there are exactly d occurrences of that digit in x.Given an integer
n, return the smallest numerically balanced number strictly greater than n.Example 1:
Input: n = 1
Output: 22
Explanation:
22 is numerically balanced since:
- The digit 2 occurs 2 times.
It is also the smallest numerically balanced number strictly greater than 1.
Example 2:
Input: n = 1000
Output: 1333
Explanation:
1333 is numerically balanced since:
- The digit 1 occurs 1 time.
- The digit 3 occurs 3 times.
It is also the smallest numerically balanced number strictly greater than 1000.
Note that 1022 cannot be the answer because 0 appeared more than 0 times.
Example 3:
Input: n = 3000
Output: 3133
Explanation:
3133 is numerically balanced since:
- The digit 1 occurs 1 time.
- The digit 3 occurs 3 times.
It is also the smallest numerically balanced number strictly greater than 3000.
Constraints:
•
0 <= n <= 10^62025-10-25
1716. Calculate Money in Leetcode Bank
Topic: Math
Difficulty: Easy
Problem:
Hercy wants to save money for his first car. He puts money in the Leetcode bank every day.
He starts by putting in
Given
Example 1:
Example 2:
Example 3:
Constraints:
•
  1716. Calculate Money in Leetcode Bank
Topic: Math
Difficulty: Easy
Problem:
Hercy wants to save money for his first car. He puts money in the Leetcode bank every day.
He starts by putting in
$1 on Monday, the first day. Every day from Tuesday to Sunday, he will put in $1 more than the day before. On every subsequent Monday, he will put in $1 more than the previous Monday. Given
n, return the total amount of money he will have in the Leetcode bank at the end of the n^th day.Example 1:
Input: n = 4
Output: 10
Explanation: After the 4^th day, the total is 1 + 2 + 3 + 4 = 10.
Example 2:
Input: n = 10
Output: 37
Explanation: After the 10^th day, the total is (1 + 2 + 3 + 4 + 5 + 6 + 7) + (2 + 3 + 4) = 37. Notice that on the 2^nd Monday, Hercy only puts in $2.
Example 3:
Input: n = 20
Output: 96
Explanation: After the 20^th day, the total is (1 + 2 + 3 + 4 + 5 + 6 + 7) + (2 + 3 + 4 + 5 + 6 + 7 + 8) + (3 + 4 + 5 + 6 + 7 + 8) = 96.
Constraints:
•
1 <= n <= 10002025-10-26
2043. Simple Bank System
Topic: Array, Hash Table, Design, Simulation
Difficulty: Medium
Problem:
You have been tasked with writing a program for a popular bank that will automate all its incoming transactions (transfer, deposit, and withdraw). The bank has
Execute all the valid transactions. A transaction is valid if:
• The given account number(s) are between
• The amount of money withdrawn or transferred from is less than or equal to the balance of the account.
Implement the
•
•
•
•
Example 1:
Constraints:
•
•
•
• At most
  2043. Simple Bank System
Topic: Array, Hash Table, Design, Simulation
Difficulty: Medium
Problem:
You have been tasked with writing a program for a popular bank that will automate all its incoming transactions (transfer, deposit, and withdraw). The bank has
n accounts numbered from 1 to n. The initial balance of each account is stored in a 0-indexed integer array balance, with the (i + 1)^th account having an initial balance of balance[i].Execute all the valid transactions. A transaction is valid if:
• The given account number(s) are between
1 and n, and• The amount of money withdrawn or transferred from is less than or equal to the balance of the account.
Implement the
Bank class:•
Bank(long[] balance) Initializes the object with the 0-indexed integer array balance.•
boolean transfer(int account1, int account2, long money) Transfers money dollars from the account numbered account1 to the account numbered account2. Return true if the transaction was successful, false otherwise.•
boolean deposit(int account, long money) Deposit money dollars into the account numbered account. Return true if the transaction was successful, false otherwise.•
boolean withdraw(int account, long money) Withdraw money dollars from the account numbered account. Return true if the transaction was successful, false otherwise.Example 1:
Input
["Bank", "withdraw", "transfer", "deposit", "transfer", "withdraw"]
[[[10, 100, 20, 50, 30]], [3, 10], [5, 1, 20], [5, 20], [3, 4, 15], [10, 50]]
Output
[null, true, true, true, false, false]
Explanation
Bank bank = new Bank([10, 100, 20, 50, 30]);
bank.withdraw(3, 10); // return true, account 3 has a balance of $20, so it is valid to withdraw $10.
// Account 3 has $20 - $10 = $10.
bank.transfer(5, 1, 20); // return true, account 5 has a balance of $30, so it is valid to transfer $20.
// Account 5 has $30 - $20 = $10, and account 1 has $10 + $20 = $30.
bank.deposit(5, 20); // return true, it is valid to deposit $20 to account 5.
// Account 5 has $10 + $20 = $30.
bank.transfer(3, 4, 15); // return false, the current balance of account 3 is $10,
// so it is invalid to transfer $15 from it.
bank.withdraw(10, 50); // return false, it is invalid because account 10 does not exist.
Constraints:
•
n == balance.length•
1 <= n, account, account1, account2 <= 10^5•
0 <= balance[i], money <= 10^12• At most
10^4 calls will be made to each function transfer, deposit, withdraw.2025-10-27
2125. Number of Laser Beams in a Bank
Topic: Array, Math, String, Matrix
Difficulty: Medium
Problem:
Anti-theft security devices are activated inside a bank. You are given a 0-indexed binary string array
There is one laser beam between any two security devices if both conditions are met:
• The two devices are located on two different rows:
• For each row
Laser beams are independent, i.e., one beam does not interfere nor join with another.
Return the total number of laser beams in the bank.
Example 1:
Image: https://assets.leetcode.com/uploads/2021/12/24/laser1.jpg
Example 2:
Image: https://assets.leetcode.com/uploads/2021/12/24/laser2.jpg
Constraints:
•
•
•
•
  2125. Number of Laser Beams in a Bank
Topic: Array, Math, String, Matrix
Difficulty: Medium
Problem:
Anti-theft security devices are activated inside a bank. You are given a 0-indexed binary string array
bank representing the floor plan of the bank, which is an m x n 2D matrix. bank[i] represents the i^th row, consisting of '0's and '1's. '0' means the cell is empty, while'1' means the cell has a security device.There is one laser beam between any two security devices if both conditions are met:
• The two devices are located on two different rows:
r_1 and r_2, where r_1 < r_2.• For each row
i where r_1 < i < r_2, there are no security devices in the i^th row.Laser beams are independent, i.e., one beam does not interfere nor join with another.
Return the total number of laser beams in the bank.
Example 1:
Image: https://assets.leetcode.com/uploads/2021/12/24/laser1.jpg
Input: bank = ["011001","000000","010100","001000"]
Output: 8
Explanation: Between each of the following device pairs, there is one beam. In total, there are 8 beams:
* bank[0][1] -- bank[2][1]
* bank[0][1] -- bank[2][3]
* bank[0][2] -- bank[2][1]
* bank[0][2] -- bank[2][3]
* bank[0][5] -- bank[2][1]
* bank[0][5] -- bank[2][3]
* bank[2][1] -- bank[3][2]
* bank[2][3] -- bank[3][2]
Note that there is no beam between any device on the 0^th row with any on the 3^rd row.
This is because the 2^nd row contains security devices, which breaks the second condition.
Example 2:
Image: https://assets.leetcode.com/uploads/2021/12/24/laser2.jpg
Input: bank = ["000","111","000"]
Output: 0
Explanation: There does not exist two devices located on two different rows.
Constraints:
•
m == bank.length•
n == bank[i].length•
1 <= m, n <= 500•
bank[i][j] is either '0' or '1'.2025-10-28
3354. Make Array Elements Equal to Zero
Topic: Array, Simulation, Prefix Sum
Difficulty: Easy
Problem:
You are given an integer array
Start by selecting a starting position
After that, you repeat the following process:
• If
• If
• Else if
• Decrement
• Reverse your movement direction (left becomes right and vice versa).
• Take a step in your new direction.
A selection of the initial position
Return the number of possible valid selections.
Example 1:
Input: nums = 1,0,2,0,3
Output: 2
Explanation:
The only possible valid selections are the following:
• Choose
•
• Choose
•
Example 2:
Input: nums = 2,3,4,0,4,1,0
Output: 0
Explanation:
There are no possible valid selections.
Constraints:
•
•
• There is at least one element
  3354. Make Array Elements Equal to Zero
Topic: Array, Simulation, Prefix Sum
Difficulty: Easy
Problem:
You are given an integer array
nums.Start by selecting a starting position
curr such that nums[curr] == 0, and choose a movement direction of either left or right.After that, you repeat the following process:
• If
curr is out of the range [0, n - 1], this process ends.• If
nums[curr] == 0, move in the current direction by incrementing curr if you are moving right, or decrementing curr if you are moving left.• Else if
nums[curr] > 0:• Decrement
nums[curr] by 1.• Reverse your movement direction (left becomes right and vice versa).
• Take a step in your new direction.
A selection of the initial position
curr and movement direction is considered valid if every element in nums becomes 0 by the end of the process.Return the number of possible valid selections.
Example 1:
Input: nums = 1,0,2,0,3
Output: 2
Explanation:
The only possible valid selections are the following:
• Choose
curr = 3, and a movement direction to the left.•
[1,0,2,0,3] -> [1,0,2,0,3] -> [1,0,1,0,3] -> [1,0,1,0,3] -> [1,0,1,0,2] -> [1,0,1,0,2] -> [1,0,0,0,2] -> [1,0,0,0,2] -> [1,0,0,0,1] -> [1,0,0,0,1] -> [1,0,0,0,1] -> [1,0,0,0,1] -> [0,0,0,0,1] -> [0,0,0,0,1] -> [0,0,0,0,1] -> [0,0,0,0,1] -> [0,0,0,0,0].• Choose
curr = 3, and a movement direction to the right.•
[1,0,2,0,3] -> [1,0,2,0,3] -> [1,0,2,0,2] -> [1,0,2,0,2] -> [1,0,1,0,2] -> [1,0,1,0,2] -> [1,0,1,0,1] -> [1,0,1,0,1] -> [1,0,0,0,1] -> [1,0,0,0,1] -> [1,0,0,0,0] -> [1,0,0,0,0] -> [1,0,0,0,0] -> [1,0,0,0,0] -> [0,0,0,0,0].Example 2:
Input: nums = 2,3,4,0,4,1,0
Output: 0
Explanation:
There are no possible valid selections.
Constraints:
•
1 <= nums.length <= 100•
0 <= nums[i] <= 100• There is at least one element
i where nums[i] == 0.2025-10-29
3370. Smallest Number With All Set Bits
Topic: Math, Bit Manipulation
Difficulty: Easy
Problem:
You are given a positive number
Return the smallest number
Example 1:
Input: n = 5
Output: 7
Explanation:
The binary representation of 7 is
Example 2:
Input: n = 10
Output: 15
Explanation:
The binary representation of 15 is
Example 3:
Input: n = 3
Output: 3
Explanation:
The binary representation of 3 is
Constraints:
•
  3370. Smallest Number With All Set Bits
Topic: Math, Bit Manipulation
Difficulty: Easy
Problem:
You are given a positive number
n.Return the smallest number
x greater than or equal to n, such that the binary representation of x contains only set bitsExample 1:
Input: n = 5
Output: 7
Explanation:
The binary representation of 7 is
"111".Example 2:
Input: n = 10
Output: 15
Explanation:
The binary representation of 15 is
"1111".Example 3:
Input: n = 3
Output: 3
Explanation:
The binary representation of 3 is
"11".Constraints:
•
1 <= n <= 10002025-10-30
1526. Minimum Number of Increments on Subarrays to Form a Target Array
Topic: Array, Dynamic Programming, Stack, Greedy, Monotonic Stack
Difficulty: Hard
Problem:
You are given an integer array
In one operation you can choose any subarray from
Return the minimum number of operations to form a
The test cases are generated so that the answer fits in a 32-bit integer.
Example 1:
Example 2:
Example 3:
Constraints:
•
•
  1526. Minimum Number of Increments on Subarrays to Form a Target Array
Topic: Array, Dynamic Programming, Stack, Greedy, Monotonic Stack
Difficulty: Hard
Problem:
You are given an integer array
target. You have an integer array initial of the same size as target with all elements initially zeros.In one operation you can choose any subarray from
initial and increment each value by one.Return the minimum number of operations to form a
target array from initial.The test cases are generated so that the answer fits in a 32-bit integer.
Example 1:
Input: target = [1,2,3,2,1]
Output: 3
Explanation: We need at least 3 operations to form the target array from the initial array.
[0,0,0,0,0] increment 1 from index 0 to 4 (inclusive).
[1,1,1,1,1] increment 1 from index 1 to 3 (inclusive).
[1,2,2,2,1] increment 1 at index 2.
[1,2,3,2,1] target array is formed.
Example 2:
Input: target = [3,1,1,2]
Output: 4
Explanation: [0,0,0,0] -> [1,1,1,1] -> [1,1,1,2] -> [2,1,1,2] -> [3,1,1,2]
Example 3:
Input: target = [3,1,5,4,2]
Output: 7
Explanation: [0,0,0,0,0] -> [1,1,1,1,1] -> [2,1,1,1,1] -> [3,1,1,1,1] -> [3,1,2,2,2] -> [3,1,3,3,2] -> [3,1,4,4,2] -> [3,1,5,4,2].
Constraints:
•
1 <= target.length <= 10^5•
1 <= target[i] <= 10^52025-10-31
3289. The Two Sneaky Numbers of Digitville
Topic: Array, Hash Table, Math
Difficulty: Easy
Problem:
In the town of Digitville, there was a list of numbers called
As the town detective, your task is to find these two sneaky numbers. Return an array of size two containing the two numbers (in any order), so peace can return to Digitville.
Example 1:
Input: nums = 0,1,1,0
Output: 0,1
Explanation:
The numbers 0 and 1 each appear twice in the array.
Example 2:
Input: nums = 0,3,2,1,3,2
Output: 2,3
Explanation:
The numbers 2 and 3 each appear twice in the array.
Example 3:
Input: nums = 7,1,5,4,3,4,6,0,9,5,8,2
Output: 4,5
Explanation:
The numbers 4 and 5 each appear twice in the array.
Constraints:
•
•
•
• The input is generated such that
  3289. The Two Sneaky Numbers of Digitville
Topic: Array, Hash Table, Math
Difficulty: Easy
Problem:
In the town of Digitville, there was a list of numbers called
nums containing integers from 0 to n - 1. Each number was supposed to appear exactly once in the list, however, two mischievous numbers sneaked in an additional time, making the list longer than usual.As the town detective, your task is to find these two sneaky numbers. Return an array of size two containing the two numbers (in any order), so peace can return to Digitville.
Example 1:
Input: nums = 0,1,1,0
Output: 0,1
Explanation:
The numbers 0 and 1 each appear twice in the array.
Example 2:
Input: nums = 0,3,2,1,3,2
Output: 2,3
Explanation:
The numbers 2 and 3 each appear twice in the array.
Example 3:
Input: nums = 7,1,5,4,3,4,6,0,9,5,8,2
Output: 4,5
Explanation:
The numbers 4 and 5 each appear twice in the array.
Constraints:
•
2 <= n <= 100•
nums.length == n + 2•
0 <= nums[i] < n• The input is generated such that
nums contains exactly two repeated elements.