📌  相关文章
📜  重新排列数组,使数组中的所有偶数索引元素都是偶数

📅  最后修改于: 2021-10-25 11:17:14             🧑  作者: Mango

给定一个数组arr[] ,任务是检查是否可以以每个偶数索引(基于 1 的索引)包含偶数的方式重新排列数组。如果这种重新排列是不可能的,打印“否”。否则,打印“是”并打印可能的安排

例子:

方法:
请按照以下步骤解决问题:

  • 计算给定数组中存在的偶数元素的总数。如果计数超过给定数组中偶数索引的总数,则打印“否”。
  • 否则,打印“是”。现在,使用两个指针ij遍历数组,分别指向偶数和奇数索引。
  • 对于包含奇数元素的任何i索引,使用j迭代奇数索引,直到遇到偶数元素。
  • 交换a[i]a[j]
  • 重复上述步骤,直到所有偶数索引都被一个偶数元素填充。

下面是上述方法的实现:

C++
// C++ Program to implement
// the above approach
#include 
using namespace std;
 
// Function to check if it the
// array can be rearranged such
// such that every even indices
// contains an even element
void checkPossible(int a[], int n)
{
    // Stores the count of even elements
    int even_no_count = 0;
 
    // Traverse array to count even numbers
    for (int i = 0; i < n; i++)
    {
        if (a[i] % 2 == 0)
            even_no_count++;
    }
 
    // If even_no_count exceeds
    // count of even indices
    if (n / 2 > even_no_count)
    {
        cout << "No" << endl;
        return;
    }
 
    cout << "Yes" << endl;
    int j = 0;
    for (int i = 1; i < n; i += 2)
    {
        if (a[i] % 2 == 0)
            continue;
        else
        {
            while (j < n && a[j] % 2 != 0)
                j += 2;
 
            a[i] += a[j];
            a[j] = a[i] - a[j];
            a[i] -= a[j];
        }
    }
 
    for (int i = 0; i < n; i++)
    {
        cout << a[i] << " ";
    }
}
 
// Driver Code
int main()
{
 
    int arr[] = { 2, 3, 4, 5, 6, 7 };
    int n = sizeof(arr) / sizeof(arr[0]);
    checkPossible(arr, n);
    return 0;
}
 
// This code is contributed by gauravrajput1


Java
// Java Program to implement
// the above approach
import java.io.*;
import java.util.*;
 
class GFG {
 
    // Function to check if it the
    // array can be rearranged such
    // such that every even indices
    // contains an even element
    static void checkPossible(int a[])
    {
        // Stores the count of even elements
        int even_no_count = 0;
 
        // Traverse array to count even numbers
        for (int i = 0; i < a.length; i++) {
 
            if (a[i] % 2 == 0)
                even_no_count++;
        }
 
        // If even_no_count exceeds
        // count of even indices
        if (a.length / 2 > even_no_count) {
            System.out.println("No");
            return;
        }
 
        System.out.println("Yes");
        int j = 0;
        for (int i = 1; i < a.length; i += 2) {
            if (a[i] % 2 == 0)
                continue;
            else {
                while (j < a.length && a[j] % 2 != 0)
                    j += 2;
 
                a[i] += a[j];
                a[j] = a[i] - a[j];
                a[i] -= a[j];
            }
        }
 
        for (int i = 0; i < a.length; i++) {
            System.out.print(a[i] + " ");
        }
    }
 
    // Driver Code
    public static void main(String args[])
    {
 
        int arr[] = { 2, 3, 4, 5, 6, 7 };
 
        checkPossible(arr);
    }
}


Python3
# Python3 program to implement
# the above approach
 
# Function to check if it the
# array can be rearranged such
# such that every even indices
# contains an even element
def checkPossible(a, n):
     
    # Stores the count of even elements
    even_no_count = 0
 
    # Traverse array to count even numbers
    for i in range(n):
        if (a[i] % 2 == 0):
            even_no_count += 1
     
    # If even_no_count exceeds
    # count of even indices
    if (n // 2 > even_no_count):
        print("No")
        return
     
    print("Yes")
    j = 0
     
    for i in range(1, n, 2):
        if (a[i] % 2 == 0):
            continue
             
        else:
            while (j < n and a[j] % 2 != 0):
                j += 2
 
            a[i] += a[j]
            a[j] = a[i] - a[j]
            a[i] -= a[j]
         
    for i in range(n):
        print(a[i], end = " ")
     
# Driver Code
arr = [ 2, 3, 4, 5, 6, 7 ]
n = len(arr)
 
checkPossible(arr, n)
 
# This code is contributed by code_hunt


C#
// C# Program to implement
// the above approach
using System;
class GFG{
 
  // Function to check if it the
  // array can be rearranged such
  // such that every even indices
  // contains an even element
  static void checkPossible(int []a)
  {
    // Stores the count of even elements
    int even_no_count = 0;
 
    // Traverse array to count even numbers
    for (int i = 0; i < a.Length; i++)
    {
      if (a[i] % 2 == 0)
        even_no_count++;
    }
 
    // If even_no_count exceeds
    // count of even indices
    if (a.Length / 2 > even_no_count)
    {
      Console.WriteLine("No");
      return;
    }
 
    Console.WriteLine("Yes");
    int j = 0;
    for (int i = 1; i < a.Length; i += 2)
    {
      if (a[i] % 2 == 0)
        continue;
      else
      {
        while (j < a.Length && a[j] % 2 != 0)
          j += 2;
 
        a[i] += a[j];
        a[j] = a[i] - a[j];
        a[i] -= a[j];
      }
    }
 
    for (int i = 0; i < a.Length; i++)
    {
      Console.Write(a[i] + " ");
    }
  }
 
  // Driver Code
  public static void Main(String []args)
  {
    int []arr = { 2, 3, 4, 5, 6, 7 };
 
    checkPossible(arr);
  }
}
 
// This code is contributed by Rajput-Ji


Javascript


输出:
Yes
3 2 5 4 7 6

时间复杂度: O(N)
辅助空间: O(1)

如果您希望与专家一起参加现场课程,请参阅DSA 现场工作专业课程学生竞争性编程现场课程