给定大小为N的数组arr [] ,任务是重新排列数组元素,以便对于每个索引i (1 <= i <= N – 1),arr [i]和arr [i – 1]的乘积是4的倍数。
例子:
Input: arr[] = {1, 10, 100}
Output: 1, 100, 10
Explanation:
1 * 100 is divisible by 4
100 * 10 is divisible by 4
Input: arr[] = {2, 7, 1, 8, 2, 8}
Output: 7, 8, 1, 8, 2, 2
天真的方法:
解决问题的最简单方法是生成数组的所有可能排列,对于每个排列,请检查每两个连续元素的乘积是否为4的倍数。
时间复杂度: O(N ^ 2 * N!)
辅助空间: O(N!)
高效方法:
请按照以下步骤优化上述方法:
- 初始化以下三个变量:
- 奇数=奇数个元素的数量。
- 4 =被4整除的元素数。
- non_four =不能被4整除的偶数元素的数量。
- 请考虑以下两种情况:
- 情况1:non_four = 0
在这种情况下,不存在无法被4整除的偶数元素。- 最佳方法是先将“奇数”元素首先在数组中的其他位置放置。
- 用偶数元素填充空缺。
- 情况1:non_four = 0
Illustration
arr[] = {1, 1, 1, 4, 4}
Step 0: four = 2, odd = 3
Step 1: {1 _ 1 _ 1}
Step 2: {1 4 1 4 1}
- 因此,为了使该方法在数学上可行,偶数和奇数元素的计数之差不应超过1。
- 情况2:non_four> 0
在这种情况下,数组中甚至存在不能被4整除的元素。
- 遵循与上述完全相同的策略,将被4整除的元素放置在替换位置,然后在空位中放置奇数元素。
- 然后,将所有剩余的偶数元素放在数组的末尾。这是因为两个偶数的乘积始终可被4整除。因此,将偶数元素放置在数组的末尾可确保其中连续元素的乘积可被4整除。
Illustration:
arr[] = {2, 7, 1, 8, 2, 8}
Step 1: four = 2, non_four = 2, odd = 2
Step 2: {_ 8 _ 8}
Step 3: {1 8 7 8}
Step 4: {1 8 7 8 2 2}
- 为了使这在数学上可行,四个> =奇数。
下面是上述方法的实现:
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 divisble
// 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 divisble
// 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 divisble
# 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 divisble
// 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
输出:
7 8 1 8 2 2
时间复杂度: O(N)
辅助空间: O(1)
如果您希望与行业专家一起参加现场课程,请参阅《 Geeks现场课程》和《 Geeks现场课程美国》。