You are given an integer array digits, where each element is a digit. The array may contain duplicates.
You need to find all the unique integers that follow the given requirements:
digits in any arbitrary order.For example, if the given digits were [1, 2, 3], integers 132 and 312 follow the requirements.
Return a sorted array of the unique integers.
Example 1:
Input: digits = [2,1,3,0] Output: [102,120,130,132,210,230,302,310,312,320] Explanation: All the possible integers that follow the requirements are in the output array. Notice that there are no odd integers or integers with leading zeros.
Example 2:
Input: digits = [2,2,8,8,2] Output: [222,228,282,288,822,828,882] Explanation: The same digit can be used as many times as it appears in digits. In this example, the digit 8 is used twice each time in 288, 828, and 882.
Example 3:
Input: digits = [3,7,5] Output: [] Explanation: No even integers can be formed using the given digits.
Constraints:
3 <= digits.length <= 1000 <= digits[i] <= 9program main
    implicit none
    integer, parameter :: n = 100
    integer :: digits(n)
    integer :: i, j, k
    logical :: found
    integer :: unique_ints(n)
    integer :: num_unique_ints
    ! Example 1:
    digits = [2, 1, 3, 0]
    call solve(digits, unique_ints, num_unique_ints)
    print '(A, I0)', 'Example 1: ', num_unique_ints
    do i = 1, num_unique_ints
        print '(I0)', unique_ints(i)
    end do
    ! Example 2:
    digits = [2, 2, 8, 8, 2]
    call solve(digits, unique_ints, num_unique_ints)
    print '(A, I0)', 'Example 2: ', num_unique_ints
    do i = 1, num_unique_ints
        print '(I0)', unique_ints(i)
    end do
    ! Example 3:
    digits = [3, 7, 5]
    call solve(digits, unique_ints, num_unique_ints)
    print '(A, I0)', 'Example 3: ', num_unique_ints
    do i = 1, num_unique_ints
        print '(I0)', unique_ints(i)
    end do
contains
    subroutine solve(digits, unique_ints, num_unique_ints)
        implicit none
        integer, intent(in) :: digits(:)
        integer, intent(out) :: unique_ints(:)
        integer, intent(out) :: num_unique_ints
        integer :: i, j, k
        logical :: found
        num_unique_ints = 0
        do i = 1, size(digits) - 2
            do j = i + 1, size(digits) - 1
                do k = j + 1, size(digits)
                    if (digits(i) /= digits(j) .and. &
                        digits(j) /= digits(k) .and. &
                        digits(k) /= digits(i)) then
                        found = .false.
                        do while (.not. found)
                            if (digits(i)*100 + digits(j)*10 + digits(k) == 0) then
                                exit
                            else if (mod(digits(i)*100 + digits(j)*10 + digits(k), 2) == 0) then
                                unique_ints(num_unique_ints+1) = digits(i)*100 + digits(j)*10 + digits(k)
                                num_unique_ints = num_unique_ints + 1
                                found = .true.
                            else
                                i = i + 1
                            end if
                        end do
                    end if
                end do
            end do
        end do
    end subroutine solve
end program maintemp.f95:61:41:
   46 |         do i = 1, size(digits) - 2
      |                                  2       
......
   61 |                                 i = i + 1
      |                                         1
Error: Variable ‘i’ at (1) cannot be redefined inside loop beginning at (2)
temp.f95:12:4:
   12 |     digits = [2, 1, 3, 0]
      |    1
Error: Different shape for array assignment at (1) on dimension 1 (100 and 4)
temp.f95:20:4:
   20 |     digits = [2, 2, 8, 8, 2]
      |    1
Error: Different shape for array assignment at (1) on dimension 1 (100 and 5)
temp.f95:28:4:
   28 |     digits = [3, 7, 5]
      |    1
Error: Different shape for array assignment at (1) on dimension 1 (100 and 3)
          module solve_problem
  implicit none
contains
  subroutine solve(digits, result)
    integer, intent(in) :: digits(:)
    integer, intent(out) :: result(:)
    ! Local variables
    integer :: i, j, k, n, m, l, count
    integer :: temp(3)
    logical :: found
    ! Initialize variables
    n = size(digits)
    m = 0
    l = 0
    count = 0
    found = .false.
    ! Loop through all possible combinations of three digits
    do i = 1, n - 2
      do j = i + 1, n - 1
        do k = j + 1, n
          ! Check if the current combination is even
          if (mod(digits(i) * 100 + digits(j) * 10 + digits(k), 2) == 0) then
            ! Check if the current combination does not have leading zeros
            if (digits(i) * 100 + digits(j) * 10 + digits(k) /= 0) then
              ! Check if the current combination is unique
              found = .false.
              do l = 1, m
                if (temp(l) == digits(i) .and. temp(l + 1) == digits(j) .and. temp(l + 2) == digits(k)) then
                  found = .true.
                  exit
                end if
              end do
              if (.not. found) then
                ! Add the current combination to the result array
                count = count + 1
                temp(l + 1:l + 3) = [digits(i), digits(j), digits(k)]
                if (l == 0) then
                  allocate(result(count))
                else
                  allocate(result(1:count))
                end if
                result(count) = digits(i) * 100 + digits(j) * 10 + digits(k)
                m = m + 3
              end if
            end if
          end if
        end do
      end do
    end do
    ! Sort the result array
    call sort(result)
    ! Print the result
    do i = 1, count
      write (*, '(I0)') result(i)
    end do
  end subroutine solve
end module solve_problem
! Test the solve function with the given examples
program test_solve
  use solve_problem
  implicit none
  integer, parameter :: digits(3) = [2, 1, 3]
  integer, parameter :: digits2(3) = [2, 2, 8, 8, 2]
  integer, parameter :: digits3(3) = [3, 7, 5]
  integer :: result(100)
  call solve(digits, result)
  call solve(digits2, result)
  call solve(digits3, result)
end program test_solve
temp.f95:40:27:
   40 |                   allocate(result(count))
      |                           1
Error: Allocate-object at (1) must be ALLOCATABLE or a POINTER
temp.f95:42:27:
   42 |                   allocate(result(1:count))
      |                           1
Error: Allocate-object at (1) must be ALLOCATABLE or a POINTER
temp.f95:66:7:
   66 |   use solve_problem
      |       1
Fatal Error: Cannot open module file ‘solve_problem.mod’ for reading at (1): No such file or directory
compilation terminated.
          def min_stones_remaining(piles, k):
    for _ in range(k):
        max_pile_index = piles.index(max(piles))
        piles[max_pile_index] -= piles[max_pile_index] // 2
    return sum(piles)
The algorithm follows these steps:
k times.#include <algorithm>
int minStonesRemaining(int piles[], int n, int k) {
    for (int i = 0; i < k; i++) {
        int max_pile_index = std::distance(piles, std::max_element(piles, piles + n));
        piles[max_pile_index] = piles[max_pile_index] - (piles[max_pile_index] / 2);
    }
    int total_stones = 0;
    for (int i = 0; i < n; i++) {
        total_stones += piles[i];
    }
    return total_stones;
}
The algorithm follows these steps:
k times.