Given an array arr[] consisting of a permutation of first N natural numbers, the task is to find the maximum possible value of ΣGCD(arr[i], i) (1-based indexing) by rearranging the given array elements.
Examples:
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 3Input: arr[] = { 4, 5, 3, 2, 1 }
Output: 15
Naive Approach: The simplest approach to solve the problem is to traverse the array and generate all possible permutations of the given array and for each permutation, find the value of ΣGCD(arr[i], i). Finally, print the maximum value of ΣGCD(arr[i], i) from each permutation.
Below is the implementation of the above approach:
C++
// C++ program to impement // the above approach #include <bits/stdc++.h> 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; } |
6
Time Complexity: O(N!)
Auxiliary Space: O(1)
Efficient Approach: The above approach can be optimized based on the following observations:
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
Follow the steps below to solve the problem:
- Initialize a variable, say res, to store the maximum possible sum of ΣGCD(arr[i], i).
- Update res = (N * (N + 1) / 2).
- Finally, print the value of res.
Below is the implementation of the above approach:
C++
// C++ program to impement // the above approach #include <bits/stdc++.h> 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; } |
6
Time Complexity: O(1)
Auxiliary Space: O(1)
Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the DSA Self Paced Course at a student-friendly price and become industry ready.

