给定一个由N个整数组成的数组arr [] ,任务是检查是否可以使用以下交换操作对数组进行排序:
Swapping of two numbers is valid only if the Greatest Common Divisor of count of set bits of the two numbers is equal to the number of set bits in the smallest element of the array.
如果仅通过执行上述交换就可以对数组进行排序,则打印“是” 。否则,打印“否” 。
例子:
Input: arr[] = {2, 3, 5, 7, 6}
Output: Yes
Explanation:
7 and 6 are needed to be swapped to make the array sorted.
7 has 3 set bits and 6 has 2 set bits.
Since GCD(2, 3) = 1, which is equal to the number of set bits in the smallest integer from the array i.e., 2 (1 set bit).
Therefore, the array can be sorted by swapping (7, 6).
Input: arr[] = {3, 3, 15, 7}
Output: No
Explanation:
15 and 7 are needed to be swapped to make the array sorted.
15 has 4 set bits and 7 has 3 set bits. GCD(4, 3) = 1, which is not equal to the number of set bits in the smallest integer from the array i.e., 3(2 set bit).
Therefore, the array cannot be sorted.
方法:请按照以下步骤解决问题:
- 对给定的数组进行排序并将其存储在辅助数组中(例如dup [] )。
- 遍历数组,对于每个元素,检查其在arr []和dup []中的索引是否相同。如果发现是正确的,则继续下一个索引。
- 否则,如果需要在arr []中交换第i和第j个位置元素,则计算arr [i]的设置位与arr [j]的设置位的GCD ,并检查它是否等于设置位的计数是否在数组的最小元素中。
- 如果不允许进行任何此类交换,请打印“否” 。否则,打印“是” 。
下面是上述方法的实现:
C++
// C++ program for the above approach
#include
using namespace std;
// Function to count number of set
// bits in an integer
int calculateSetBit(int n)
{
int count = 0;
// Traverse every bits
for (int i = 0; i < 32; i++) {
if (n & 1)
count++;
// Right shift by 1
n = n >> 1;
}
return count;
}
// Function to check if sorting the
// given array is possible or not
void sortPossible(int arr[], int n)
{
// Duplicate array
int dup[n];
for (int i = 0; i < n; i++)
dup[i] = arr[i];
// Sorted array to check if the
// original array can be sorted
sort(dup, dup + n);
// Flag variable to check
// if possible to sort
bool flag = 1;
// Calculate bits of smallest
// array element
int bit = calculateSetBit(dup[0]);
// Check every wrong placed
// integer in the array
for (int i = 0; i < n; i++) {
if (arr[i] != dup[i]) {
// Swapping only if GCD of set
// bits is equal to set bits in
// smallest integer
if (__gcd(
calculateSetBit(arr[i]),
bit)
!= bit) {
flag = 0;
break;
}
}
}
// Print the result
if (flag) {
cout << "Yes";
}
else {
cout << "No";
}
return;
}
// Driver Code
int main()
{
int arr[] = { 3, 9, 12, 6 };
int N = sizeof(arr) / sizeof(arr[0]);
// Function Call
sortPossible(arr, N);
return 0;
}
Java
// Java program for the above approach
import java.io.*;
import java.util.*;
class GFG{
// Recursive function to return
// gcd of a and b
static int gcd(int a, int b)
{
// Everything divides 0
if (a == 0)
return b;
if (b == 0)
return a;
// Base case
if (a == b)
return a;
// a is greater
if (a > b)
return gcd(a - b, b);
return gcd(a, b - a);
}
// Function to count number of set
// bits in an integer
static int calculateSetBit(int n)
{
int count = 0;
// Traverse every bits
for(int i = 0; i < 32; i++)
{
if ((n & 1) != 0)
count++;
// Right shift by 1
n = n >> 1;
}
return count;
}
// Function to check if sorting the
// given array is possible or not
static void sortPossible(int arr[], int n)
{
// Duplicate array
int dup[] = new int[n];
for(int i = 0; i < n; i++)
dup[i] = arr[i];
// Sorted array to check if the
// original array can be sorted
Arrays.sort(dup);
// Flag variable to check
// if possible to sort
int flag = 1;
// Calculate bits of smallest
// array element
int bit = calculateSetBit(dup[0]);
// Check every wrong placed
// integer in the array
for(int i = 0; i < n; i++)
{
if (arr[i] != dup[i])
{
// Swapping only if GCD of set
// bits is equal to set bits in
// smallest integer
if (gcd(calculateSetBit(
arr[i]), bit) != bit)
{
flag = 0;
break;
}
}
}
// Print the result
if (flag != 0)
{
System.out.println("Yes");
}
else
{
System.out.println("No");
}
return;
}
// Driver Code
public static void main(String[] args)
{
int arr[] = { 3, 9, 12, 6 };
int N = arr.length;
// Function call
sortPossible(arr, N);
}
}
// This code is contributed by sanjoy_62
Python3
# Python3 program for the above approach
from math import gcd
# Function to count number of set
# bits in an eger
def calculateSetBit(n):
count = 0
# Traverse every bits
for i in range(32):
if (n & 1):
count += 1
# Right shift by 1
n = n >> 1
return count
# Function to check if sorting the
# given array is possible or not
def sortPossible(arr, n):
# Duplicate array
dup = [0] * n
for i in range(n):
dup[i] = arr[i]
# Sorted array to check if the
# original array can be sorted
dup = sorted(dup)
# Flag variable to check
# if possible to sort
flag = 1
# Calculate bits of smallest
# array element
bit = calculateSetBit(dup[0])
# Check every wrong placed
# eger in the array
for i in range(n):
if (arr[i] != dup[i]):
# Swapping only if GCD of set
# bits is equal to set bits in
# smallest eger
if (gcd(calculateSetBit(arr[i]), bit) != bit):
flag = 0
break
# Print the result
if (flag):
print("Yes")
else:
print("No")
return
# Driver Code
if __name__ == '__main__':
arr = [ 3, 9, 12, 6 ]
N = len(arr)
# Function call
sortPossible(arr, N)
# This code is contributed by mohit kumar 29
C#
// C# program for the above approach
using System;
class GFG{
// Recursive function to return
// gcd of a and b
static int gcd(int a, int b)
{
// Everything divides 0
if (a == 0)
return b;
if (b == 0)
return a;
// Base case
if (a == b)
return a;
// a is greater
if (a > b)
return gcd(a - b, b);
return gcd(a, b - a);
}
// Function to count number of set
// bits in an integer
static int calculateSetBit(int n)
{
int count = 0;
// Traverse every bits
for(int i = 0; i < 32; i++)
{
if ((n & 1) != 0)
count++;
// Right shift by 1
n = n >> 1;
}
return count;
}
// Function to check if sorting the
// given array is possible or not
static void sortPossible(int[] arr, int n)
{
// Duplicate array
int[] dup = new int[n];
for(int i = 0; i < n; i++)
dup[i] = arr[i];
// Sorted array to check if the
// original array can be sorted
Array.Sort(dup);
// Flag variable to check
// if possible to sort
int flag = 1;
// Calculate bits of smallest
// array element
int bit = calculateSetBit(dup[0]);
// Check every wrong placed
// integer in the array
for(int i = 0; i < n; i++)
{
if (arr[i] != dup[i])
{
// Swapping only if GCD of set
// bits is equal to set bits in
// smallest integer
if (gcd(calculateSetBit(
arr[i]), bit) != bit)
{
flag = 0;
break;
}
}
}
// Print the result
if (flag != 0)
{
Console.WriteLine("Yes");
}
else
{
Console.WriteLine("No");
}
return;
}
// Driver Code
public static void Main()
{
int[] arr = { 3, 9, 12, 6 };
int N = arr.Length;
// Function call
sortPossible(arr, N);
}
}
// This code is contributed by sanjoy_62
Javascript
Yes
时间复杂度: O(N)
辅助空间: O(N)