The Wayback Machine - https://web.archive.org/web/20210104102737/https://www.geeksforgeeks.org/rearrange-array-to-maximize-sum-of-gcd-of-array-elements-with-their-respective-indices/
Related Articles

Related Articles

Rearrange array to maximize sum of GCD of array elements with their respective indices
  • Last Updated : 04 Jan, 2021

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 3

Input: 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++

filter_none

edit
close

play_arrow

link
brightness_4
code

// 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;
}

chevron_right


Output:

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++

filter_none

edit
close

play_arrow

link
brightness_4
code

// 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;
}

chevron_right


Output:

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.




My Personal Notes arrow_drop_up
Recommended Articles
Page :