![]() |
VOOZH | about |
Given an array arr of size N. Consider a graph formed using the given array where the graph has N vertices and the value of i'th vertices is arr[i] there is an edge between i'th and j'th vertice if gcd(arr[i], arr[j])>1. Your task is to print the size of the largest connected component formed with the above graph.
Examples:
Input: N=6, arr[]={10, 4, 2, 5, 3, 9}
Output: 4
Explanation: there are two components of 4, 2, largest is 4.Input: N=10, arr[]={2, 10, 10, 1, 13, 8, 20, 2, 2, 16}
Output: 8
Explanation: there are eight components and the largest is 8.
Below is the C++ implementation of the above approach:
4
Time Complexity: O(N*sqrt(Arr[i])).
Auxiliary Space: O(N + max(Arr[i])).