[Leetcode]1590. Make Sum Divisible by P

LeetCode_logo
Given an array of positive integers nums, remove the smallest subarray (possibly empty) such that the sum of the remaining elements is divisible by p. It is not allowed to remove the whole array.
Return the length of the smallest subarray that you need to remove, or -1 if it's impossible.
A subarray is defined as a contiguous block of elements in the array.
Continue reading “[Leetcode]1590. Make Sum Divisible by P”

[Leetcode]31. Next Permutation

LeetCode_logo
Implement next permutation, which rearranges numbers into the lexicographically next greater permutation of numbers.
If such arrangement is not possible, it must rearrange it as the lowest possible order (ie, sorted in ascending order).
The replacement must be in-place and use only constant extra memory.
Here are some examples. Inputs are in the left-hand column and its corresponding outputs are in the right-hand column.
1,2,3 → 1,3,2
3,2,1 → 1,2,3
1,1,5 → 1,5,1
Continue reading “[Leetcode]31. Next Permutation”

[Leetcode]1513. Number of Substrings With Only 1s

LeetCode_logo
Given a binary string s (a string consisting only of '0' and '1's).
Return the number of substrings with all characters 1's.
Since the answer may be too large, return it modulo 10^9 + 7.
 
Example 1:
Input: s = "0110111" Output: 9 Explanation: There are 9 substring in total with only 1's characters. "1" -> 5 times. "11" -> 3 times. "111" -> 1 time.
Continue reading “[Leetcode]1513. Number of Substrings With Only 1s”

[Leetcode]1497. Check If Array Pairs Are Divisible by k

LeetCode_logo
Given an array of integers arr of even length n and an integer k.
We want to divide the array into exactly n / 2 pairs such that the sum of each pair is divisible by k.
Return True If you can find a way to do that or False otherwise.
 
Example 1:
Input: arr = [1,2,3,4,5,10,6,7,8,9], k = 5 Output: true 
Explanation: Pairs are (1,9),(2,8),(3,7),(4,6) and (5,10).
Continue reading “[Leetcode]1497. Check If Array Pairs Are Divisible by k”