Module challenge
Challenge: brute force first
15 min · 3 steps
Step 1 of 3 · Write the code
solution(nums, k)Return how many pairs
(i, j)withi < jhavenums[i] + nums[j]divisible byk.Example:
solution([1, 2, 3, 4], 3)returns2, the pairs (1, 2) and (2, 4).Constraints:
0 ≤ nums.length ≤ 1000 ≤ nums[i] ≤ 10001 ≤ k ≤ 100
function solution(nums, k) { }Step 2 of 3 · Write the code
solution(prices)prices[i]is the price on dayi. You buy on one day, then sell on a later day. Return the biggest profit. If you can't make a profit, return0.Example:
solution([7, 1, 5, 3, 6])returns5(buy at 1, sell at 6).Constraints:
0 ≤ prices.length ≤ 10000 ≤ prices[i] ≤ 10⁴
function solution(prices) { }Step 3 of 3 · Write the code
solution(nums)Return the smallest difference (absolute value) between two different items in
nums.Example:
solution([4, 9, 1, 7])returns2(between 9 and 7).Constraints:
2 ≤ nums.length ≤ 1000-10⁶ ≤ nums[i] ≤ 10⁶
function solution(nums) { }