Find minimum number of non palindromic partition of Binary Array
Last Updated :
19 Dec, 2022
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++
#include <bits/stdc++.h>
using namespace std;
bool isPalindrome( int arr[], int n)
{
int flag = 0;
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 ;
}
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;
}
}
}
int main()
{
int A[] = { 1, 0, 1, 1, 0, 1 };
int N = sizeof (A) / sizeof (A[0]);
numOfPartition(A, N);
return 0;
}
|
Java
import java.io.*;
import java.util.*;
public class GFG {
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 );
}
}
}
static boolean isPalindrome( int arr[], int n)
{
int flag = 0 ;
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 ;
}
public static void main(String[] args)
{
int [] A = { 1 , 0 , 1 , 1 , 0 , 1 };
int N = A.length;
numOfPartition(A, N);
}
}
|
Python
def isPalindrome(arr, n):
flag = 0
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
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 )
A = [ 1 , 0 , 1 , 1 , 0 , 1 ]
N = len (A)
numOfPartition(A, N)
|
C#
using System;
public class GFG {
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);
}
}
}
static bool isPalindrome( int [] arr, int n)
{
int flag = 0;
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()
{
int [] A = { 1, 0, 1, 1, 0, 1 };
int N = A.Length;
numOfPartition(A, N);
}
}
|
Javascript
function isPalindrome(arr, n)
{
let flag = 0;
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 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" );
}
}
}
let A = [ 1, 0, 1, 1, 0, 1 ];
let N = A.length;
numOfPartition(A, N);
|
Time Complexity: O(N)
Auxiliary Space: O(1)
Like Article
Suggest improvement
Share your thoughts in the comments
Please Login to comment...