You are given a 0-indexed array of positive integers nums
. Find the number of triplets (i, j, k)
that meet the following conditions:
0 <= i < j < k < nums.length
nums[i]
, nums[j]
, and nums[k]
are pairwise distinct.nums[i] != nums[j]
, nums[i] != nums[k]
, and nums[j] != nums[k]
.Return the number of triplets that meet the conditions.
Example 1:
Input: nums = [4,4,2,4,3] Output: 3 Explanation: The following triplets meet the conditions: - (0, 2, 4) because 4 != 2 != 3 - (1, 2, 4) because 4 != 2 != 3 - (2, 3, 4) because 2 != 4 != 3 Since there are 3 triplets, we return 3. Note that (2, 0, 4) is not a valid triplet because 2 > 0.
Example 2:
Input: nums = [1,1,1,1,1] Output: 0 Explanation: No triplets meet the conditions so we return 0.
Constraints:
3 <= nums.length <= 100
1 <= nums[i] <= 1000
program main
! This program solves the problem described in the statement.
! It runs with all provided examples and outputs to stdout.
implicit none
integer :: nums(5)
integer :: i, j, k
integer :: count
nums = (/ 4, 4, 2, 4, 3 /)
count = count_triplets(nums)
print '(I0)', count
nums = (/ 1, 1, 1, 1, 1 /)
count = count_triplets(nums)
print '(I0)', count
contains
function count_triplets(nums) result(count)
implicit none
integer, intent(in) :: nums(:)
integer :: count
integer :: i, j, k
logical :: pairwise_distinct
count = 0
do i = 0, size(nums)-3
do j = i+1, size(nums)-2
do k = j+1, size(nums)-1
if (nums(i) /= nums(j) .and. &
nums(i) /= nums(k) .and. &
nums(j) /= nums(k)) then
count = count + 1
end if
end do
end do
end do
end function count_triplets
end program main
3 0
module solution
implicit none
contains
integer function solution(nums) result(count)
integer, intent(in) :: nums(:)
integer :: i, j, k
integer :: n
n = size(nums)
count = 0
do i = 0, n - 3
do j = i + 1, n - 2
do k = j + 1, n - 1
if (nums(i) /= nums(j) .and. nums(i) /= nums(k) .and. nums(j) /= nums(k)) then
count = count + 1
end if
end do
end do
end do
end function solution
end module solution
program test
use solution
implicit none
integer, parameter :: nums = [4, 4, 2, 4, 3]
integer :: result
result = solution(nums)
write (*,*) "Result: ", result
end program test
temp.f95:4:27: 4 | integer function solution(nums) result(count) | 1 Error: MODULE attribute of ‘solution’ conflicts with PROCEDURE attribute at (1) temp.f95:5:34: 5 | integer, intent(in) :: nums(:) | 1 Error: Unexpected data declaration statement in CONTAINS section at (1) temp.f95:6:22: 6 | integer :: i, j, k | 1 Error: Unexpected data declaration statement in CONTAINS section at (1) temp.f95:7:16: 7 | integer :: n | 1 Error: Unexpected data declaration statement in CONTAINS section at (1) temp.f95:8:18: 8 | n = size(nums) | 1 Error: Unexpected assignment statement in CONTAINS section at (1) temp.f95:9:13: 9 | count = 0 | 1 Error: Unexpected assignment statement in CONTAINS section at (1) temp.f95:10:19: 10 | do i = 0, n - 3 | 1 Error: Unexpected DO statement in CONTAINS section at (1) temp.f95:11:25: 11 | do j = i + 1, n - 2 | 1 Error: Unexpected DO statement in CONTAINS section at (1) temp.f95:12:27: 12 | do k = j + 1, n - 1 | 1 Error: Unexpected DO statement in CONTAINS section at (1) temp.f95:13:88: 13 | if (nums(i) /= nums(j) .and. nums(i) /= nums(k) .and. nums(j) /= nums(k)) then | 1 Error: Unexpected block IF statement in CONTAINS section at (1) temp.f95:14:29: 14 | count = count + 1 | 1 Error: Unexpected assignment statement in CONTAINS section at (1) temp.f95:15:13: 15 | end if | 1 Error: Expecting END MODULE statement at (1) temp.f95:16:11: 16 | end do | 1 Error: Expecting END MODULE statement at (1) temp.f95:17:9: 17 | end do | 1 Error: Expecting END MODULE statement at (1) temp.f95:18:7: 18 | end do | 1 Error: Expecting END MODULE statement at (1) temp.f95:19:5: 19 | end function solution | 1 Error: Expecting END MODULE statement at (1) temp.f95:23:7: 23 | use solution | 1 Fatal Error: Cannot open module file ‘solution.mod’ for reading at (1): No such file or directory compilation terminated.
def countTriplets(nums):
count = 0
n = len(nums)
for i in range(n):
for j in range(i + 1, n):
for k in range(j + 1, n):
if nums[i] != nums[j] and nums[i] != nums[k] and nums[j] != nums[k]:
count += 1
return count
The given problem can be solved by iterating over each possible triplet (i, j, k), with the range 0 <= i < j < k < nums.length, and checking if the elements at indices i, j, and k are distinct. The algorithm runs in O(n^3) time due to the nested loops, where n is the size of the input array. If the elements at i, j, and k are distinct, we increment the count by 1. Finally, we return the total count of the triplets after iterating through all possible combinations.
The algorithm remains the same across all languages; the only difference is the syntax in each language.
int countTriplets(vector<int>& nums) {
int count = 0;
int n = nums.size();
for(int i = 0; i < n; i++) {
for(int j = i + 1; j < n; j++) {
for(int k = j + 1; k < n; k++) {
if(nums[i] != nums[j] && nums[i] != nums[k] && nums[j] != nums[k]) {
count++;
}
}
}
}
return count;
}
The given problem can be solved by iterating over each possible triplet (i, j, k), with the range 0 <= i < j < k < nums.length, and checking if the elements at indices i, j, and k are distinct. The algorithm runs in O(n^3) time due to the nested loops, where n is the size of the input array. If the elements at i, j, and k are distinct, we increment the count by 1. Finally, we return the total count of the triplets after iterating through all possible combinations.
The algorithm remains the same across all languages; the only difference is the syntax in each language.