给定数组arr [] ,任务是找到最小的整数(除1以外),该整数对给定数组的每个元素进行除法。
例子:
Input: arr[] = { 2, 4, 8 }
Output: 2
2 is the smallest possible number which divides the whole array.
Input: arr[] = { 4, 7, 5 }
Output: -1
There’s no integer possible which divides the whole array other than 1.
方法:我们知道整个数组的GCD将是将数组的每个元素相除的最大整数。如果GCD = 1,则不可能将整个数组相除。但是,如果GCD> 1,则存在一个整数,该整数将数组完全分割。例如,
If GCD = 36 then
36 divides the whole array.
18 divides the whole array.
12 divides the whole array.
9 divides the whole array.
…
1 divides the whole array.
Thus, we see that all factors of 36 also divide the array. The smallest prime factor of 36 i.e. 2 is the smallest possible integer which divides the whole array. Hence, we need to find the smallest prime factor of the GCD as the required answer.
下面是上述方法的实现:
C++
// C++ implementation of the approach
#include
using namespace std;
// Function to find the smallest divisor
int smallestDivisor(int x)
{
// if divisible by 2
if (x % 2 == 0)
return 2;
// iterate from 3 to sqrt(n)
for (int i = 3; i * i <= x; i += 2) {
if (x % i == 0)
return i;
}
return x;
}
// Function to return smallest possible integer
// which divides the whole array
int smallestInteger(int* arr, int n)
{
// To store the GCD of all the array elements
int gcd = 0;
for (int i = 0; i < n; i++)
gcd = __gcd(gcd, arr[i]);
// Return the smallest prime factor
// of the gcd calculated
return smallestDivisor(gcd);
}
// Driver code
int main()
{
int arr[] = { 2, 4, 8 };
int n = sizeof(arr) / sizeof(arr[0]);
cout << smallestInteger(arr, n);
return 0;
}
Java
// Java implementation of the approach
class GFG
{
static int __gcd(int a, int b)
{
if (b == 0)
return a;
return __gcd(b, a % b);
}
// Function to find the smallest divisor
static int smallestDivisor(int x)
{
// if divisible by 2
if (x % 2 == 0)
return 2;
// iterate from 3 to sqrt(n)
for (int i = 3; i * i <= x; i += 2)
{
if (x % i == 0)
return i;
}
return x;
}
// Function to return smallest possible integer
// which divides the whole array
static int smallestInteger(int []arr, int n)
{
// To store the GCD of all the array elements
int gcd = 0;
for (int i = 0; i < n; i++)
gcd = __gcd(gcd, arr[i]);
// Return the smallest prime factor
// of the gcd calculated
return smallestDivisor(gcd);
}
// Driver code
public static void main(String[] args)
{
int []arr = { 2, 4, 8 };
int n = arr.length;
System.out.println(smallestInteger(arr, n));
}
}
// This code is contributed by Code_Mech.
Python3
# Python3 implementation of the approach
from math import sqrt, gcd
# Function to find the smallest divisor
def smallestDivisor(x) :
# if divisible by 2
if (x % 2 == 0) :
return 2;
# iterate from 3 to sqrt(n)
for i in range(3, int(sqrt(x)) + 1, 2) :
if (x % i == 0) :
return i;
return x
# Function to return smallest possible
# integer which divides the whole array
def smallestInteger(arr, n) :
# To store the GCD of all the
# array elements
__gcd = 0;
for i in range(n) :
__gcd = gcd(__gcd, arr[i]);
# Return the smallest prime factor
# of the gcd calculated
return smallestDivisor(__gcd);
# Driver code
if __name__ == "__main__" :
arr = [ 2, 4, 8 ];
n = len(arr);
print(smallestInteger(arr, n));
# This code is contributed by Ryuga
C#
// C# implementation of the approach
using System;
class GFG
{
static int __gcd(int a, int b)
{
if (b == 0)
return a;
return __gcd(b, a % b);
}
// Function to find the smallest divisor
static int smallestDivisor(int x)
{
// if divisible by 2
if (x % 2 == 0)
return 2;
// iterate from 3 to sqrt(n)
for (int i = 3; i * i <= x; i += 2)
{
if (x % i == 0)
return i;
}
return x;
}
// Function to return smallest possible integer
// which divides the whole array
static int smallestInteger(int []arr, int n)
{
// To store the GCD of all the array elements
int gcd = 0;
for (int i = 0; i < n; i++)
gcd = __gcd(gcd, arr[i]);
// Return the smallest prime factor
// of the gcd calculated
return smallestDivisor(gcd);
}
// Driver code
static void Main()
{
int []arr = { 2, 4, 8 };
int n = arr.Length;
Console.WriteLine(smallestInteger(arr, n));
}
}
// This code is contributed by mits
PHP
C++
// C++ implementation of the approach
#include
using namespace std;
const int MAX = 100005;
// To store the smallest prime factor
int spf[MAX];
// Function to store spf of integers
void sieve()
{
memset(spf, 0, sizeof(spf));
spf[0] = 1;
// When gcd is 1 then the answer is -1
spf[1] = -1;
for (int i = 2; i * i < MAX; i++) {
if (spf[i] == 0) {
for (int j = i * 2; j < MAX; j += i) {
if (spf[j] == 0) {
spf[j] = i;
}
}
}
}
for (int i = 2; i < MAX; i++) {
if (!spf[i])
spf[i] = i;
}
}
// Function to return smallest possible integer
// which divides the whole array
int smallestInteger(int* arr, int n)
{
// To store the GCD of all the array elements
int gcd = 0;
for (int i = 0; i < n; i++)
gcd = __gcd(gcd, arr[i]);
// Return the smallest prime factor
// of the gcd calculated
return spf[gcd];
}
// Driver code
int main()
{
sieve();
int arr[] = { 2, 4, 8 };
int n = sizeof(arr) / sizeof(arr[0]);
cout << smallestInteger(arr, n);
return 0;
}
Java
// Java implementation of the approach
class GFG
{
static int MAX = 100005;
// To store the smallest prime factor
static int spf[] = new int[MAX];
// Function to store spf of integers
static void sieve()
{
spf[0] = 1;
// When gcd is 1 then the answer is -1
spf[1] = -1;
for (int i = 2; i * i < MAX; i++)
{
if (spf[i] == 0)
{
for (int j = i * 2; j < MAX; j += i)
{
if (spf[j] == 0)
{
spf[j] = i;
}
}
}
}
for (int i = 2; i < MAX; i++)
{
if (spf[i] != 1)
spf[i] = i;
}
}
// Function to return smallest possible integer
// which divides the whole array
static int smallestInteger(int[] arr, int n)
{
// To store the GCD of all the array elements
int gcd = 0;
for (int i = 0; i < n; i++)
gcd = __gcd(gcd, arr[i]);
// Return the smallest prime factor
// of the gcd calculated
return spf[gcd];
}
static int __gcd(int a, int b)
{
if (b == 0)
return a;
return __gcd(b, a % b);
}
// Driver code
public static void main(String[] args)
{
sieve();
int arr[] = { 2, 4, 8 };
int n = arr.length;
System.out.println(smallestInteger(arr, n));
}
}
/* This code contributed by PrinciRaj1992 */
Python3
# Python3 implementation of the approach
MAX = 10005;
# To store the smallest prime factor
spf = [0] * MAX;
# Function to store spf of integers
def sieve():
spf[0] = 1;
# When gcd is 1 then the answer is -1
spf[1] = -1;
i = 2;
while (i * i < MAX):
if (spf[i] == 0):
for j in range(i * 2, MAX, i):
if (spf[j] == 0):
spf[j] = i;
i += 1;
for i in range(2, MAX):
if (spf[i] == 0):
spf[i] = i;
# find gcd of two no
def __gcd(a, b):
if (b == 0):
return a;
return __gcd(b, a % b);
# Function to return smallest possible integer
# which divides the whole array
def smallestInteger(arr, n):
# To store the GCD of all the array elements
gcd = 0;
for i in range(n):
gcd = __gcd(gcd, arr[i]);
# Return the smallest prime factor
# of the gcd calculated
return spf[gcd];
# Driver code
sieve();
arr = [ 2, 4, 8 ];
n = len(arr);
print(smallestInteger(arr, n));
# This code is contributed by mits
C#
// C# implemenatation of above approach
using System;
class GFG
{
static int MAX = 100005;
// To store the smallest prime factor
static int []spf = new int[MAX];
// Function to store spf of integers
static void sieve()
{
spf[0] = 1;
// When gcd is 1 then the answer is -1
spf[1] = -1;
for (int i = 2; i * i < MAX; i++)
{
if (spf[i] == 0)
{
for (int j = i * 2; j < MAX; j += i)
{
if (spf[j] == 0)
{
spf[j] = i;
}
}
}
}
for (int i = 2; i < MAX; i++)
{
if (spf[i] != 1)
spf[i] = i;
}
}
// Function to return smallest possible integer
// which divides the whole array
static int smallestInteger(int[] arr, int n)
{
// To store the GCD of all the array elements
int gcd = 0;
for (int i = 0; i < n; i++)
gcd = __gcd(gcd, arr[i]);
// Return the smallest prime factor
// of the gcd calculated
return spf[gcd];
}
static int __gcd(int a, int b)
{
if (b == 0)
return a;
return __gcd(b, a % b);
}
// Driver code
public static void Main(String[] args)
{
sieve();
int []arr = { 2, 4, 8 };
int n = arr.Length;
Console.WriteLine(smallestInteger(arr, n));
}
}
// This code has been contributed by 29AjayKumar
PHP
2
对于多个查询,我们可以使用筛子预先计算出最小的质数因子,直到达到最大值。
C++
// C++ implementation of the approach
#include
using namespace std;
const int MAX = 100005;
// To store the smallest prime factor
int spf[MAX];
// Function to store spf of integers
void sieve()
{
memset(spf, 0, sizeof(spf));
spf[0] = 1;
// When gcd is 1 then the answer is -1
spf[1] = -1;
for (int i = 2; i * i < MAX; i++) {
if (spf[i] == 0) {
for (int j = i * 2; j < MAX; j += i) {
if (spf[j] == 0) {
spf[j] = i;
}
}
}
}
for (int i = 2; i < MAX; i++) {
if (!spf[i])
spf[i] = i;
}
}
// Function to return smallest possible integer
// which divides the whole array
int smallestInteger(int* arr, int n)
{
// To store the GCD of all the array elements
int gcd = 0;
for (int i = 0; i < n; i++)
gcd = __gcd(gcd, arr[i]);
// Return the smallest prime factor
// of the gcd calculated
return spf[gcd];
}
// Driver code
int main()
{
sieve();
int arr[] = { 2, 4, 8 };
int n = sizeof(arr) / sizeof(arr[0]);
cout << smallestInteger(arr, n);
return 0;
}
Java
// Java implementation of the approach
class GFG
{
static int MAX = 100005;
// To store the smallest prime factor
static int spf[] = new int[MAX];
// Function to store spf of integers
static void sieve()
{
spf[0] = 1;
// When gcd is 1 then the answer is -1
spf[1] = -1;
for (int i = 2; i * i < MAX; i++)
{
if (spf[i] == 0)
{
for (int j = i * 2; j < MAX; j += i)
{
if (spf[j] == 0)
{
spf[j] = i;
}
}
}
}
for (int i = 2; i < MAX; i++)
{
if (spf[i] != 1)
spf[i] = i;
}
}
// Function to return smallest possible integer
// which divides the whole array
static int smallestInteger(int[] arr, int n)
{
// To store the GCD of all the array elements
int gcd = 0;
for (int i = 0; i < n; i++)
gcd = __gcd(gcd, arr[i]);
// Return the smallest prime factor
// of the gcd calculated
return spf[gcd];
}
static int __gcd(int a, int b)
{
if (b == 0)
return a;
return __gcd(b, a % b);
}
// Driver code
public static void main(String[] args)
{
sieve();
int arr[] = { 2, 4, 8 };
int n = arr.length;
System.out.println(smallestInteger(arr, n));
}
}
/* This code contributed by PrinciRaj1992 */
Python3
# Python3 implementation of the approach
MAX = 10005;
# To store the smallest prime factor
spf = [0] * MAX;
# Function to store spf of integers
def sieve():
spf[0] = 1;
# When gcd is 1 then the answer is -1
spf[1] = -1;
i = 2;
while (i * i < MAX):
if (spf[i] == 0):
for j in range(i * 2, MAX, i):
if (spf[j] == 0):
spf[j] = i;
i += 1;
for i in range(2, MAX):
if (spf[i] == 0):
spf[i] = i;
# find gcd of two no
def __gcd(a, b):
if (b == 0):
return a;
return __gcd(b, a % b);
# Function to return smallest possible integer
# which divides the whole array
def smallestInteger(arr, n):
# To store the GCD of all the array elements
gcd = 0;
for i in range(n):
gcd = __gcd(gcd, arr[i]);
# Return the smallest prime factor
# of the gcd calculated
return spf[gcd];
# Driver code
sieve();
arr = [ 2, 4, 8 ];
n = len(arr);
print(smallestInteger(arr, n));
# This code is contributed by mits
C#
// C# implemenatation of above approach
using System;
class GFG
{
static int MAX = 100005;
// To store the smallest prime factor
static int []spf = new int[MAX];
// Function to store spf of integers
static void sieve()
{
spf[0] = 1;
// When gcd is 1 then the answer is -1
spf[1] = -1;
for (int i = 2; i * i < MAX; i++)
{
if (spf[i] == 0)
{
for (int j = i * 2; j < MAX; j += i)
{
if (spf[j] == 0)
{
spf[j] = i;
}
}
}
}
for (int i = 2; i < MAX; i++)
{
if (spf[i] != 1)
spf[i] = i;
}
}
// Function to return smallest possible integer
// which divides the whole array
static int smallestInteger(int[] arr, int n)
{
// To store the GCD of all the array elements
int gcd = 0;
for (int i = 0; i < n; i++)
gcd = __gcd(gcd, arr[i]);
// Return the smallest prime factor
// of the gcd calculated
return spf[gcd];
}
static int __gcd(int a, int b)
{
if (b == 0)
return a;
return __gcd(b, a % b);
}
// Driver code
public static void Main(String[] args)
{
sieve();
int []arr = { 2, 4, 8 };
int n = arr.Length;
Console.WriteLine(smallestInteger(arr, n));
}
}
// This code has been contributed by 29AjayKumar
的PHP
2
如果您希望与行业专家一起参加现场课程,请参阅《 Geeks现场课程》和《 Geeks现场课程美国》。