Counting Elements

🏠 ⬅️ ➡️

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

Note: This problem is from LeetCode.
Compiled
Executed
Correct
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
Compiled
Executed
Correct
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
🌐 Data from online sources
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.

🌐 Data from online sources
#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.