给定一个由前N个自然数组成的数组arr [] ,任务是通过重新排列给定的数组元素来找到ΣGCD(arr [i],i) (基于1的索引)的最大可能值。
例子:
Input: arr[] = { 2, 1}
Output: 6
Explanation:
Rearranging the given array to { 2, 1}.
ΣGCD(arr[i], i) = GCD(arr[1], 1) + GCD(arr[2], 2) = GCD(2, 1) + GCD(1, 2)= 2
Rearranging the given array to { 1, 2 }.
ΣGCD(arr[i], i) = GCD(arr[1], 1) + GCD(arr[2], 2) = GCD(1, 1) + GCD(2, 2) = 3
Therefore, the required output is 3
Input: arr[] = { 4, 5, 3, 2, 1 }
Output: 15
天真的方法:解决问题的最简单方法是遍历数组并生成给定数组的所有可能排列,对于每个排列,找到ΣGCD(arr [i],i)的值。最后,从每个排列中打印ΣGCD(arr [i],i)的最大值。
下面是上述方法的实现:
C++
// C++ program to impement
// the above approach
#include
using namespace std;
// Function to find the maximum sum of
// GCD(arr[i], i) by rearranging the array
int findMaxValByRearrArr(int arr[], int N)
{
// Sort the array in
// ascending order
sort(arr, arr + N);
// Stores maximum sum of GCD(arr[i], i)
// by rearranging the array elements
int res = 0;
// Generate all possible
// permutations of the array
do {
// Stores sum of GCD(arr[i], i)
int sum = 0;
// Traverse the array
for (int i = 0; i < N; i++) {
// Update sum
sum += __gcd(i + 1, arr[i]);
}
// Update res
res = max(res, sum);
} while (next_permutation(arr, arr + N));
return res;
}
// Driver Code
int main()
{
int arr[] = { 3, 2, 1 };
int N = sizeof(arr) / sizeof(arr[0]);
cout << findMaxValByRearrArr(arr, N);
return 0;
}
Java
// Java program to impement
// the above approach
import java.util.*;
class GFG{
// Function to find the maximum sum of
// GCD(arr[i], i) by rearranging the array
static int findMaxValByRearrArr(int arr[], int N)
{
// Sort the array in
// ascending order
Arrays.sort(arr);
// Stores maximum sum of GCD(arr[i], i)
// by rearranging the array elements
int res = 0;
// Generate all possible
// permutations of the array
do {
// Stores sum of GCD(arr[i], i)
int sum = 0;
// Traverse the array
for (int i = 0; i < N; i++) {
// Update sum
sum += __gcd(i + 1, arr[i]);
}
// Update res
res = Math.max(res, sum);
} while (next_permutation(arr));
return res;
}
static int __gcd(int a, int b)
{
return b == 0? a:__gcd(b, a % b);
}
static boolean next_permutation(int[] p)
{
for (int a = p.length - 2; a >= 0; --a)
if (p[a] < p[a + 1])
for (int b = p.length - 1;; --b)
if (p[b] > p[a])
{
int t = p[a];
p[a] = p[b];
p[b] = t;
for (++a, b = p.length - 1; a < b; ++a, --b)
{
t = p[a];
p[a] = p[b];
p[b] = t;
}
return true;
}
return false;
}
// Driver Code
public static void main(String[] args)
{
int arr[] = { 3, 2, 1 };
int N = arr.length;
System.out.print(findMaxValByRearrArr(arr, N));
}
}
// This code is contributed by 29AjayKumar
Python3
# Python3 program to implement
# the above approach
# Function to find the maximum sum of
# GCD(arr[i], i) by rearranging the array
def findMaxValByRearrArr(arr, N):
# Sort the array in
# ascending order
arr.sort()
# Stores maximum sum of GCD(arr[i], i)
# by rearranging the array elements
res = 0
# Generate all possible
# permutations of the array
while (True):
# Stores sum of GCD(arr[i], i)
Sum = 0
# Traverse the array
for i in range(N):
# Update sum
Sum += __gcd(i + 1, arr[i])
# Update res
res = max(res, Sum)
if (not next_permutation(arr)):
break
return res
def __gcd(a, b):
if b == 0:
return a
else:
return __gcd(b, a % b)
def next_permutation(p):
for a in range(len(p) - 2, -1, -1):
if (p[a] < p[a + 1]):
b = len(p) - 1
while True:
if (p[b] > p[a]):
t = p[a]
p[a] = p[b]
p[b] = t
a += 1
b = len(p) - 1
while a < b:
t = p[a]
p[a] = p[b]
p[b] = t
a += 1
b -= 1
return True
b -= 1
return False
# Driver code
arr = [ 3, 2, 1 ]
N = len(arr)
print(findMaxValByRearrArr(arr, N))
# This code is contributed by divyesh072019
C#
// C# program to impement
// the above approach
using System;
class GFG
{
// Function to find the maximum sum of
// GCD(arr[i], i) by rearranging the array
static int findMaxValByRearrArr(int []arr, int N)
{
// Sort the array in
// ascending order
Array.Sort(arr);
// Stores maximum sum of GCD(arr[i], i)
// by rearranging the array elements
int res = 0;
// Generate all possible
// permutations of the array
do
{
// Stores sum of GCD(arr[i], i)
int sum = 0;
// Traverse the array
for (int i = 0; i < N; i++)
{
// Update sum
sum += __gcd(i + 1, arr[i]);
}
// Update res
res = Math.Max(res, sum);
} while (next_permutation(arr));
return res;
}
static int __gcd(int a, int b)
{
return b == 0? a:__gcd(b, a % b);
}
static bool next_permutation(int[] p)
{
for (int a = p.Length - 2; a >= 0; --a)
if (p[a] < p[a + 1])
for (int b = p.Length - 1;; --b)
if (p[b] > p[a])
{
int t = p[a];
p[a] = p[b];
p[b] = t;
for (++a, b = p.Length - 1; a < b; ++a, --b)
{
t = p[a];
p[a] = p[b];
p[b] = t;
}
return true;
}
return false;
}
// Driver Code
public static void Main(String[] args)
{
int []arr = { 3, 2, 1 };
int N = arr.Length;
Console.Write(findMaxValByRearrArr(arr, N));
}
}
// This code is contributed by 29AjayKumar
C++
// C++ program to impement
// the above approach
#include
using namespace std;
// Function to find the maximum sum of
// GCD(arr[i], i) by rearranging the array
int findMaxValByRearrArr(int arr[], int N)
{
// Stores maximum sum of GCD(arr[i], i)
// by rearranging the array elements
int res = 0;
// Update res
res = (N * (N + 1)) / 2;
return res;
}
// Driver Code
int main()
{
int arr[] = { 3, 2, 1 };
int N = sizeof(arr) / sizeof(arr[0]);
cout << findMaxValByRearrArr(arr, N);
return 0;
}
Java
// Java program to impement
// the above approach
import java.util.*;
class GFG
{
// Function to find the maximum sum of
// GCD(arr[i], i) by rearranging the array
static int findMaxValByRearrArr(int arr[], int N)
{
// Stores maximum sum of GCD(arr[i], i)
// by rearranging the array elements
int res = 0;
// Update res
res = (N * (N + 1)) / 2;
return res;
}
// Driver Code
public static void main(String[] args)
{
int arr[] = { 3, 2, 1 };
int N = arr.length;
System.out.print(findMaxValByRearrArr(arr, N));
}
}
// This code is contributed by shikhasingrajput
Python3
# Python program to impement
# the above approach
# Function to find the maximum sum of
# GCD(arr[i], i) by rearranging the array
def findMaxValByRearrArr(arr, N):
# Stores maximum sum of GCD(arr[i], i)
# by rearranging the array elements
res = 0;
# Update res
res = (N * (N + 1)) // 2;
return res;
# Driver Code
if __name__ == '__main__':
arr = [ 3, 2, 1 ];
N = len(arr);
print(findMaxValByRearrArr(arr, N));
# This code contributed by shikhasingrajput
C#
// C# program to impement
// the above approach
using System;
class GFG{
// Function to find the maximum sum of
// GCD(arr[i], i) by rearranging the array
static int findMaxValByRearrArr(int []arr, int N)
{
// Stores maximum sum of GCD(arr[i], i)
// by rearranging the array elements
int res = 0;
// Update res
res = (N * (N + 1)) / 2;
return res;
}
// Driver Code
public static void Main(String[] args)
{
int []arr = { 3, 2, 1 };
int N = arr.Length;
Console.Write(findMaxValByRearrArr(arr, N));
}
}
// This code is contributed by shikhasingrajput
Javascript
6
时间复杂度: O(N!)
辅助空间: O(1)
高效的方法:可以基于以下观察来优化上述方法:
Maximum possible value of GCD(X, Y) = min(X, Y)
Therefore, the maximum possible value of GCD(i, arr[i]) = min(i, arr[i])
If array is sorted then i = arr[i] and the value of GCD(i, arr[i]) = i
ΣGCD(arr[i], i) = Σi = N * (N + 1) / 2
请按照以下步骤解决问题:
- 初始化一个变量,例如res ,以存储ΣGCD(arr [i],i)的最大可能和。
- 更新res =(N *(N +1)/ 2) 。
- 最后,打印res的值。
下面是上述方法的实现:
C++
// C++ program to impement
// the above approach
#include
using namespace std;
// Function to find the maximum sum of
// GCD(arr[i], i) by rearranging the array
int findMaxValByRearrArr(int arr[], int N)
{
// Stores maximum sum of GCD(arr[i], i)
// by rearranging the array elements
int res = 0;
// Update res
res = (N * (N + 1)) / 2;
return res;
}
// Driver Code
int main()
{
int arr[] = { 3, 2, 1 };
int N = sizeof(arr) / sizeof(arr[0]);
cout << findMaxValByRearrArr(arr, N);
return 0;
}
Java
// Java program to impement
// the above approach
import java.util.*;
class GFG
{
// Function to find the maximum sum of
// GCD(arr[i], i) by rearranging the array
static int findMaxValByRearrArr(int arr[], int N)
{
// Stores maximum sum of GCD(arr[i], i)
// by rearranging the array elements
int res = 0;
// Update res
res = (N * (N + 1)) / 2;
return res;
}
// Driver Code
public static void main(String[] args)
{
int arr[] = { 3, 2, 1 };
int N = arr.length;
System.out.print(findMaxValByRearrArr(arr, N));
}
}
// This code is contributed by shikhasingrajput
Python3
# Python program to impement
# the above approach
# Function to find the maximum sum of
# GCD(arr[i], i) by rearranging the array
def findMaxValByRearrArr(arr, N):
# Stores maximum sum of GCD(arr[i], i)
# by rearranging the array elements
res = 0;
# Update res
res = (N * (N + 1)) // 2;
return res;
# Driver Code
if __name__ == '__main__':
arr = [ 3, 2, 1 ];
N = len(arr);
print(findMaxValByRearrArr(arr, N));
# This code contributed by shikhasingrajput
C#
// C# program to impement
// the above approach
using System;
class GFG{
// Function to find the maximum sum of
// GCD(arr[i], i) by rearranging the array
static int findMaxValByRearrArr(int []arr, int N)
{
// Stores maximum sum of GCD(arr[i], i)
// by rearranging the array elements
int res = 0;
// Update res
res = (N * (N + 1)) / 2;
return res;
}
// Driver Code
public static void Main(String[] args)
{
int []arr = { 3, 2, 1 };
int N = arr.Length;
Console.Write(findMaxValByRearrArr(arr, N));
}
}
// This code is contributed by shikhasingrajput
Java脚本
6
时间复杂度: O(1)
辅助空间: O(1)