Permutation of Array such that products of all adjacent elements are even
Last Updated :
07 Mar, 2022
Given an array arr[] consisting of N positive integers, the task is to find any permutation of given array such that the product of adjacent elements is even. Print any such permutation or -1 if not possible.
Example:
Input: arr[] = {6,7,9,8,10,11}
Output: 8 9 10 7 6 11
Explanation:
Product of adjacent elements =>
8 x 9 = 72 (even)
9 x 10 = 90 (even)
10 x 7 = 70 (even)
7 x 6 = 42 (even)
6 x 11 = 66 (even)
Input: arr[] = {3,2,5,7,1,4,9}
Output: -1
Explanation: There is no possible arrangements of elements such that product of adjacent elements is equal.
Naive Approach: The simplest approach to solve this problem is to try every possible arrangement of the elements and check the condition to be true.
Time Complexity: O(N*N!) where N is the number of elements in the array. O(N!) is the time taken to create all permutations of the given array and O(N) is the time required to check if the current permutation is the required one or not.
Auxiliary Space: O(N) to store the permutation each time.
Efficient Approach: The solution can be found using simple observations. If there are multiple odd and even elements in the array then an optimal arrangement of any adjacent elements can be either of the below cases for the product to be even:
{Odd, Even}
{Even, Odd}
{Even, Even}
Please note that {Odd, Odd} arrangement of any adjacent element will give an Odd product. Hence, this arrangement is not possible.
The above arrangements is only possible if
number_of_odd_elements <= number_of_even_elements + 1 in the array.
Follow the steps below to solve the problem.
- Take two vectors even and odd to store the even and odd elements of the array separately.
- If the size of odd vector is greater than size of even vector + 1, (as explained above) then solution is not possible. Therefore, print -1.
- Else first print one element from the odd vector and then one element from even vector until both vectors are empty.
Below is the implementation of the above approach.
C++
// C++ program to Permutation of Array
// such that product of all
// adjacent elements is even
#include <bits/stdc++.h>
using namespace std;
// Function to print
// the required permutation
void printPermutation(int arr[], int n)
{
vector<int> odd, even;
// push odd elements in 'odd'
// and even elements in 'even'
for (int i = 0; i < n; i++) {
if (arr[i] % 2 == 0)
even.push_back(arr[i]);
else
odd.push_back(arr[i]);
}
int size_odd = odd.size();
int size_even = even.size();
// Check if it possible to
// arrange the elements
if (size_odd > size_even + 1)
cout << -1 << endl;
// else print the permutation
else {
int i = 0;
int j = 0;
while (i < size_odd && j < size_even) {
cout << odd[i] << " ";
++i;
cout << even[j] << " ";
++j;
}
// Print remaining odds are even.
// and even elements
while (i < size_odd) {
cout << odd[i] << " ";
++i;
}
while (j < size_even) {
cout << even[j] << " ";
}
}
}
// Driver code
int main()
{
int arr[] = { 6, 7, 9, 8, 10, 11 };
int N = sizeof(arr) / sizeof(arr[0]);
printPermutation(arr, N);
return 0;
}
Java
// Java program to permutation of array
// such that product of all adjacent
// elements is even
import java.io.*;
import java.util.*;
class GFG{
// Function to print
// the required permutation
static void printPermutation(int arr[], int n)
{
ArrayList<Integer> odd = new ArrayList<Integer>();
ArrayList<Integer> even = new ArrayList<Integer>();
// push odd elements in 'odd'
// and even elements in 'even'
for(int i = 0; i < n; i++)
{
if (arr[i] % 2 == 0)
even.add(arr[i]);
else
odd.add(arr[i]);
}
int size_odd = odd.size();
int size_even = even.size();
// Check if it possible to
// arrange the elements
if (size_odd > size_even + 1)
System.out.println("-1");
// Else print the permutation
else
{
int i = 0;
int j = 0;
while (i < size_odd && j < size_even)
{
System.out.print(odd.get(i) + " ");
++i;
System.out.print(even.get(j) + " ");
++j;
}
// Print remaining odds are even.
// and even elements
while (i < size_odd)
{
System.out.print(odd.get(i) + " ");
++i;
}
while (j < size_even)
{
System.out.print(even.get(j) + " ");
}
}
}
// Driver Code
public static void main (String[] args)
{
int arr[] = { 6, 7, 9, 8, 10, 11 };
int N = arr.length;
printPermutation(arr, N);
}
}
// This code is contributed by offbeat
Python3
# Python3 program to Permutation of Array
# such that product of all
# adjacent elements is even
# Function to print
# the required permutation
def printPermutation(arr, n):
odd, even = [], []
# push odd elements in 'odd'
# and even elements in 'even'
for i in range(n):
if(arr[i] % 2 == 0):
even.append(arr[i])
else:
odd.append(arr[i])
size_odd = len(odd)
size_even = len(even)
# Check if it possible to
# arrange the elements
if(size_odd > size_even + 1):
print(-1)
# else print the permutation
else:
i, j = 0, 0
while(i < size_odd and j < size_even):
print(odd[i], end = " ")
i += 1
print(even[j], end = " ")
j += 1
# Print remaining odds are even.
# and even elements
while(i < size_odd):
print(odd[i], end = " ")
i += 1
while(j < size_even):
print(even[j], end = " ")
j += 1
# Driver Code
arr = [ 6, 7, 9, 8, 10, 11 ]
N = len(arr)
# Function call
printPermutation(arr, N)
# This code is contributed by Shivam Singh
C#
// C# program to permutation of array
// such that product of all adjacent
// elements is even
using System;
using System.Collections.Generic;
class GFG{
// Function to print
// the required permutation
static void printPermutation(int []arr, int n)
{
List<int> odd = new List<int>();
List<int> even = new List<int>();
// push odd elements in 'odd'
// and even elements in 'even'
for(int i = 0; i < n; i++)
{
if (arr[i] % 2 == 0)
even.Add(arr[i]);
else
odd.Add(arr[i]);
}
int size_odd = odd.Count;
int size_even = even.Count;
// Check if it possible to
// arrange the elements
if (size_odd > size_even + 1)
Console.WriteLine("-1");
// Else print the permutation
else
{
int i = 0;
int j = 0;
while (i < size_odd && j < size_even)
{
Console.Write(odd[i] + " ");
++i;
Console.Write(even[j] + " ");
++j;
}
// Print remaining odds are even.
// and even elements
while (i < size_odd)
{
Console.Write(odd[i] + " ");
++i;
}
while (j < size_even)
{
Console.Write(even[j] + " ");
}
}
}
// Driver Code
public static void Main(String[] args)
{
int []arr = { 6, 7, 9, 8, 10, 11 };
int N = arr.Length;
printPermutation(arr, N);
}
}
// This code is contributed by PrinciRaj1992
JavaScript
<script>
// JavaScript program to permutation of array
// such that product of all adjacent
// elements is even
// Function to print
// the required permutation
function printPermutation(arr, n)
{
let odd = [];
let even = [];
// push odd elements in 'odd'
// and even elements in 'even'
for(let i = 0; i < n; i++)
{
if (arr[i] % 2 == 0)
even.push(arr[i]);
else
odd.push(arr[i]);
}
let size_odd = odd.length;
let size_even = even.length;
// Check if it possible to
// arrange the elements
if (size_odd > size_even + 1)
document.write("-1");
// Else print the permutation
else
{
let i = 0;
let j = 0;
while (i < size_odd && j < size_even)
{
document.write(odd[i] + " ");
++i;
document.write(even[j] + " ");
++j;
}
// Print remaining odds are even.
// and even elements
while (i < size_odd)
{
document.write(odd[i] + " ");
++i;
}
while (j < size_even)
{
document.write(even[j] + " ");
}
}
}
// Driver Code
let arr = [ 6, 7, 9, 8, 10, 11 ];
let N = arr.length;
printPermutation(arr, N);
</script>
Time Complexity: O(N) where N the number of elements. O(N) time is required to traverse the given array and form the odd & even vectors and O(N) is required to print the permutation.
Auxiliary Space: O(N) because the given array elements are distributed among the two vectors.
Similar Reads
Find permutation such that adjacent elements has difference of at least 2 Given a number N, the task is to find a permutation A[] of first N integers such that the absolute difference of adjacent elements is at least 2 i.e., | Ai+1 ? Ai | ? 2 for all 0 ? i < N?1.If no such permutation exists, print -1. Examples: Input: N = 4Output: 3 1 4 2?Explanation: Here A[] = {3, 1
6 min read
Count of product operations to make adjacent Array elements of different parity Given an array arr[] consisting of N elements. At each operation, you can select any 2 adjacent elements in which both the elements are of the same parity and then delete both of them, and insert their product in the same position, the task is to find the minimum number of operations needed for this
8 min read
Make all array elements even by replacing adjacent pair of array elements with their sum Given an array arr[] of size N, the task is to make all array elements even by replacing a pair of adjacent elements with their sum. Examples: Input: arr[] = { 2, 4, 5, 11, 6 }Output: 1Explanation:Replacing a pair (arr[2], arr[3]) with their sum ( = 5 + 11 = 16) modifies arr[] to { 2, 4, 16, 16, 6 }
8 min read
Convert Array such that no two even or odd numbers are adjacent Given an array arr[], the task is to print the minimum number of operations required to convert the array such that neither two even elements are adjacent nor two odd numbers are adjacent. In other words arr[i]%2 != arr[i+1]%2. For that, you can perform certain operations. (consider 0-based indexing
9 min read
Make all the elements of array even with given operations Given an array arr[] of positive integers, find the minimum number of operations required to make all the array elements even where: If there is an odd number, then, increment the element and the next adjacent element by 1.Each increment costs one operation. Note: If there is any number in arr[] whi
6 min read
Make all the elements of array even with given operations Given an array arr[] of positive integers, find the minimum number of operations required to make all the array elements even where: If there is an odd number, then, increment the element and the next adjacent element by 1.Each increment costs one operation. Note: If there is any number in arr[] whi
6 min read