The array-form of an integer num
is an array representing its digits in left to right order.
num = 1321
, the array form is [1,3,2,1]
.Given num
, the array-form of an integer, and an integer k
, return the array-form of the integer num + k
.
Example 1:
Input: num = [1,2,0,0], k = 34 Output: [1,2,3,4] Explanation: 1200 + 34 = 1234
Example 2:
Input: num = [2,7,4], k = 181 Output: [4,5,5] Explanation: 274 + 181 = 455
Example 3:
Input: num = [2,1,5], k = 806 Output: [1,0,2,1] Explanation: 215 + 806 = 1021
Constraints:
1 <= num.length <= 104
0 <= num[i] <= 9
num
does not contain any leading zeros except for the zero itself.1 <= k <= 104
program main
implicit none
integer :: num(4), k
integer :: i
! Example 1
num = [1, 2, 0, 0]
k = 34
call print_array_form(num)
write (*,*) " + ", k, " = "
call add_to_array_form(num, k)
call print_array_form(num)
write (*,*) ""
! Example 2
num = [2, 7, 4]
k = 181
call print_array_form(num)
write (*,*) " + ", k, " = "
call add_to_array_form(num, k)
call print_array_form(num)
write (*,*) ""
! Example 3
num = [2, 1, 5]
k = 806
call print_array_form(num)
write (*,*) " + ", k, " = "
call add_to_array_form(num, k)
call print_array_form(num)
write (*,*) ""
contains
subroutine print_array_form(num)
implicit none
integer, intent(in) :: num(:)
integer :: i
do i = size(num), 1, -1
write (*,*) num(i), " "
end do
end subroutine print_array_form
subroutine add_to_array_form(num, k)
implicit none
integer, intent(inout) :: num(:)
integer, value :: k
integer :: carry
carry = 0
do i = 1, size(num)
num(i) = num(i) + carry
carry = num(i) / 10
num(i) = mod(num(i), 10)
end do
if (carry > 0) then
allocate(num(size(num)+1))
num(1:size(num)-1) = 0
num(size(num)) = carry
end if
end subroutine add_to_array_form
end program main
temp.f95:59:21: 59 | allocate(num(size(num)+1)) | 1 Error: Allocate-object at (1) must be ALLOCATABLE or a POINTER temp.f95:17:4: 17 | num = [2, 7, 4] | 1 Error: Different shape for array assignment at (1) on dimension 1 (4 and 3) temp.f95:26:4: 26 | num = [2, 1, 5] | 1 Error: Different shape for array assignment at (1) on dimension 1 (4 and 3)
module AddToArrayForm
implicit none
private
public :: addToArrayForm
contains
function addToArrayForm(num, k) result(result)
integer, intent(in) :: num(:)
integer, intent(in) :: k
integer :: result(size(num))
integer :: carry, i
carry = k
do i = 1, size(num)
result(i) = (num(i) + carry) mod 10
carry = (num(i) + carry) / 10
end do
if (carry > 0) then
allocate(result(size(num) + 1))
result(1:size(num)) = num
result(size(num) + 1) = carry
end if
end function addToArrayForm
end module AddToArrayForm
program test_addToArrayForm
use AddToArrayForm
implicit none
integer, parameter :: num = [1, 3, 2, 1]
integer, parameter :: k = 34
integer :: result(size(num))
result = addToArrayForm(num, k)
write (*,*) "Result: ", result
end program test_addToArrayForm
temp.f95:4:28: 4 | public :: addToArrayForm | 1 Error: PUBLIC attribute applied to MODULE addtoarrayform at (1) temp.f95:6:27: 6 | function addToArrayForm(num, k) result(result) | 1 Error: MODULE attribute of ‘addtoarrayform’ conflicts with PROCEDURE attribute at (1) temp.f95:7:37: 7 | integer, intent(in) :: num(:) | 1 Error: Unexpected data declaration statement in CONTAINS section at (1) temp.f95:8:32: 8 | integer, intent(in) :: k | 1 Error: Unexpected data declaration statement in CONTAINS section at (1) temp.f95:9:31: 9 | integer :: result(size(num)) | 1 Error: Symbol ‘num’ is used before it is typed at (1) temp.f95:11:27: 11 | integer :: carry, i | 1 Error: Unexpected data declaration statement in CONTAINS section at (1) temp.f95:13:17: 13 | carry = k | 1 Error: Unexpected assignment statement in CONTAINS section at (1) temp.f95:14:27: 14 | do i = 1, size(num) | 1 Error: Unexpected DO statement in CONTAINS section at (1) temp.f95:16:41: 16 | carry = (num(i) + carry) / 10 | 1 Error: Unexpected assignment statement in CONTAINS section at (1) temp.f95:17:11: 17 | end do | 1 Error: Expecting END MODULE statement at (1) temp.f95:19:27: 19 | if (carry > 0) then | 1 Error: Unexpected block IF statement in CONTAINS section at (1) temp.f95:20:21: 20 | allocate(result(size(num) + 1)) | 1 Error: Allocate-object at (1) is neither a data pointer nor an allocatable variable temp.f95:22:41: 22 | result(size(num) + 1) = carry | 1 Error: Unexpected assignment statement in CONTAINS section at (1) temp.f95:23:11: 23 | end if | 1 Error: Expecting END MODULE statement at (1) temp.f95:24:7: 24 | end function addToArrayForm | 1 Error: Expecting END MODULE statement at (1) temp.f95:28:9: 28 | use AddToArrayForm | 1 Fatal Error: Cannot open module file ‘addtoarrayform.mod’ for reading at (1): No such file or directory compilation terminated.
from collections import defaultdict
def largestComponentSize(nums):
def primes(n):
for i in range(2, int(n**0.5) + 1):
if n % i == 0:
return i
return n
def dfs(node, visited, graph):
if node in visited:
return 0
visited.add(node)
size = 1
for neighbor in graph[node]:
size += dfs(neighbor, visited, graph)
return size
graph = defaultdict(set)
visited = set()
for num in nums:
prime = primes(num)
graph[prime].add(num)
if num != prime:
graph[num].add(prime)
count = 0
for num in nums:
count = max(count, dfs(num, visited, graph))
return count
The algorithm uses the following steps:
nums
, find the smallest prime divisor, which is the prime number that divides the current number.nums
, counting the size of connected components and keeping track of visited nodes.This algorithm starts by mapping each number to its smallest prime divisor, and then building a graph to model the relationships between the numbers and the divisors. The DFS traversal helps to find connected components, and the largest connected component is returned. The time complexity of this algorithm is O(N√W), where N is the size of the nums array, and W is the maximum number in it.
#include<vector>
#include<unordered_set>
#include<unordered_map>
using namespace std;
int primes(int n) {
for (int i = 2; i * i <= n; ++i) {
if (n % i == 0) return i;
}
return n;
}
int largestComponentSize(vector<int>& nums) {
int count = 0;
unordered_map<int, unordered_set<int>> graph;
unordered_set<int> visited;
for (int num : nums) {
int prime = primes(num);
graph[prime].insert(num);
if (num != prime) graph[num].insert(prime);
}
function<int(int)> dfs = [&](int node) {
if (!visited.insert(node).second) return 0;
int res = 1;
for (int neighbor : graph[node]) {
res += dfs(neighbor);
}
return res;
};
for (int num : nums) {
count = max(count, dfs(num));
}
return count;
}
The algorithm uses the following steps:
nums
, find the smallest prime divisor, which is the prime number that divides the current number.nums
, counting the size of connected components and keeping track of visited nodes.This algorithm starts by mapping each number to its smallest prime divisor, and then building a graph to model the relationships between the numbers and the divisors. The DFS traversal helps to find connected components, and the largest connected component is returned. The time complexity of this algorithm is O(N√W), where N is the size of the nums array, and W is the maximum number in it.