Given a binary array A[] of length 2*N, the task is to find the minimum number of partitions that follow the below conditions:
- Each array element belongs to exactly one partition
- None of the partitioned subarrays is a palindrome
Note: If there are multiple answers print anyone that satisfies the above conditions and if no such partition exists that satisfies the above condition then print -1.
Examples:
Input: A[] = {1, 0, 1, 1, 0, 1}
Output: 2
?Explanation: One of the valid partitions of A = {1, 0, 1, 1, 0, 1} is {1, 0} and {1, 1, 0, 1} which satisfy above conditions.Hence we can partition an array into 2 subarrays.Input: A[] = {1, 0, 0, 1, 0, 0, 1, 1}
Output: 1
Explanation: Here A = {1, 0, 0, 1, 0, 0, 1, 1} is itself not a palindrome. Then, no more partitioning needs to be done: just take A itself. Hence number of partitions of an array is 1.
Approach: The problem can be solved based on the following observation:
- A trivial impossible case is when an array A consists of only 0's or only 1's as an element and print -1.
- In every other case, a valid partition exists and only 2 partitions are sufficient.
- Suppose A is itself not a palindrome. Then, no more partitioning needs to be done: just take A itself.
- Now we consider the case when A is a palindrome. Note that the length of A is even.
- Suppose the first half of A is not a palindrome. Then, its second half is also not a palindrome, so simply partition it into these two halves.
- Otherwise, the first N elements of A form a palindrome. In this case, consider the string formed by the first N+1 elements of A: this is definitely not a palindrome.
Follow the below steps to solve the problem:
- Check whether an array is a palindrome or not and if the array is not a palindrome then print 1.
- After that check whether array A[] consists of only 0's or only 1's as an element and if it is true then print -1.
- Otherwise, print 2 as an answer for the number of partitions of an array that satisfy conditions.
Below is the implementation of the above approach.
// C++ code to implement the approach
#include <bits/stdc++.h>
using namespace std;
// Function to check if the array is palindrome
bool isPalindrome(int arr[], int n)
{
int flag = 0;
// Loop till array size n/2.
for (int i = 0; i <= n / 2 && n != 0; i++) {
if (arr[i] != arr[n - i - 1]) {
flag = 1;
break;
}
}
if (flag == 1)
return false;
else
return true;
}
// Function to find number of
// partitions of an array
void numOfPartition(int arr[], int n)
{
if (!isPalindrome(arr, n)) {
cout << 1;
}
else {
int k = 0, z = 0;
for (int i = 0; i < n; i++) {
if (arr[i] == 0)
z++;
else
k++;
}
if (k == 0 || z == 0) {
cout << -1;
}
else {
cout << 2;
}
}
}
// Driver Code
int main()
{
int A[] = { 1, 0, 1, 1, 0, 1 };
int N = sizeof(A) / sizeof(A[0]);
// Function call
numOfPartition(A, N);
return 0;
}
// This code is contributed by aarohirai2616.
// Java code to implement the approach
import java.io.*;
import java.util.*;
public class GFG {
// Function to find number of
// partitions of an array
public static void numOfPartition(int arr[], int n)
{
if (!isPalindrome(arr, n)) {
System.out.println(1);
}
else {
int k = 0, z = 0;
for (int i = 0; i < n; i++) {
if (arr[i] == 0)
z++;
else
k++;
}
if (k == 0 || z == 0) {
System.out.println(-1);
}
else {
System.out.println(2);
}
}
}
// Function to check if the array is palindrome
static boolean isPalindrome(int arr[], int n)
{
int flag = 0;
// Loop till array size n/2.
for (int i = 0; i <= n / 2 && n != 0; i++) {
if (arr[i] != arr[n - i - 1]) {
flag = 1;
break;
}
}
if (flag == 1)
return false;
else
return true;
}
// Driver Code
public static void main(String[] args)
{
int[] A = { 1, 0, 1, 1, 0, 1 };
int N = A.length;
// Function Call
numOfPartition(A, N);
}
}
# Python code to implement the approach
# Function to check if the array is palindrome
def isPalindrome(arr, n):
flag = 0
# Loop till array size n/2.
i = 0
while(i <= n // 2 and n != 0):
if (arr[i] != arr[n - i - 1]):
flag = 1
break
i += 1
if (flag == 1):
return False
else:
return True
# Function to find number of
# partitions of an array
def numOfPartition(arr, n):
if (isPalindrome(arr, n) == False):
print(1)
else:
k = 0
z = 0
for i in range(0, n):
if (arr[i] == 0):
z += 1
else:
k += 1
if (k == 0 or z == 0):
print(-1)
else:
print(2)
# Driver Code
A = [1, 0, 1, 1, 0, 1]
N = len(A)
# Function call
numOfPartition(A, N)
# This code is contributed by Samim Hossain Mondal.
// C# code to implement the approach
using System;
public class GFG {
// Function to find number of
// partitions of an array
public static void numOfPartition(int[] arr, int n)
{
if (!isPalindrome(arr, n)) {
Console.WriteLine(1);
}
else {
int k = 0, z = 0;
for (int i = 0; i < n; i++) {
if (arr[i] == 0)
z++;
else
k++;
}
if (k == 0 || z == 0) {
Console.WriteLine(-1);
}
else {
Console.WriteLine(2);
}
}
}
// Function to check if the array is palindrome
static bool isPalindrome(int[] arr, int n)
{
int flag = 0;
// Loop till array size n/2.
for (int i = 0; i <= n / 2 && n != 0; i++) {
if (arr[i] != arr[n - i - 1]) {
flag = 1;
break;
}
}
if (flag == 1)
return false;
else
return true;
}
static public void Main()
{
// Code
int[] A = { 1, 0, 1, 1, 0, 1 };
int N = A.Length;
// Function Call
numOfPartition(A, N);
}
}
// This code is contributed by lokesh
// Javascript code to implement the approach
// Function to check if the array is palindrome
function isPalindrome(arr, n)
{
let flag = 0;
// Loop till array size n/2.
for (let i = 0; i <= n / 2 && n != 0; i++) {
if (arr[i] != arr[n - i - 1]) {
flag = 1;
break;
}
}
if (flag == 1)
return false;
else
return true;
}
// Function to find number of
// partitions of an array
function numOfPartition(arr, n)
{
if (!isPalindrome(arr, n)) {
console.log("1");
}
else {
let k = 0, z = 0;
for (let i = 0; i < n; i++) {
if (arr[i] == 0)
z++;
else
k++;
}
if (k == 0 || z == 0) {
console.log("-1");
}
else {
console.log("2");
}
}
}
// Driver Code
let A = [ 1, 0, 1, 1, 0, 1 ];
let N = A.length;
// Function call
numOfPartition(A, N);
// This code is contributed by Pushpesh Raj.
Output
2
Time Complexity: O(N)
Auxiliary Space: O(1)