📌  相关文章
📜  重新排列一个数组,使得每两个连续元素的乘积是 4 的倍数

📅  最后修改于: 2021-10-27 03:25:40             🧑  作者: Mango

给定一个大小为N的数组arr[] ,任务是重新排列数组元素,使得对于每个索引i (1 <= i <= N – 1),arr[i] 和 arr[i – 1] 的乘积是 4 的倍数。
例子:

天真的方法:
解决问题的最简单方法是生成数组的所有可能排列,对于每个排列,检查每两个连续元素的乘积是否是 4 的倍数。

时间复杂度: O(N^2 * N!)
辅助空间: O(N!)

有效的方法:
按照以下步骤优化上述方法:

  • 初始化以下三个变量:
    • 奇数=奇数元素的数量。
    • =可被 4 整除的元素
    • non_four =不能被 4 整除偶数元素的数量。
  • 考虑以下两种情况:
    • 情况 1:non_four = 0
      在这种情况下,不存在不能被 4 整除的偶数元素。
      • 最佳方法是首先将“奇数”元素放在数组中的交替位置。
      • 用偶数元素填补空缺。
  • 因此,为了使该方法在数学上可行,偶数和奇数元素的计数之间的差异不应超过 1。
  • 情况 2:non_four > 0

在这种情况下,即使是不能被 4 整除的元素也存在于数组中。

  • 遵循与上述完全相同的策略,将可被 4 整除的元素放在交替位置,然后在空位中放置奇数元素。
  • 然后,将所有剩余的偶数元素放在数组的末尾。这是因为两个偶数的乘积总是可以被 4 整除。 因此,将偶数元素放在数组的末尾可以保证它们之间连续元素的乘积可以被 4 整除。
  • 为了使这在数学上成为可能,四个 >=odd

下面是上述方法的实现:

C++
// C++ Program to rearray array
// elements such that the product
// of every two consecutive
// elements is a multiple of 4
 
#include 
using namespace std;
 
// Function to rearrange array
// elements such that the every
// two consecutive elements is
// a multiple of 4
void Permute(vector& arr,
            int n)
{
 
    int odd = 0, four = 0;
    int non_four = 0;
 
    vector ODD, FOUR,
        NON_FOUR;
 
    for (auto x : arr) {
 
        // If element is odd
        if (x & 1) {
            odd++;
            // Odd
            ODD.push_back(x);
        }
 
        // If element is divisible
        // by 4
        else if (x % 4 == 0) {
            four++;
            // Divisible by 4
            FOUR.push_back(x);
        }
 
        // If element is not
        // divisible by 4
        else {
            non_four++;
            // Even but not divisible
            // by 4
            NON_FOUR.push_back(x);
        }
    }
 
    // Condition for rearrangement
    // to be possible
    if (non_four == 0
        && four >= odd - 1) {
 
        int x = ODD.size();
        int y = FOUR.size();
        int i;
 
        // Print ODD[i] and FOUR[i]
        // consecutively
        for (i = 0; i < x; i++) {
            cout << ODD[i] << " ";
            if (i < y)
                cout << FOUR[i] << " ";
        }
 
        // Print the remaining
        // FOUR[i], if any
        while (i < y)
            cout << FOUR[i] << " ";
        cout << endl;
    }
 
    // Condition for rearrangement
    // to be possible
    else if (non_four > 0
            and four >= odd) {
 
        int x = ODD.size();
        int y = FOUR.size();
        int i;
 
        // Print ODD[i] and FOUR[i]
        // consecutively
        for (i = 0; i < x; i++) {
            cout << ODD[i] << " ";
            if (i < y)
                cout << FOUR[i] << " ";
        }
 
        // Print the remaining
        // FOUR[i], if any
        while (i < y)
            cout << FOUR[i] << " ";
 
        // Print the NON_FOUR[i]
        // elements at the end
        for (int j = 0;
            j < (int)NON_FOUR.size();
            j++)
            cout << NON_FOUR[j] << " ";
        cout << endl;
    }
    else
 
        // No possible configuration
        cout << "Not Possible" << endl;
}
 
// Driver Code
signed main()
{
 
    vector arr = { 2, 7, 1,
                        8, 2, 8 };
    int N = sizeof(arr) / sizeof(arr);
    Permute(arr, N);
 
    return 0;
}


Java
// Java program to rearray array
// elements such that the product
// of every two consecutive
// elements is a multiple of
import java.io.*;
import java.util.*;
 
class GFG{
     
// Function to rearrange array
// elements such that the every
// two consecutive elements is
// a multiple of 4
public static void Permute(Vector arr, int n)
{
    int odd = 0, four = 0;
    int non_four = 0;
 
    Vector ODD = new Vector();
    Vector FOUR = new Vector(n);
    Vector NON_FOUR = new Vector(n);
 
    for(int x : arr)
    {
         
        // If element is odd
        if (x % 2 != 0)
        {
            odd++;
             
            // Odd
            ODD.add(x);
        }
 
        // If element is divisible
        // by 4
        else if (x % 4 == 0)
        {
            four++;
             
            // Divisible by 4
            FOUR.add(x);
        }
 
        // If element is not
        // divisible by 4
        else
        {
            non_four++;
             
            // Even but not divisible
            // by 4
            NON_FOUR.add(x);
        }
    }
 
    // Condition for rearrangement
    // to be possible
    if (non_four == 0 && four >= odd - 1)
    {
        int x = ODD.size();
        int y = FOUR.size();
        int i;
 
        // Print ODD.get(i) and FOUR.get(i)
        // consecutively
        for(i = 0; i < x; i++)
        {
            System.out.print(ODD.get(i) + " ");
             
            if (i < y)
                System.out.print(FOUR.get(i) + " ");
        }
 
        // Print the remaining
        // FOUR.get(i), if any
        while (i < y)
            System.out.print(FOUR.get(i) + " ");
             
        System.out.println();
    }
 
    // Condition for rearrangement
    // to be possible
    else if (non_four > 0 && four >= odd)
    {
        int x = ODD.size();
        int y = FOUR.size();
        int i;
 
        // Print ODD.get(i) and FOUR.get(i)
        // consecutively
        for(i = 0; i < x; i++)
        {
            System.out.print(ODD.get(i) + " ");
             
            if (i < y)
                System.out.print(FOUR.get(i) + " ");
        }
 
        // Print the remaining
        // FOUR.get(i), if any
        while (i < y)
            System.out.print(FOUR.get(i) + " ");
 
        // Print the NON_FOUR.get(i)
        // elements at the end
        for(int j = 0; j < (int)NON_FOUR.size(); j++)
            System.out.print(NON_FOUR.get(j) + " ");
             
        System.out.println();
    }
    else
 
        // No possible configuration
        System.out.println("Not Possible");
}
 
// Driver Code
public static void main(String[] args)
{
    Vector arr = new Vector();
    arr.add(2);
    arr.add(7);
    arr.add(1);
    arr.add(8);
    arr.add(2);
    arr.add(8);
     
    Permute(arr, arr.size());
}
}
 
// This code is contributed by grand_master


Python3
# Python3 program to rearray array
# elements such that the product
# of every two consecutive
# elements is a multiple of 4
 
# Function to rearrange array
# elements such that the every
# two consecutive elements is
# a multiple of 4
def Permute(arr, n):
     
    odd = 0
    four = 0
    non_four = 0
    ODD, FOUR, NON_FOUR = [], [], []
 
    for x in arr:
 
        # If element is odd
        if (x & 1):
            odd += 1
             
            # Odd
            ODD.append(x)
             
        # If element is divisible
        # by 4
        elif (x % 4 == 0):
            four += 1
             
            # Divisible by 4
            FOUR.append(x)
 
        # If element is not
        # divisible by 4
        else:
            non_four += 1
             
            # Even but not divisible
            # by 4
            NON_FOUR.append(x)
             
    # Condition for rearrangement
    # to be possible
    if (non_four == 0 and four >= odd - 1):
        x = len(ODD)
        y = len(FOUR)
        i = 0
 
        # Print ODD[i] and FOUR[i]
        # consecutively
        while i < x:
            print(ODD[i], end = " ")
            if (i < y):
                print(FOUR[i], end = " ")
 
        # Print the remaining
        # FOUR[i], if any
        while (i < y):
            print(FOUR[i], end = " ")
            i += 1
        print()
 
    # Condition for rearrangement
    # to be possible
    elif (non_four > 0 and four >= odd):
        x = len(ODD)
        y = len(FOUR)
        i = 0
 
        # Print ODD[i] and FOUR[i]
        # consecutively
        while i < x:
            print(ODD[i], end = " ")
            if (i < y):
                print(FOUR[i], end = " ")
            i += 1
 
        # Print the remaining
        # FOUR[i], if any
        while (i < y):
            print(FOUR[i], end = " ")
            i += 1
 
        # Print the NON_FOUR[i]
        # elements at the end
        for j in NON_FOUR:
            print(j, end = " ")
    else:
 
        # No possible configuration
        print("Not Possible")
 
# Driver Code
if __name__ == '__main__':
 
    arr = [ 2, 7, 1, 8, 2, 8 ]
    N = len(arr)
     
    Permute(arr, N)
 
# This code is contributed by mohit kumar 29


C#
// C# program to rearray array
// elements such that the product
// of every two consecutive
// elements is a multiple of
using System;
using System.Collections.Generic;
class GFG{
     
// Function to rearrange array
// elements such that the every
// two consecutive elements is
// a multiple of 4
public static void Permute(List arr,
                           int n)
{
  int odd = 0, four = 0;
  int non_four = 0;
 
  List ODD =
            new List();
  List FOUR =
            new List(n);
  List NON_FOUR =
            new List(n);
 
  foreach(int x in arr)
  {
    // If element is odd
    if (x % 2 != 0)
    {
      odd++;
 
      // Odd
      ODD.Add(x);
    }
 
    // If element is divisible
    // by 4
    else if (x % 4 == 0)
    {
      four++;
 
      // Divisible by 4
      FOUR.Add(x);
    }
 
    // If element is not
    // divisible by 4
    else
    {
      non_four++;
 
      // Even but not divisible
      // by 4
      NON_FOUR.Add(x);
    }
  }
 
  // Condition for rearrangement
  // to be possible
  if (non_four == 0 &&
      four >= odd - 1)
  {
    int x = ODD.Count;
    int y = FOUR.Count;
    int i;
 
    // Print ODD[i] and FOUR[i]
    // consecutively
    for(i = 0; i < x; i++)
    {
      Console.Write(ODD[i] + " ");
 
      if (i < y)
        Console.Write(FOUR[i] + " ");
    }
 
    // Print the remaining
    // FOUR[i], if any
    while (i < y)
      Console.Write(FOUR[i] + " ");
 
    Console.WriteLine();
  }
 
  // Condition for rearrangement
  // to be possible
  else if (non_four > 0 &&
           four >= odd)
  {
    int x = ODD.Count;
    int y = FOUR.Count;
    int i;
 
    // Print ODD[i] and FOUR[i]
    // consecutively
    for(i = 0; i < x; i++)
    {
      Console.Write(ODD[i] + " ");
 
      if (i < y)
        Console.Write(FOUR[i] + " ");
    }
 
    // Print the remaining
    // FOUR[i], if any
    while (i < y)
      Console.Write(FOUR[i] + " ");
 
    // Print the NON_FOUR[i]
    // elements at the end
    for(int j = 0;
            j < (int)NON_FOUR.Count; j++)
      Console.Write(NON_FOUR[j] + " ");
 
    Console.WriteLine();
  }
  else
 
    // No possible configuration
    Console.WriteLine("Not Possible");
}
 
// Driver Code
public static void Main(String[] args)
{
  List arr = new List();
  arr.Add(2);
  arr.Add(7);
  arr.Add(1);
  arr.Add(8);
  arr.Add(2);
  arr.Add(8);
 
  Permute(arr, arr.Count);
}
}
 
// This code is contributed by 29AjayKumar


Javascript


输出:

7 8 1 8 2 2

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