一次替换后可除所有数组元素的最大个数
给定一个数组arr ,用任何其他整数替换数组中的任何一个元素。任务是返回完全除此数组中所有元素的最大数。
例子:
Input: arr = [15, 9, 3]
Output: 3
Explanation: Here replace 15 with 3 so array becomes arr = [3, 9, 3] and now all elements in array are divisible by 3, so 3 is our answer
Input: arr = [16, 5, 10, 25]
Output: 5
方法:解决上述问题:
- 在整数 i 上从零迭代数组到小于它的长度
- 每次,从数组中删除第 i 个元素并找到最大公约数并将gcd存储在变量中
- 如果当前gcd大于maxGcd ,则更新maxGcd
下面是上述方法的实现:
C++14
// C++ Implementation for the above approach
#include
using namespace std;
// Function to return gcd of two numbers
int gcdOfTwoNos(int num1, int num2)
{
// If one of numbers is 0
// then gcd is other number
if (num1 == 0)
return num2;
if (num2 == 0)
return num1;
// If both are equal
// then that value is gcd
if (num1 == num2)
return num1;
// One is greater
if (num1 > num2)
return gcdOfTwoNos(num1 - num2, num2);
return gcdOfTwoNos(num1, num2 - num1);
}
// Function to return minimum sum
int Min_sum(int arr[], int N)
{
// Initialize min_sum with large value
int min_sum = 1000000, maxGcd = 1;
for (int i = 0; i < N; i++) {
// Initialize variable gcd
int gcd;
if (i == 0)
gcd = arr[1];
else {
gcd = arr[i - 1];
}
for (int j = 0; j < N; j++) {
if (j != i)
gcd = gcdOfTwoNos(gcd, arr[j]);
}
// Storing value of arr[i] in c
int c = arr[i];
// Update maxGcd if gcd is greater
// than maxGcd
if (gcd > maxGcd)
maxGcd = gcd;
}
// returning the maximum divisor
// of all elements
return maxGcd;
}
// Driver code
int main()
{
int arr[] = { 16, 5, 10, 25 };
int N = sizeof(arr) / sizeof(int);
cout << Min_sum(arr, N);
return 0;
}
Java
// Java program for the above approach
import java.io.*;
class GFG
{
// Function to return gcd of two numbers
static int gcdOfTwoNos(int num1, int num2)
{
// If one of numbers is 0
// then gcd is other number
if (num1 == 0)
return num2;
if (num2 == 0)
return num1;
// If both are equal
// then that value is gcd
if (num1 == num2)
return num1;
// One is greater
if (num1 > num2)
return gcdOfTwoNos(num1 - num2, num2);
return gcdOfTwoNos(num1, num2 - num1);
}
// Function to return minimum sum
static int Min_sum(int arr[], int N)
{
// Initialize min_sum with large value
int min_sum = 1000000, maxGcd = 1;
for (int i = 0; i < N; i++) {
// Initialize variable gcd
int gcd;
if (i == 0)
gcd = arr[1];
else {
gcd = arr[i - 1];
}
for (int j = 0; j < N; j++) {
if (j != i)
gcd = gcdOfTwoNos(gcd, arr[j]);
}
// Storing value of arr[i] in c
int c = arr[i];
// Update maxGcd if gcd is greater
// than maxGcd
if (gcd > maxGcd)
maxGcd = gcd;
}
// returning the maximum divisor
// of all elements
return maxGcd;
}
// Driver code
public static void main (String[] args) {
int arr[] = { 16, 5, 10, 25 };
int N = arr.length;
System.out.println( Min_sum(arr, N));
}
}
// This code is contributed by Potta Lokesh
Python3
# Python 3 Implementation for the above approach
# Function to return gcd1 of two numbers
def gcd1OfTwoNos(num1, num2):
# If one of numbers is 0
# then gcd1 is other number
if (num1 == 0):
return num2
if (num2 == 0):
return num1
# If both are equal
# then that value is gcd1
if (num1 == num2):
return num1
# One is greater
if (num1 > num2):
return gcd1OfTwoNos(num1 - num2, num2)
return gcd1OfTwoNos(num1, num2 - num1)
# Function to return minimum sum
def Min_sum(arr, N):
# Initialize min_sum with large value
min_sum = 1000000
maxgcd1 = 1
for i in range(N):
# Initialize variable gcd1
gcd1 = 1
if (i == 0):
gcd1 = arr[1]
else:
gcd1 = arr[i - 1]
for j in range(N):
if (j != i):
gcd1 = gcd1OfTwoNos(gcd1, arr[j])
# Storing value of arr[i] in c
c = arr[i]
# Update maxgcd1 if gcd1 is greater
# than maxgcd1
if (gcd1 > maxgcd1):
maxgcd1 = gcd1
# returning the maximum divisor
# of all elements
return maxgcd1
# Driver code
if __name__ == '__main__':
arr = [16, 5, 10, 25]
N = len(arr)
print(Min_sum(arr, N))
# This code is contributed by SURENDRA_GANGWAR.
C#
// C# program for the above approach
using System;
public class GFG
{
// Function to return gcd of two numbers
static int gcdOfTwoNos(int num1, int num2)
{
// If one of numbers is 0
// then gcd is other number
if (num1 == 0)
return num2;
if (num2 == 0)
return num1;
// If both are equal
// then that value is gcd
if (num1 == num2)
return num1;
// One is greater
if (num1 > num2)
return gcdOfTwoNos(num1 - num2, num2);
return gcdOfTwoNos(num1, num2 - num1);
}
// Function to return minimum sum
static int Min_sum(int []arr, int N)
{
// Initialize min_sum with large value
int min_sum = 1000000, maxGcd = 1;
for (int i = 0; i < N; i++) {
// Initialize variable gcd
int gcd;
if (i == 0)
gcd = arr[1];
else {
gcd = arr[i - 1];
}
for (int j = 0; j < N; j++) {
if (j != i)
gcd = gcdOfTwoNos(gcd, arr[j]);
}
// Update maxGcd if gcd is greater
// than maxGcd
if (gcd > maxGcd)
maxGcd = gcd;
}
// returning the maximum divisor
// of all elements
return maxGcd;
}
// Driver code
public static void Main (string[] args) {
int []arr = { 16, 5, 10, 25 };
int N = arr.Length;
Console.WriteLine(Min_sum(arr, N));
}
}
// This code is contributed by AnkThon
Javascript
输出:
5
时间复杂度: O((N^2) * Log(M)),其中 M 是数组中的最小值
辅助空间: O(1)