📌  相关文章
📜  通过交换总和为奇数的元素打印字典顺序最小的数组

📅  最后修改于: 2022-05-13 01:57:50.681000             🧑  作者: Mango

通过交换总和为奇数的元素打印字典顺序最小的数组

给定一个包含N个整数的数组。任务是通过多次应用给定操作来找到字典顺序最小的数组。该操作是选择两个元素a ia j (1<=i, j<=N) 使得a i + a j是奇数,然后交换a ia j
例子:

方法:观察到如果两个元素具有不同的奇偶性,则可以交换它们。如果数组中的所有元素都具有相同的奇偶性(奇数 + 奇数和偶数 + 偶数不是奇数) ,则不可能进行交换。因此答案将只是输入数组。否则,您实际上可以交换任何一对元素。假设您要交换 2 个元素 a 和 b,并且它们具有相同的奇偶性。必须有第三个元素 c 具有不同的奇偶性。不失一般性,假设数组是[a, b, c] 。让我们进行以下交换:

  • 交换 a 和 c:
  • 交换 b 和 c: [b, c, a]
  • 交换 a 和 c: [b, a, c]

也就是说,使用 c 作为中间元素来交换 a 和 b,之后总是会回到原来的位置。由于任何一对元素之间都可以交换,我们总是可以对数组进行排序,这将是字典上最小的数组。
下面是上述方法的实现:

C++
// CPP program to find possible
// lexicographically smaller array
// by swapping only elements whose sum is odd
#include 
using namespace std;
 
// Function to find possible lexicographically smaller array
void lexicographically_smaller(int arr[], int n)
{
    // To store number of even and odd integers
    int odd = 0, even = 0;
 
    // Find number of even and odd integers
    for (int i = 0; i < n; i++) {
        if (arr[i] % 2)
            odd++;
        else
            even++;
    }
 
    // If it possible to make
    // lexicographically smaller
    if (odd && even)
        sort(arr, arr + n);
 
    // Print the array
    for (int i = 0; i < n; i++)
        cout << arr[i] << " ";
}
 
// Driver code
int main()
{
    int arr[] = { 1, 5, 4, 3, 2 };
 
    int n = sizeof(arr) / sizeof(arr[0]);
 
    // Function call
    lexicographically_smaller(arr, n);
 
    return 0;
}


Java
// Java program to find possible
// lexicographically smaller array
// by swapping only elements whose sum is odd
import java.util.*;
 
class GFG
{
 
// Function to find possible lexicographically smaller array
static void lexicographically_smaller(int arr[], int n)
{
    // To store number of even and odd integers
    int odd = 0, even = 0;
 
    // Find number of even and odd integers
    for (int i = 0; i < n; i++)
    {
        if (arr[i] % 2 == 1)
            odd++;
        else
            even++;
    }
 
    // If it possible to make
    // lexicographically smaller
    if (odd > 0 && even > 0)
        Arrays.sort(arr);
 
    // Print the array
    for (int i = 0; i < n; i++)
        System.out.print(arr[i] + " ");
}
 
// Driver code
public static void main(String[] args)
{
    int arr[] = { 1, 5, 4, 3, 2 };
 
    int n = arr.length;
 
    // Function call
    lexicographically_smaller(arr, n);
}
}
 
// This code is contributed by Rajput-Ji


Python3
# Python3 program to find possible
# lexicographically smaller array
# by swapping only elements whose sum is odd
 
# Function to find possible
# lexicographically smaller array
def lexicographically_smaller(arr, n):
     
    # To store number of even and odd integers
    odd, even = 0, 0;
 
    # Find number of even and odd integers
    for i in range(n):
        if (arr[i] % 2 == 1):
            odd += 1;
        else:
            even += 1;
 
    # If it possible to make
    # lexicographically smaller
    if (odd > 0 and even > 0):
        arr.sort();
 
    # Print the array
    for i in range(n):
        print(arr[i], end = " ");
 
# Driver code
if __name__ == '__main__':
 
    arr = [ 1, 5, 4, 3, 2 ];
 
    n = len(arr);
 
    # Function call
    lexicographically_smaller(arr, n);
 
# This code contributed by Rajput-Ji


C#
// C# program to find possible
// lexicographically smaller array by
// swapping only elements whose sum is odd
using System;
     
class GFG
{
 
// Function to find possible
// lexicographically smaller array
static void lexicographically_smaller(int []arr, int n)
{
    // To store number of even and odd integers
    int odd = 0, even = 0;
 
    // Find number of even and odd integers
    for (int i = 0; i < n; i++)
    {
        if (arr[i] % 2 == 1)
            odd++;
        else
            even++;
    }
 
    // If it possible to make
    // lexicographically smaller
    if (odd > 0 && even > 0)
        Array.Sort(arr);
 
    // Print the array
    for (int i = 0; i < n; i++)
        Console.Write(arr[i] + " ");
}
 
// Driver code
public static void Main(String[] args)
{
    int []arr = { 1, 5, 4, 3, 2 };
 
    int n = arr.Length;
 
    // Function call
    lexicographically_smaller(arr, n);
}
}
 
// This code is contributed by 29AjayKumar


Javascript


输出:
1 2 3 4 5

时间复杂度:O(N log N)