Check for Subarray splitting possibility in Prime number
Last Updated :
25 Nov, 2023
Given a positive integer array arr[] of size N and a prime number p, the task is to form an N number of non-empty subarrays by performing a series of steps. In each step, you can select an existing subarray (which may be the result of previous steps) with a length of at least two and split it into two sub-subarrays, if, for each resulting sub-subarray, at least one of the following holds:
- The length of the sub-subarray is one.
- The sum of elements of any one of the two sub-subarrays is greater than or equal to the given prime number.
Examples:
Input: arr = [2, 5, 8, 3] p = 11
Output: Possible to Split
Explanation: We can split the array into [2] and [5, 8, 3] in the first step. Then, in the second step, we can split [5, 8, 3] into [5] and [8, 3]. and in the last step, we can split [8, 3] into [8] and [3]. As a result, the answer is possible.
Input: arr = [4, 2, 1] p = 7
Output: Not possible to split
Explanation: We cannot split the array into two valid subarrays, as none of the valid splitting conditions can satisfy any combination of splitting steps.
Input: arr = [2, 4, 5, 7, 4] p = 13
Output: Not possible to split
Approach: To solve the problem follow the below Intuition:
- We just need subarray of length 2 whose sum >= p, through this subarray we can reduce all elements which are present before and after the subarray one by one.
- If N == 2, can split once.
- If N > 2, we must have subarray of length 2 whose arr[i] + arr[i + 1] >= p. Otherwise it is not possible to split the array into N subarrays.
Follow the steps to solve the problem:
- When splitting an array arr[] if we find a case where arr[i] + arr[i+1] >= p we can split the whole array into N parts.
- Since while splitting the array into subarrays we may reach a stage where size of array arr[] is 3 and only way to split the array is when we have sum of 2 consecutive numbers is greater than or equal to p.
Below is the implementation for the above approach:
C++14
#include <bits/stdc++.h>
using namespace std;
bool isPossibleToSplitArray(vector< int >& arr, int p)
{
int n = arr.size();
if (n <= 2)
return true ;
for ( int i = 1; i < n; i++) {
int sum = arr[i - 1] + arr[i];
if (sum >= p)
return true ;
}
return false ;
}
int main()
{
vector< int > arr = { 2, 5, 8, 3 };
int p = 11;
bool ans = isPossibleToSplitArray(arr, p);
if (ans == true )
cout << "Possible to Split" << endl;
else
cout << "Not Possible to Split" << endl;
return 0;
}
|
Java
import java.io.*;
import java.util.ArrayList;
import java.util.List;
public class Main {
public static boolean isPossibleToSplitArray(List<Integer> arr, int p) {
int n = arr.size();
if (n <= 2 ) {
return true ;
}
for ( int i = 1 ; i < n; i++) {
int sum = arr.get(i - 1 ) + arr.get(i);
if (sum >= p) {
return true ;
}
}
return false ;
}
public static void main(String[] args) {
List<Integer> arr = new ArrayList<>();
arr.add( 2 );
arr.add( 5 );
arr.add( 8 );
arr.add( 3 );
int p = 11 ;
boolean ans = isPossibleToSplitArray(arr, p);
if (ans) {
System.out.println( "Possible to Split" );
} else {
System.out.println( "Not Possible to Split" );
}
}
}
|
Python3
def is_possible_to_split_array(arr, p):
n = len (arr)
if n < = 2 :
return True
for i in range ( 1 , n):
sum_val = arr[i - 1 ] + arr[i]
if sum_val > = p:
return True
return False
if __name__ = = "__main__" :
arr = [ 2 , 5 , 8 , 3 ]
p = 11
ans = is_possible_to_split_array(arr, p)
if ans:
print ( "Possible to Split" )
else :
print ( "Not Possible to Split" )
|
C#
using System;
using System.Collections.Generic;
class Program
{
public static bool IsPossibleToSplitList(List< int > list, int p)
{
int n = list.Count;
if (n <= 2)
{
return true ;
}
for ( int i = 1; i < n; i++)
{
int sum = list[i - 1] + list[i];
if (sum >= p)
{
return true ;
}
}
return false ;
}
public static void Main( string [] args)
{
List< int > list = new List< int > { 2, 5, 8, 3 };
int p = 11;
bool isPossible = IsPossibleToSplitList(list, p);
if (isPossible)
{
Console.WriteLine( "Possible to Split" );
}
else
{
Console.WriteLine( "Not Possible to Split" );
}
}
}
|
Javascript
function isPossibleToSplitArray(arr, p) {
const n = arr.length;
if (n <= 2) {
return true ;
}
for (let i = 1; i < n; i++) {
const sum = arr[i - 1] + arr[i];
if (sum >= p) {
return true ;
}
}
return false ;
}
function main() {
const arr = [2, 5, 8, 3];
const p = 11;
const ans = isPossibleToSplitArray(arr, p);
if (ans) {
console.log( "Possible to Split" );
} else {
console.log( "Not Possible to Split" );
}
}
main();
|
Time Complexity: O(n), where n is the length of the array
Auxiliary Space: O(1), no extra space is required, so it is a constant.
Share your thoughts in the comments
Please Login to comment...