Robbie Hatley's Solutions To The Weekly Challenge #243

For those not familiar with "The Weekly Challenge", it is a weekly programming puzzle with two parts, cycling every Sunday. You can find it here:

The Weekly Challenge

This week (2023-11-12 through 2023-11-18) is weekly challenge #243.

Both of these proved easy to solve by using a pair of three-part loops each:

Task 243-1: Reverse Pairs
Submitted by: Mohammad S Anwar
You are given an array of integers. Write a script to return the
number of "reverse pairs" in the given array. A "reverse pair"
is a pair (i, j) obeying both of the following:
a) 0 <= i < j < nums.length and
b) nums[i] > 2 * nums[j].

Example 1:
Input: @nums = (1, 3, 2, 3, 1)
Output: 2
(1, 4) => nums[1] = 3, nums[4] = 1, 3 > 2 * 1
(3, 4) => nums[3] = 3, nums[4] = 1, 3 > 2 * 1

Example 2:
Input: @nums = (2, 4, 3, 5, 1)
Output: 3
(1, 4) => nums[1] = 4, nums[4] = 1, 4 > 2 * 1
(2, 4) => nums[2] = 3, nums[4] = 1, 3 > 2 * 1
(3, 4) => nums[3] = 5, nums[4] = 1, 5 > 2 * 1

I used two three-part loops to check all pairs and find all "reverse pairs":

Robbie Hatley's Solution to The Weekly Challenge 243-1

Task 243-2: Floor Sum
Submitted by: Mohammad S Anwar
You are given an array of positive integers (>=1). Write a
script to return the sum of floor(nums[i] / nums[j]) where
0 <= i,j < nums.length. The floor() function returns the
integer part of the division.

Example 1:
Input: @nums = (2, 5, 9)
Output: 10
floor(2 / 5) = 0
floor(2 / 9) = 0
floor(5 / 9) = 0
floor(2 / 2) = 1
floor(5 / 5) = 1
floor(9 / 9) = 1
floor(5 / 2) = 2
floor(9 / 2) = 4
floor(9 / 5) = 1

Example 2:
Input: @nums = (7, 7, 7, 7, 7, 7, 7)
Output: 49

I used two three-part loops to sum all floors of quotients of pairs:

Robbie Hatley's Solution to The Weekly Challenge 243-2

That's it for 243; see you on 244!

Comments

Popular posts from this blog

Robbie Hatley's Solutions To The Weekly Challenge #262

Robbie Hatley's Solutions To The Weekly Challenge #266

Robbie Hatley's Solutions To The Weekly Challenge #273