Given a 2D integer array nums where nums[i] is a non-empty array of distinct positive integers, return the list of integers that are present in each array of nums sorted in ascending order.

Example 1:

Input: nums = [[3,1,2,4,5],[1,2,3,4],[3,4,5,6]] Output: [3,4] Explanation: The only integers present in each of nums[0] = [3,1,2,4,5], nums[1] = [1,2,3,4], and nums[2] = [3,4,5,6] are 3 and 4, so we return [3,4].

Example 2:

Input: nums = [[1,2,3],[4,5,6]] Output: [] Explanation: There does not exist any integer present both in nums[0] and nums[1], so we return an empty list [].

Constraints:

  • 1 <= nums.length <= 1000
  • 1 <= sum(nums[i].length) <= 1000
  • 1 <= nums[i][j] <= 1000
  • All the values of nums[i] are unique.

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

    integer, parameter :: n = 3
    integer, dimension(n) :: nums(n) = reshape([3, 1, 2, 4, 5, 1, 2, 3, 4, 3, 4, 5, 6], shape(nums))
    integer, allocatable :: result(:)

    call solve(nums, result)

    write (*, '(*(g0, :, ", "))') result

contains

    subroutine solve(nums, result)
        implicit none
        integer, intent(in) :: nums(:, :)
        integer, allocatable, intent(out) :: result(:)
        integer :: i, j, k
        logical :: found

        allocate(result(size(nums)))

        do i = 1, size(nums)
            found = .false.
            do j = 1, size(nums)
                if (all(nums(i) == nums(j))) then
                    found = .true.
                    exit
                end if
            end do
            if (.not. found) then
                k = k + 1
                result(k) = nums(i)(1)
            end if
        end do

        result = sort(result)
    end subroutine solve

end program main
โŒ Compiled
โŒ Executed
โŒ Correct
program main
      implicit none

      integer, parameter :: n = 3
      integer, parameter :: m = 5
      integer, parameter :: nums(n, m) = reshape((/ 3, 1, 2, 4, 5, 1, 2, 3, 4, 3, 4, 5, 6 /), shape(nums))

      integer :: i, j
      integer :: result(size(nums, 1))

      result = solve(nums)

      do i = 1, size(nums, 1)
        write (*, '(A, I0, A)') 'nums(', i, ') = ', result(i)
      end do

      contains

      function solve(nums) result(result)
        integer, intent(in) :: nums(n, m)
        integer :: result(size(nums, 1))

        integer :: i, j
        integer :: count(size(nums, 2))

        count = 0

        do i = 1, size(nums, 1)
          do j = 1, size(nums, 2)
            if (count(nums(i, j)) == 0) then
              count(nums(i, j)) = 1
            else
              count(nums(i, j)) = count(nums(i, j)) + 1
            end if
          end do
        end do

        result = pack(count, count > 0)

      end function solve

      end program main
๐ŸŒ Data from online sources
def min_cost(cost):
    cost.sort(reverse=True)
    n = len(cost)
    min_cost = 0

    for i in range(0, n, 3):
        min_cost += cost[i]
        if i+1 < n:
            min_cost += cost[i+1]

    return min_cost

The algorithm sorts all the candies in the array in descending order of cost, so that the most expensive candies are bought first. Then, it loops over the array, taking two candies at a time (the loop increments 3 positions each iteration) and adding their cost to the total min_cost. The choice of the third free candy will always be one of the two already chosen, so its cost is already included in min_cost. After iterating through the entire array, the function returns the minimum cost.

๐ŸŒ Data from online sources
#include <vector>
#include <algorithm>

int minCost(std::vector<int>& cost) {
    std::sort(cost.begin(), cost.end(), std::greater<>());
    int n = cost.size();

    int min_cost = 0;

    for(int i = 0; i < n; i += 3) {
        min_cost += cost[i];
        if(i+1 < n) min_cost += cost[i+1];
    }

    return min_cost;
}

The algorithm sorts all the candies in the array in descending order of cost, so that the most expensive candies are bought first. Then, it loops over the array, taking two candies at a time (the loop increments 3 positions each iteration) and adding their cost to the total min_cost. The choice of the third free candy will always be one of the two already chosen, so its cost is already included in min_cost. After iterating through the entire array, the function returns the minimum cost.