Given an array arr[] of size N, the task is to rearrange the array elements such that for every index i(1 <= i <= N – 1), the product of arr[i] and arr[i – 1] is a multiple of 4.
Example:
Input: arr[] = {1, 10, 100}
Output: 1, 100, 10
Explanation:
1 * 100 is divisible by 4
100 * 10 is divisible by 4Input: arr[] = {2, 7, 1, 8, 2, 8}
Output: 7, 8, 1, 8, 2, 2
Naive Approach:
The simplest approach to solve the problem is to generate all possible permutations of the array and for every permutation, check if the product of every two consecutive elements is a multiple of 4 or not.
Time complexity: O(N^2 * N!)
Auxiliary Space: O(N!)
Efficient Approach:
Follow the steps below to optimize the above approach:

Initialize following three variables:
 odd = Number of odd elements.
 four = Number of elements divisible by 4.
 non_four = Number of even elements not divisible by 4.

Consider the following two cases:

Case 1: non_four = 0
In this case, no even elements are present which are not divisible by 4. The optimal way is to first place the “odd” elements first in the array in alternate positions.
 Fill the vacancies with the even elements.
 Hence, for the approach to be mathematically possible, the difference between the count of even and odd elements should not exceed 1.
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} 
Case 2: non_four > 0
In this case, even elements which are not divisible by 4 exist in the array.
 Follow the exact same strategy as depicted above by placing the elements divisible by 4 in alternate positions followed by odd elements in the vacancies.

Then, place all the remaining even elements at the end of the array. This is because product of two even numbers is always divisible by 4. Hence, placing the even elements towards the end of the array guarantees that product of consecutive elements among them is divisible by 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}  For this to be possible mathematically, four >= odd.
Below is the implementation of the above approach:
C++
// C++ Program to rearray array
// elements such that the product
// of every two consecutive
// elements is a multiple of 4
#include <bits/stdc++.h>
using
namespace
std;
// Function to rearrange array
// elements such that the every
// two consecutive elements is
// a multiple of 4
void
Permute(vector<
int
>& arr,
int
n)
{
int
odd = 0, four = 0;
int
non_four = 0;
vector<
int
> 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<
int
> arr = { 2, 7, 1,
8, 2, 8 };
int
N =
sizeof
(arr) /
sizeof
(arr);
Permute(arr, N);
return
0;
}
chevron_rightfilter_nonePython3
# 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
chevron_rightfilter_noneOutput:7 8 1 8 2 2
Time Complexity: O(N)
Auxiliary Space: O(1)Recommended Posts:
 Rearrange an array to minimize sum of product of consecutive pair elements
 Print numbers such that no two consecutive numbers are coprime and every three consecutive numbers are coprime
 Rearrange an array such that every odd indexed element is greater than it previous
 Rearrange array such that even index elements are smaller and odd index elements are greater
 Rearrange array elements such that Bitwise AND of first N  1 elements is equal to last element
 Rearrange array such that all evenindexed elements in the Array is even
 Generate original array from difference between every two consecutive elements
 Find minimum sum such that one of every three consecutive elements is taken
 Minimum sum subsequence such that at least one of every four consecutive elements is picked
 Maximum product from array such that frequency sum of all repeating elements in product is less than or equal to 2 * k
 Rearrange numbers in an array such that no two adjacent numbers are same
 Rearrange array such that difference of adjacent elements is in descending order
 Count array elements that can be represented as sum of at least two consecutive array elements
 Rearrange an Array such that Sum of sameindexed subsets differ from their Sum in the original Array
 Rearrange characters in a string such that no two adjacent are same
 Color all boxes in line such that every M consecutive boxes are unique
 Minimum bit flips such that every K consecutive bits contain at least one set bit
 Rearrange a given list such that it consists of alternating minimum maximum elements
 Replace array elements by sum of next two consecutive elements
 Check if it is possible to create a matrix such that every row has A 1s and every column has B 1s
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.
Improved By : mohit kumar 29

Case 1: non_four = 0