Given an integer array arr
, count how many elements x
there are, such that x + 1
is also in arr
. If there are duplicates in arr
, count them separately.
Example 1:
Input: arr = [1,2,3] Output: 2 Explanation: 1 and 2 are counted cause 2 and 3 are in arr.
Example 2:
Input: arr = [1,1,3,3,5,5,7,7] Output: 0 Explanation: No numbers are counted, cause there is no 2, 4, 6, or 8 in arr.
Constraints:
1 <= arr.length <= 1000
0 <= arr[i] <= 1000
program main
implicit none
integer, parameter :: n = 3
integer, dimension(n) :: arr = (/ 1, 2, 3 /)
integer :: x
integer :: count
count = count_elements(arr)
write (*, '(I0)') count
contains
function count_elements(arr) result(count)
implicit none
integer, intent(in) :: arr(:)
integer :: count
integer :: i
logical :: found
count = 0
do i = 1, size(arr) - 1
if (any(arr(i+1:) == arr(i))) then
count = count + 1
end if
end do
end function count_elements
end program main
0
module count_pairs
implicit none
private
public :: count_pairs
contains
subroutine count_pairs(arr, n, result)
implicit none
integer, intent(in) :: arr(n)
integer, intent(in) :: n
integer, intent(out) :: result
integer :: i, j, count
count = 0
do i = 1, n - 1
do j = i + 1, n
if (arr(i) == arr(j) - 1) then
count = count + 1
end if
end do
end do
result = count
end subroutine count_pairs
end module count_pairs
program test_count_pairs
use count_pairs
implicit none
integer, parameter :: n = 3
integer :: arr(n) = [1, 2, 3]
integer :: result
call count_pairs(arr, n, result)
write (*, '(I0)') result
end program test_count_pairs
program test_count_pairs_2
use count_pairs
implicit none
integer, parameter :: n = 8
integer :: arr(n) = [1, 1, 3, 3, 5, 5, 7, 7]
integer :: result
call count_pairs(arr, n, result)
write (*, '(I0)') result
end program test_count_pairs_2
temp.f95:4:25: 4 | public :: count_pairs | 1 Error: PUBLIC attribute applied to MODULE count_pairs at (1) temp.f95:6:26: 6 | subroutine count_pairs(arr, n, result) | 1 Error: MODULE attribute of ‘count_pairs’ conflicts with PROCEDURE attribute at (1) temp.f95:7:21: 7 | implicit none | 1 Error: Unexpected IMPLICIT NONE statement in CONTAINS section at (1) temp.f95:8:37: 8 | integer, intent(in) :: arr(n) | 1 Error: Explicit array shape at (1) must be constant of INTEGER type and not UNKNOWN type temp.f95:9:32: 9 | integer, intent(in) :: n | 1 Error: Unexpected data declaration statement in CONTAINS section at (1) temp.f95:10:38: 10 | integer, intent(out) :: result | 1 Error: Unexpected data declaration statement in CONTAINS section at (1) temp.f95:11:30: 11 | integer :: i, j, count | 1 Error: Unexpected data declaration statement in CONTAINS section at (1) temp.f95:13:17: 13 | count = 0 | 1 Error: Unexpected assignment statement in CONTAINS section at (1) temp.f95:14:23: 14 | do i = 1, n - 1 | 1 Error: Unexpected DO statement in CONTAINS section at (1) temp.f95:15:27: 15 | do j = i + 1, n | 1 Error: Unexpected DO statement in CONTAINS section at (1) temp.f95:16:46: 16 | if (arr(i) == arr(j) - 1) then | 1 Error: Unexpected block IF statement in CONTAINS section at (1) temp.f95:17:37: 17 | count = count + 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:22:22: 22 | result = count | 1 Error: Unexpected assignment statement in CONTAINS section at (1) temp.f95:23:7: 23 | end subroutine count_pairs | 1 Error: Expecting END MODULE statement at (1) temp.f95:27:9: 27 | use count_pairs | 1 Fatal Error: Cannot open module file ‘count_pairs.mod’ for reading at (1): No such file or directory compilation terminated.
def sum_zero(n):
res = []
for i in range(1, (n // 2) + 1):
res.append(i)
res.append(-i)
if n % 2 != 0:
res.append(0)
return res
The algorithm iterates from 1 to n/2 (inclusive). For each number i, it appends both i and -i to the result array, making their sum equal to 0. If the given integer n is odd, then there is one more number remaining which is 0, so we append 0 to the result array. By doing this, we create a unique array of integers adding up to 0. The time complexity of this algorithm is O(n/2) = O(n), and the space complexity is also O(n) as the size of the result array is equal to the given input n.
#include <vector>
std::vector<int> sumZero(int n) {
std::vector<int> res;
for (int i = 1; i <= n / 2; i++) {
res.push_back(i);
res.push_back(-i);
}
if (n % 2 != 0) {
res.push_back(0);
}
return res;
}
The algorithm iterates from 1 to n/2 (inclusive). For each number i, it appends both i and -i to the result array, making their sum equal to 0. If the given integer n is odd, then there is one more number remaining which is 0, so we append 0 to the result array. By doing this, we create a unique array of integers adding up to 0. The time complexity of this algorithm is O(n/2) = O(n), and the space complexity is also O(n) as the size of the result array is equal to the given input n.