给定一个由N 个正整数组成的数组arr[] ,任务是根据每个元素的数字的 GCD 递增顺序对数组arr[]进行排序。如果两个或多个元素的GCD相同,则根据它们的值进行排序。
例子:
Input: arr[] = {555, 363, 488, 244}
Output: 244 363 488 555
Explanation:
Following the GCD of the digits of each number:
- 555: GCD(5, 5, 5) = 5.
- 363: GCD(3, 6, 3) = 3.
- 488: GCD(4, 8, 8) = 4.
- 244: GCD(2, 4, 4) = 2.
After sorting according the given criteria, the order of elements are {244, 363, 488, 555}.
Input: arr[] = {555, 363, 488, 244, 444, 5}
Output: 244 363 444 488 5 555
方法:给定的问题可以通过使用带有 sort()函数的比较器函数来解决。比较器函数定义为:
- 它一次接受两个参数,如果第一个参数的 GCD 小于第二个参数,则返回true 。
- 如果GCD值相同,则如果第一个参数小于第二个参数,则返回true 。否则,返回false 。
下面是上述方法的实现:
C++
// C++ program for above approach
#include
using namespace std;
// Function to calculate GCD of two integers
int gcd(int a, int b)
{
// Base case
if (b == 0)
return a;
// Recursively calculate GCD
return gcd(b, a % b);
}
// Function to calculate GCD of
// digits of array elemets
int keyFunc(int n)
{
int getGCD = 0;
while (n > 0)
{
getGCD = gcd(n % 10, getGCD);
// If at point GCD becomes 1,
// return it
if (getGCD == 1)
return 1;
n = n / 10;
}
return getGCD;
}
// Comparator function that compares
// elements according to their gcd value.
bool compare(int o1, int o2)
{
int x = keyFunc(o1);
int y = keyFunc(o2);
if(x == y)
{
return o1 < o2;
}
return x < y;
}
// Function to sort an array in according
// to GCD of digits of array elements
void sortArrayByGCD(vectorarr)
{
vectorlist;
for(int i : arr)
{
list.push_back(i);
}
// Sort the array according to gcd of
// digits using comparator function
sort(list.begin(), list.end(), compare);
// Print the resultant array
for(int i : list)
{
cout << i << " ";
}
}
// Driver code
int main()
{
vectorarr = { 555, 363, 488, 244 };;
sortArrayByGCD(arr);
}
// This code is contributed by nirajgusain5
Java
// Java program for the above approach
import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
class GFG{
// Function to calculate GCD of two integers
static int gcd(int a, int b)
{
// Base case
if (b == 0)
return a;
// Recursively calculate GCD
return gcd(b, a % b);
}
// Function to calculate GCD of
// digits of array elemets
static int keyFunc(int n)
{
int getGCD = 0;
while (n > 0)
{
getGCD = gcd(n % 10, getGCD);
// If at point GCD becomes 1,
// return it
if (getGCD == 1)
return 1;
n = n / 10;
}
return getGCD;
}
// Function to sort an array in according
// to GCD of digits of array elements
public static void sortArrayByGCD(int[] arr)
{
ArrayList list = new ArrayList();
for(int i : arr)
{
list.add(i);
}
// Sort the array according to gcd of
// digits using comparator function
Collections.sort(list, new Comparator()
{
@Override
public int compare(Integer o1, Integer o2)
{
int x = keyFunc(o1) - keyFunc(o2);
if (x == 0)
{
if (o1 > o2)
x = 1;
else
x = -1;
}
return x;
}
});
// Print the resultant array
for(int i : list)
{
System.out.print(i + " ");
}
}
// Driver code
public static void main(String[] args)
{
int arr[] = { 555, 363, 488, 244 };
sortArrayByGCD(arr);
}
}
// This code is contributed by abhinavjain194
Python3
# Python3 program for the above approach
# Function to calculate
# GCD of two integers
def gcd(a, b):
# Base Case
if not b:
return a
# Recursively calculate GCD
return gcd(b, a % b)
# Function to calculate GCD
# of two array elements
def keyFunc(n):
getGCD = int(str(n)[0])
# Update the getGCD
for i in str(n):
getGCD = gcd(getGCD, int(i))
# Return the resultant GCD
return getGCD
# Function to sort an array by
# increasing order of GCD of
# digits of array elements
def sortArrayByGCD(arr):
# Sort the array
arr.sort()
# Sort the array according to gcd of
# digits using comparator function
arr = sorted(arr, key = keyFunc)
# Print the resultant array
print(*arr)
# Driver Code
# Given array
arr = [555, 363, 488, 244]
sortArrayByGCD(arr)
输出:
244 363 488 555
时间复杂度: O(N * log N)
辅助空间: O(1)
如果您想与行业专家一起参加直播课程,请参阅Geeks Classes Live