Skip to content
Brute force first

Module challenge

Challenge: brute force first

15 min · 3 steps

  1. Step 1 of 3 · Write the code

    solution(nums, k)

    Return how many pairs (i, j) with i < j have nums[i] + nums[j] divisible by k.

    Example: solution([1, 2, 3, 4], 3) returns 2, the pairs (1, 2) and (2, 4).

    Constraints:

    • 0 ≤ nums.length ≤ 100
    • 0 ≤ nums[i] ≤ 1000
    • 1 ≤ k ≤ 100
    function solution(nums, k) { }
  2. Step 2 of 3 · Write the code

    solution(prices)

    prices[i] is the price on day i. You buy on one day, then sell on a later day. Return the biggest profit. If you can't make a profit, return 0.

    Example: solution([7, 1, 5, 3, 6]) returns 5 (buy at 1, sell at 6).

    Constraints:

    • 0 ≤ prices.length ≤ 1000
    • 0 ≤ prices[i] ≤ 10⁴
    function solution(prices) { }
  3. 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]) returns 2 (between 9 and 7).

    Constraints:

    • 2 ≤ nums.length ≤ 1000
    • -10⁶ ≤ nums[i] ≤ 10⁶
    function solution(nums) { }