Given an array arr[] size N such that each array element is either -1, 0, or 1, the task is to check if is it possible to split the array into 3 contiguous subarrays such that the product of the first, second and third subarrays is negative, 0 and positive respectively.
Examples:
Input: arr[] = {-1, 0, 1}, N = 3
Output: -1
0
1
Explanation: Split the array into subarrays {-1}, {0}, {1}.
Input: arr[] = {1, -1, 1, -1}, N = 4
Output: Not Possible
Approach: The problem can be solved by greedily selecting the elements up to first ‘-1‘ having no 0′s as the first subarray and then selecting the elements from the last until the product becomes 1 as the third subarray and the remaining elements as the second subarray. Follow the steps below to solve the problem:
- Initialize two variables, say l and r as -1, to store the index of the last element of the first subarray and the first element of the third subarray.
- Traverse the array arr[] and if l is -1 and arr[i] is 0, then print “Not possible”. Otherwise, if arr[i] is -1, then assign i to l and break out of the loop.
- Traverse the array arr[] in reverse order and if the product in the range [i, N – 1] is greater than 0, then assign i to r and break out of the loop.
- Check if the value l or r is -1 or l ≥ r or count of 0s in the range [l, r] is 0. If any of the condition is true, print “Not Possible”.
- Print the first subarray over the range [0, l], the second subarray over the range [l+1, r-1], and then the last subarray over the range [r, N-1].
Below is the implementation of the above approach:
C++
#include <bits/stdc++.h>
using namespace std;
void PrintAllArrays( int arr[], int N)
{
int l = -1, r = -1;
for ( int i = 0; i < N; i++) {
if (l == -1) {
if (arr[i] == -1) {
l = i;
break ;
}
if (arr[i] == 0) {
cout << "Not Possible" << endl;
return ;
}
}
}
int p = 1;
for ( int i = N - 1; i >= 0; i--) {
p *= arr[i];
if (p > 0) {
r = i;
break ;
}
}
if (l == -1 || r == -1 || l >= r
|| count(arr + l, arr + r + 1, 0) == 0) {
cout << "Not Possible" << endl;
return ;
}
for ( int i = 0; i <= l; i++)
cout << arr[i] << " " ;
cout << "\n" ;
for ( int i = l + 1; i < r; i++)
cout << arr[i] << " " ;
cout << "\n" ;
for ( int i = r; i < N; i++)
cout << arr[i] << " " ;
}
int main()
{
int arr[] = { -1, 0, 1 };
int N = sizeof (arr) / sizeof ( int );
PrintAllArrays(arr, N);
return 0;
}
|
Java
import java.io.*;
import java.lang.*;
import java.util.*;
class GFG{
static void PrintAllArrays( int arr[], int N)
{
int l = - 1 , r = - 1 ;
for ( int i = 0 ; i < N; i++)
{
if (l == - 1 )
{
if (arr[i] == - 1 )
{
l = i;
break ;
}
if (arr[i] == 0 )
{
System.out.println( "Not Possible" );
return ;
}
}
}
int p = 1 ;
for ( int i = N - 1 ; i >= 0 ; i--)
{
p *= arr[i];
if (p > 0 )
{
r = i;
break ;
}
}
if (l == - 1 || r == - 1 || l >= r ||
count(arr, l, r + 1 , 0 ) == 0 )
{
System.out.println( "Not Possible" );
return ;
}
for ( int i = 0 ; i <= l; i++)
System.out.print(arr[i] + " " );
System.out.println();
for ( int i = l + 1 ; i < r; i++)
System.out.print(arr[i] + " " );
System.out.println();
for ( int i = r; i < N; i++)
System.out.print(arr[i] + " " );
}
static int count( int arr[], int start,
int end, int val)
{
int count = 0 ;
for ( int i = start; i < end; i++)
{
if (arr[i] == val)
{
count++;
}
}
return count;
}
public static void main(String[] args)
{
int arr[] = { - 1 , 0 , 1 };
int N = arr.length;
PrintAllArrays(arr, N);
}
}
|
Python3
def PrintAllArrays(arr, N):
l = - 1
r = - 1
for i in range (N):
if (l = = - 1 ):
if (arr[i] = = - 1 ):
l = i
break
if (arr[i] = = 0 ):
print ( "Not Possible" )
return
p = 1
for i in range (N - 1 , - 1 , - 1 ):
p * = arr[i]
if (p > 0 ):
r = i
break
if (l = = - 1 or r = = - 1 or l > = r or
count(arr, l, r + 1 , 0 ) = = 0 ):
print ( "Not Possible" )
return
for i in range ( 0 , l + 1 , 1 ):
print (arr[i], end = " " )
print ()
for i in range (l + 1 , r, 1 ):
print (arr[i], end = " " )
print ()
for i in range (r, N, 1 ):
print (arr[i], end = " " )
def count(arr, start, end, val):
count = 0
for i in range (start, end, 1 ):
if (arr[i] = = val):
count + = 1
return count
arr = [ - 1 , 0 , 1 ]
N = len (arr)
PrintAllArrays(arr, N)
|
C#
using System;
class GFG{
static void PrintAllArrays( int [] arr, int N)
{
int l = -1, r = -1;
for ( int i = 0; i < N; i++)
{
if (l == -1)
{
if (arr[i] == -1)
{
l = i;
break ;
}
if (arr[i] == 0)
{
Console.WriteLine( "Not Possible" );
return ;
}
}
}
int p = 1;
for ( int i = N - 1; i >= 0; i--)
{
p *= arr[i];
if (p > 0)
{
r = i;
break ;
}
}
if (l == -1 || r == -1 || l >= r ||
count(arr, l, r + 1, 0) == 0)
{
Console.WriteLine( "Not Possible" );
return ;
}
for ( int i = 0; i <= l; i++)
Console.Write(arr[i] + " " );
Console.WriteLine();
for ( int i = l + 1; i < r; i++)
Console.Write(arr[i] + " " );
Console.WriteLine();
for ( int i = r; i < N; i++)
Console.Write(arr[i] + " " );
}
static int count( int [] arr, int start,
int end, int val)
{
int count = 0;
for ( int i = start; i < end; i++)
{
if (arr[i] == val)
{
count++;
}
}
return count;
}
public static void Main(String []args)
{
int [] arr = { -1, 0, 1 };
int N = arr.Length;
PrintAllArrays(arr, N);
}
}
|
Javascript
<script>
function PrintAllArrays(arr, N)
{
let l = -1, r = -1;
for (let i = 0; i < N; i++)
{
if (l == -1)
{
if (arr[i] == -1)
{
l = i;
break ;
}
if (arr[i] == 0)
{
document.write( "Not Possible" + "<br/>" );
return ;
}
}
}
let p = 1;
for (let i = N - 1; i >= 0; i--)
{
p *= arr[i];
if (p > 0)
{
r = i;
break ;
}
}
if (l == -1 || r == -1 || l >= r ||
count(arr, l, r + 1, 0) == 0)
{
document.write( "Not Possible" + "<br/>" );
return ;
}
for (let i = 0; i <= l; i++)
document.write(arr[i] + " " );
document.write( "<br/>" );
for (let i = l + 1; i < r; i++)
document.write(arr[i] + " " );
document.write( "<br/>" );
for (let i = r; i < N; i++)
document.write(arr[i] + " " );
}
function count(arr, start, end, val)
{
let count = 0;
for (let i = start; i < end; i++)
{
if (arr[i] == val)
{
count++;
}
}
return count;
}
let arr = [ -1, 0, 1 ];
let N = arr.length;
PrintAllArrays(arr, N);
</script>
|
Time Complexity: O(N)
Auxiliary Space: O(1)