Count ways to split Array such that sum of max of left and min of right is at least K
Last Updated :
28 Nov, 2022
Given an array A[] consisting of N integers and an integer K. The task is to find the total number of ways in which you can divide the array into two parts such that the sum of the largest element in the left part and the smallest element in the right part is greater than or equal to K.
Examples:
Input: A[] = {1, 2, 3}, N = 3, K = 3
Output: 2
Explanation:
Two ways in which array is divided to satisfy above conditions are:
{1} and {2, 3} -> 1+2>=3(satisfies the condition)
{1, 2} and {3} -> 2+3>=3(satisfies the condition)
Input: A[] = {1, 2, 3, 4, 5}, N = 5, K = 5
Output: 3
Explanation:
{1, 2} and {3, 4, 5} -> 2+3>=5
{1, 2, 3} and {4, 5} -> 3+4>=5
{1, 2, 3, 4} and {5} -> 4+5>=5
Approach:
Use the following idea to solve the given problem:
Prefix array of the maximum element for the left and suffix array of the minimum element for the right can give the maximum and minimum element in constant time
Follow the below steps to solve the given problem:
- Create two arrays left and right .
- left[] array is used to store the maximum elements from 0 to i.
- right[] array is used to store the minimum elements from i to N-1.
- Traverse the given array and check if left[i] + right[i+1] ? K. If the condition is satisfied, Increment the answer.
Below is the implementation of the above approach.
C++
#include <bits/stdc++.h>
using namespace std;
int totalCuts( int N, int K, vector< int >& A)
{
vector< int > left(N), right(N);
left[0] = A[0];
right[N - 1] = A[N - 1];
for ( int i = 1; i < N; i++)
left[i] = max(A[i], left[i - 1]);
for ( int i = N - 2; i >= 0; i--)
right[i] = min(A[i], right[i + 1]);
int ans = 0;
for ( int i = 0; i < N - 1; i++)
if (left[i] + right[i + 1] >= K)
ans++;
return ans;
}
int main()
{
int K = 5;
vector< int > A = { 1, 2, 3, 4, 5 };
int N = A.size();
cout << totalCuts(N, K, A);
return 0;
}
|
Java
import java.io.*;
class GFG {
static int totalCuts( int N, int K, int []A)
{
int [] left = new int [N];
int [] right = new int [N];
left[ 0 ] = A[ 0 ];
right[N - 1 ] = A[N - 1 ];
for ( int i = 1 ; i < N; i++)
left[i] = Math.max(A[i], left[i - 1 ]);
for ( int i = N - 2 ; i >= 0 ; i--)
right[i] = Math.min(A[i], right[i + 1 ]);
int ans = 0 ;
for ( int i = 0 ; i < N - 1 ; i++)
if (left[i] + right[i + 1 ] >= K)
ans++;
return ans;
}
public static void main (String[] args) {
int K = 5 ;
int []A = { 1 , 2 , 3 , 4 , 5 };
int N = A.length;
System.out.println(totalCuts(N, K, A));
}
}
|
Python3
def totalCuts(N, K, A) :
left = [ 0 ] * N;
right = [ 0 ] * N;
left[ 0 ] = A[ 0 ];
right[N - 1 ] = A[N - 1 ];
for i in range ( 1 , N) :
left[i] = max (A[i], left[i - 1 ]);
for i in range (N - 2 , - 1 , - 1 ) :
right[i] = min (A[i], right[i + 1 ]);
ans = 0 ;
for i in range (N - 1 ) :
if (left[i] + right[i + 1 ] > = K) :
ans + = 1 ;
return ans;
if __name__ = = "__main__" :
K = 5 ;
A = [ 1 , 2 , 3 , 4 , 5 ];
N = len (A);
print (totalCuts(N, K, A));
|
C#
using System;
class GFG {
static int totalCuts( int N, int K, int [] A)
{
int [] left = new int [N];
int [] right = new int [N];
left[0] = A[0];
right[N - 1] = A[N - 1];
for ( int i = 1; i < N; i++)
left[i] = Math.Max(A[i], left[i - 1]);
for ( int i = N - 2; i >= 0; i--)
right[i] = Math.Min(A[i], right[i + 1]);
int ans = 0;
for ( int i = 0; i < N - 1; i++)
if (left[i] + right[i + 1] >= K)
ans++;
return ans;
}
public static void Main()
{
int K = 5;
int [] A = { 1, 2, 3, 4, 5 };
int N = A.Length;
Console.WriteLine(totalCuts(N, K, A));
}
}
|
Javascript
function totalCuts(N, K, A)
{
let left= Array(N);
let right= Array(N);
left[0] = A[0];
right[N - 1] = A[N - 1];
for (let i = 1; i < N; i++)
left[i] = Math.max(A[i], left[i - 1]);
for (let i = N - 2; i >= 0; i--)
right[i] = Math.min(A[i], right[i + 1]);
let ans = 0;
for (let i = 0; i < N - 1; i++)
if (left[i] + right[i + 1] >= K)
ans++;
return ans;
}
let K = 5;
let A = [ 1, 2, 3, 4, 5 ];
let N = A.length;
console.log(totalCuts(N, K, A));
|
Time Complexity: O(N)
Auxiliary Space: O(N)
Like Article
Suggest improvement
Share your thoughts in the comments
Please Login to comment...