Minimum Distance to the Target Element

🏠 ⬅️ ➡️

Given an integer array nums (0-indexed) and two integers target and start, find an index i such that nums[i] == target and abs(i - start) is minimized. Note that abs(x) is the absolute value of x.

Return abs(i - start).

It is guaranteed that target exists in nums.

Example 1:

Input: nums = [1,2,3,4,5], target = 5, start = 3 Output: 1 Explanation: nums[4] = 5 is the only value equal to target, so the answer is abs(4 - 3) = 1.

Example 2:

Input: nums = [1], target = 1, start = 0 Output: 0 Explanation: nums[0] = 1 is the only value equal to target, so the answer is abs(0 - 0) = 0.

Example 3:

Input: nums = [1,1,1,1,1,1,1,1,1,1], target = 1, start = 0 Output: 0 Explanation: Every value of nums is 1, but nums[0] minimizes abs(i - start), which is abs(0 - 0) = 0.

Constraints:

  • 1 <= nums.length <= 1000
  • 1 <= nums[i] <= 104
  • 0 <= start < nums.length
  • target is in nums.

Note: This problem is from LeetCode.
Compiled
Executed
Correct
program main
    implicit none

    integer, parameter :: n = 10
    integer, dimension(n) :: nums
    integer :: i, target, start, ans

    ! Example 1
    nums = [ (i, i=1,n) ]
    target = 5
    start = 3
    ans = solve(nums, target, start)
    write (*, '(A, I0)') 'Example 1: ', ans

    ! Example 2
    nums = [1]
    target = 1
    start = 0
    ans = solve(nums, target, start)
    write (*, '(A, I0)') 'Example 2: ', ans

    ! Example 3
    nums = [ (i, i=1,n) ]
    target = 1
    start = 0
    ans = solve(nums, target, start)
    write (*, '(A, I0)') 'Example 3: ', ans

contains

    function solve(nums, target, start) result(ans)
        implicit none
        integer, intent(in) :: nums(:), target, start
        integer :: ans
        integer :: i, j, min_diff

        min_diff = huge(1)
        do i = 1, size(nums)
            if (nums(i) == target) then
                j = i - start
                if (j >= 0) then
                    min_diff = min(min_diff, j)
                else
                    min_diff = min(min_diff, -j)
                end if
            end if
        end do

        ans = abs(min_diff)
    end function solve

end program main
Compiled
Executed
Correct
module MinAbsDistance
  implicit none
contains
  function min_abs_distance(nums, target, start) result(abs_distance)
    integer, intent(in) :: nums(:)
    integer, intent(in) :: target
    integer, intent(in) :: start
    integer :: abs_distance
    integer :: i

    ! Find the index of the target value in the array
    do i = 1, size(nums)
      if (nums(i) == target) exit
    end do

    ! Calculate the absolute distance between the index and the start index
    abs_distance = abs(i - start)
  end function min_abs_distance
end module MinAbsDistance

program test_min_abs_distance
  use MinAbsDistance
  implicit none
  integer, parameter :: nums(5) = [1, 2, 3, 4, 5]
  integer :: abs_distance

  ! Test case 1:
  abs_distance = min_abs_distance(nums, 5, 3)
  write (*,*) 'Test case 1:', abs_distance

  ! Test case 2:
  abs_distance = min_abs_distance(nums, 1, 0)
  write (*,*) 'Test case 2:', abs_distance

  ! Test case 3:
  abs_distance = min_abs_distance(nums, 1, 0)
  write (*,*) 'Test case 3:', abs_distance
end program test_min_abs_distance
🌐 Data from online sources
def sum_of_unique_elements(nums):
    elem_count = {}
    for num in nums:
        elem_count[num] = elem_count.get(num, 0) + 1
    sum = 0
    for elem, count in elem_count.items():
        if count == 1:
            sum += elem
    return sum
The algorithm starts by creating an empty hash map (or dictionary in Python) to store the count of each element in the input array. After that, it iterates through the input array and increments the count of each element in the hash map. Finally, it iterates through the hash map and checks the count of each element. If the count is 1, it adds the element to the sum. Once iteration is complete, the total sum of unique elements is returned.
🌐 Data from online sources
int sumOfUniqueElements(const std::vector<int>& nums) {
    std::unordered_map<int, int> elemCount;
    for (const int num : nums) {
        elemCount[num]++;
    }
    int sum = 0;
    for (const auto &[elem, count] : elemCount) {
        if (count == 1) {
            sum += elem;
        }
    }
    return sum;
}
The algorithm starts by creating an empty hash map (or dictionary in Python) to store the count of each element in the input array. After that, it iterates through the input array and increments the count of each element in the hash map. Finally, it iterates through the hash map and checks the count of each element. If the count is 1, it adds the element to the sum. Once iteration is complete, the total sum of unique elements is returned.