Rearrange array to maximize sum of GCD of array elements with their respective indices
Last Updated :
25 Jun, 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++
#include <bits/stdc++.h>
using namespace std;
int findMaxValByRearrArr( int arr[], int N)
{
sort(arr, arr + N);
int res = 0;
do {
int sum = 0;
for ( int i = 0; i < N; i++) {
sum += __gcd(i + 1, arr[i]);
}
res = max(res, sum);
} while (next_permutation(arr, arr + N));
return res;
}
int main()
{
int arr[] = { 3, 2, 1 };
int N = sizeof (arr) / sizeof (arr[0]);
cout << findMaxValByRearrArr(arr, N);
return 0;
}
|
Java
import java.util.*;
class GFG{
static int findMaxValByRearrArr( int arr[], int N)
{
Arrays.sort(arr);
int res = 0 ;
do {
int sum = 0 ;
for ( int i = 0 ; i < N; i++) {
sum += __gcd(i + 1 , arr[i]);
}
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 ;
}
public static void main(String[] args)
{
int arr[] = { 3 , 2 , 1 };
int N = arr.length;
System.out.print(findMaxValByRearrArr(arr, N));
}
}
|
Python3
def findMaxValByRearrArr(arr, N):
arr.sort()
res = 0
while ( True ):
Sum = 0
for i in range (N):
Sum + = __gcd(i + 1 , arr[i])
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
arr = [ 3 , 2 , 1 ]
N = len (arr)
print (findMaxValByRearrArr(arr, N))
|
C#
using System;
class GFG
{
static int findMaxValByRearrArr( int []arr, int N)
{
Array.Sort(arr);
int res = 0;
do
{
int sum = 0;
for ( int i = 0; i < N; i++)
{
sum += __gcd(i + 1, arr[i]);
}
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 ;
}
public static void Main(String[] args)
{
int []arr = { 3, 2, 1 };
int N = arr.Length;
Console.Write(findMaxValByRearrArr(arr, N));
}
}
|
Javascript
<script>
function findMaxValByRearrArr(arr, N)
{
arr.sort();
let res = 0;
do {
let sum = 0;
for (let i = 0; i < N; i++) {
sum += __gcd(i + 1, arr[i]);
}
res = Math.max(res, sum);
} while (next_permutation(arr));
return res;
}
function __gcd(a, b)
{
return b == 0? a:__gcd(b, a % b);
}
function next_permutation(p)
{
for (let a = p.length - 2; a >= 0; --a)
if (p[a] < p[a + 1])
for (let b = p.length - 1;; --b)
if (p[b] > p[a])
{
let 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 ;
}
let arr = [ 3, 2, 1 ];
let N = arr.length;
document.write(findMaxValByRearrArr(arr, N));
</script>
|
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++
#include <bits/stdc++.h>
using namespace std;
int findMaxValByRearrArr( int arr[], int N)
{
int res = 0;
res = (N * (N + 1)) / 2;
return res;
}
int main()
{
int arr[] = { 3, 2, 1 };
int N = sizeof (arr) / sizeof (arr[0]);
cout << findMaxValByRearrArr(arr, N);
return 0;
}
|
Java
import java.util.*;
class GFG
{
static int findMaxValByRearrArr( int arr[], int N)
{
int res = 0 ;
res = (N * (N + 1 )) / 2 ;
return res;
}
public static void main(String[] args)
{
int arr[] = { 3 , 2 , 1 };
int N = arr.length;
System.out.print(findMaxValByRearrArr(arr, N));
}
}
|
Python3
def findMaxValByRearrArr(arr, N):
res = 0 ;
res = (N * (N + 1 )) / / 2 ;
return res;
if __name__ = = '__main__' :
arr = [ 3 , 2 , 1 ];
N = len (arr);
print (findMaxValByRearrArr(arr, N));
|
C#
using System;
class GFG{
static int findMaxValByRearrArr( int []arr, int N)
{
int res = 0;
res = (N * (N + 1)) / 2;
return res;
}
public static void Main(String[] args)
{
int []arr = { 3, 2, 1 };
int N = arr.Length;
Console.Write(findMaxValByRearrArr(arr, N));
}
}
|
Javascript
<script>
function findMaxValByRearrArr(arr, N)
{
let res = 0;
res = parseInt((N * (N + 1)) / 2, 10);
return res;
}
let arr = [ 3, 2, 1 ];
let N = arr.length;
document.write(findMaxValByRearrArr(arr, N));
</script>
|
Time Complexity: O(1)
Auxiliary Space: O(1)
Like Article
Suggest improvement
Share your thoughts in the comments
Please Login to comment...