Given a 0-indexed integer array nums
of length n
and an integer k
, return the number of pairs (i, j)
where 0 <= i < j < n
, such that nums[i] == nums[j]
and (i * j)
is divisible by k
.
Example 1:
Input: nums = [3,1,2,2,2,1,3], k = 2 Output: 4 Explanation: There are 4 pairs that meet all the requirements: - nums[0] == nums[6], and 0 * 6 == 0, which is divisible by 2. - nums[2] == nums[3], and 2 * 3 == 6, which is divisible by 2. - nums[2] == nums[4], and 2 * 4 == 8, which is divisible by 2. - nums[3] == nums[4], and 3 * 4 == 12, which is divisible by 2.
Example 2:
Input: nums = [1,2,3,4], k = 1 Output: 0 Explanation: Since no value in nums is repeated, there are no pairs (i,j) that meet all the requirements.
Constraints:
1 <= nums.length <= 100
1 <= nums[i], k <= 100
program main
implicit none
integer, parameter :: n = 7
integer, parameter :: k = 2
integer, dimension(n) :: nums
integer :: i, j
integer :: count
! Example 1
nums = [3, 1, 2, 2, 2, 1, 3]
write (*, '(A)') 'Example 1:'
write (*, '(A)') 'Input: nums = [3,1,2,2,2,1,3], k = 2'
write (*, '(A)') 'Output: ', solve(nums, k)
write (*, '(A)') 'Expected: 4'
! Example 2
nums = [1, 2, 3, 4]
write (*, '(A)') 'Example 2:'
write (*, '(A)') 'Input: nums = [1,2,3,4], k = 1'
write (*, '(A)') 'Output: ', solve(nums, k)
write (*, '(A)') 'Expected: 0'
contains
function solve(nums, k) result(count)
implicit none
integer, intent(in) :: nums(:), k
integer :: count
integer :: i, j
count = 0
do i = 1, size(nums) - 1
do j = i + 1, size(nums)
if (nums(i) == nums(j)) then
if (mod((i - 1) * j, k) == 0) then
count = count + 1
end if
end if
end do
end do
end function solve
end program main
temp.f95:18:4: 18 | nums = [1, 2, 3, 4] | 1 Error: Different shape for array assignment at (1) on dimension 1 (7 and 4)
module pair_with_divisible_product
implicit none
private
public :: pair_with_divisible_product
contains
subroutine pair_with_divisible_product(nums, k, result)
integer, intent(in) :: nums(:)
integer, intent(in) :: k
integer, intent(out) :: result
integer :: i, j, product
result = 0
do i = 1, size(nums) - 1
do j = i + 1, size(nums)
product = nums(i) * nums(j)
if (product == k) then
result = result + 1
end if
end do
end do
end subroutine pair_with_divisible_product
end module pair_with_divisible_product
program test_pair_with_divisible_product
use pair_with_divisible_product
implicit none
integer, parameter :: n = 7
integer, parameter :: k = 2
integer :: nums(n) = [3, 1, 2, 2, 2, 1, 3]
integer :: result
call pair_with_divisible_product(nums, k, result)
write (*,*) "Result:", result
nums = [1, 2, 3, 4]
k = 1
call pair_with_divisible_product(nums, k, result)
write (*,*) "Result:", result
end program test_pair_with_divisible_product
temp.f95:4:41: 4 | public :: pair_with_divisible_product | 1 Error: PUBLIC attribute applied to MODULE pair_with_divisible_product at (1) temp.f95:6:42: 6 | subroutine pair_with_divisible_product(nums, k, result) | 1 Error: MODULE attribute of ‘pair_with_divisible_product’ conflicts with PROCEDURE attribute at (1) temp.f95:7:38: 7 | integer, intent(in) :: nums(:) | 1 Error: Unexpected data declaration statement in CONTAINS section at (1) temp.f95:8:32: 8 | integer, intent(in) :: k | 1 Error: Unexpected data declaration statement in CONTAINS section at (1) temp.f95:9:38: 9 | integer, intent(out) :: result | 1 Error: Unexpected data declaration statement in CONTAINS section at (1) temp.f95:10:32: 10 | integer :: i, j, product | 1 Error: Unexpected data declaration statement in CONTAINS section at (1) temp.f95:12:18: 12 | result = 0 | 1 Error: Unexpected assignment statement in CONTAINS section at (1) temp.f95:13:32: 13 | do i = 1, size(nums) - 1 | 1 Error: Unexpected DO statement in CONTAINS section at (1) temp.f95:14:36: 14 | do j = i + 1, size(nums) | 1 Error: Unexpected DO statement in CONTAINS section at (1) temp.f95:15:43: 15 | product = nums(i) * nums(j) | 1 Error: Unexpected assignment statement in CONTAINS section at (1) temp.f95:16:38: 16 | if (product == k) then | 1 Error: Unexpected block IF statement in CONTAINS section at (1) temp.f95:17:39: 17 | result = result + 1 | 1 Error: Unexpected assignment statement in CONTAINS section at (1) temp.f95:18:19: 18 | end if | 1 Error: Expecting END MODULE statement at (1) temp.f95:19:15: 19 | end do | 1 Error: Expecting END MODULE statement at (1) temp.f95:20:11: 20 | end do | 1 Error: Expecting END MODULE statement at (1) temp.f95:21:7: 21 | end subroutine pair_with_divisible_product | 1 Error: Expecting END MODULE statement at (1) temp.f95:25:9: 25 | use pair_with_divisible_product | 1 Fatal Error: Cannot open module file ‘pair_with_divisible_product.mod’ for reading at (1): No such file or directory compilation terminated.
def min_months(n, relations, time):
order = [0] * n
for r in relations:
order[r[1] - 1] = max(order[r[1] - 1], r[0])
totalTime = 0
for i in range(n):
totalTime = max(totalTime, time[i] + order[i])
return totalTime
The algorithm starts by creating an array order
to keep track of the maximum prerequisite course number for each course. For each relation, it updates the maximum prerequisite course number for the nextCourse
with the prevCourse
.
Next, go through each course, and calculate the total time needed to finish that course by adding the course's time plus the time of its maximum prerequisite course. Track the maximum time across all courses as totalTime
.
Finally, return the maximum time totalTime
as the minimum number of months needed to complete all courses.
#include <vector>
#include <algorithm>
int min_months(int n, std::vector<std::vector<int>>& relations, std::vector<int>& time) {
std::vector<int> order(n, 0);
for (auto& r : relations)
order[r[1] - 1] = std::max(order[r[1] - 1], r[0]);
int totalTime = 0;
for (int i = 0; i < n; ++i)
totalTime = std::max(totalTime, time[i] + order[i]);
return totalTime;
}
The algorithm starts by creating an array order
to keep track of the maximum prerequisite course number for each course. For each relation, it updates the maximum prerequisite course number for the nextCourse
with the prevCourse
.
Next, go through each course, and calculate the total time needed to finish that course by adding the course's time plus the time of its maximum prerequisite course. Track the maximum time across all courses as totalTime
.
Finally, return the maximum time totalTime
as the minimum number of months needed to complete all courses.