Maximize non decreasing Array size by replacing Subarray with sum
Last Updated :
24 Nov, 2022
Given an array A[] of size N. In one operation only one subarray can be selected and replaced with the sum of the subarray. The task is to find the maximum size of the array after making it non-decreasing.
Examples:
Input: N = 5, A[] = {5, 1, 6, 6, 6}
Output: 4
Explanation: maximum size non-decreasing array, in this case, is {6, 6, 6, 6} which is obtained by replacing subarray(0, 1) = A[0] + A[1] = 6
Input: N = 9, A[] = {5, 1, 6, 7, 7, 1, 6, 4, 5 }
Output: 6
Explanation: maximum size non-decreasing array, in this case, is {5, 7, 7, 7, 7, 9} which is obtained by replacing subarray(1, 2) = A[1] + A[2] = 7. Subarray(5, 6) = A[5] + A[6] = 7. Subarray(7, 8) = A[7] + A[8] = 9
Approach: This problem can be solved using greedy approach based on the below observation:
Iterate linearly and consider the first element to be made up of subarray from 0 to i. Now to find the remaining elements, find the minimum size subarray whose sum is at least same as the previous element.
Follow the below steps to implement the idea:
- Let the first element of the final non-decreasing subarray be start.
- Iterate from i = 0 to N-1,
- Calculate start as the sum of the prefix of the array till i.
- For each value of start, iterate from j = i+1, and initialize temp=1
- temp store the value of the size of optimal non-decreasing array size for the current value of start.
- Consider subarray starting from j until the sum of the subarray is greater than equal to start.
- If a subarray is found then, increase the temp by 1 and update the start to the new subarray sum.
- Continue this iteration till j becomes N.
- The maximum value of temp among all iterations is the answer.
Below is the Implementation of the above approach:
C++
#include <bits/stdc++.h>
using namespace std;
int findmaxsize( int N, int A[])
{
int ans = 0, sum = 0;
for ( int i = 0; i < N; i++) {
int temp = 1;
sum += A[i];
int j = i + 1;
int start = sum;
while (j < N) {
int count = 0;
int k = j;
while (k < N && count < start) {
count += A[k];
k++;
}
if (count >= start) {
start = count;
temp++;
}
j = k;
}
ans = max(ans, temp);
}
return ans;
}
int main()
{
int A[] = { 5, 1, 6, 6, 6 };
int N = sizeof (A) / sizeof (A[0]);
cout << findmaxsize(N, A) << endl;
return 0;
}
|
Java
import java.io.*;
import java.util.*;
class GFG {
static int findmaxsize( int N, int A[])
{
int ans = 0 , sum = 0 ;
for ( int i = 0 ; i < N; i++) {
int temp = 1 ;
sum += A[i];
int j = i + 1 ;
int start = sum;
while (j < N) {
int count = 0 ;
int k = j;
while (k < N && count < start) {
count += A[k];
k++;
}
if (count >= start) {
start = count;
temp++;
}
j = k;
}
ans = Math.max(ans, temp);
}
return ans;
}
public static void main(String[] args)
{
int A[] = { 5 , 1 , 6 , 6 , 6 };
int N = A.length;
System.out.println(findmaxsize(N, A));
}
}
|
Python3
def findmaxsize(N, A):
ans, Sum = 0 , 0
for i in range (N):
temp = 1
Sum + = A[i]
j = i + 1
start = Sum
while (j < N):
count = 0
k = j
while (k < N and count < start):
count + = A[k]
k + = 1
if (count > = start):
start = count
temp + = 1
j = k
ans = max (ans, temp)
return ans
A = [ 5 , 1 , 6 , 6 , 6 ]
N = len (A)
print (findmaxsize(N, A))
|
C#
using System;
public class GFG {
static int findmaxsize( int N, int []A)
{
int ans = 0, sum = 0;
for ( int i = 0; i < N; i++) {
int temp = 1;
sum += A[i];
int j = i + 1;
int start = sum;
while (j < N) {
int count = 0;
int k = j;
while (k < N && count < start) {
count += A[k];
k++;
}
if (count >= start) {
start = count;
temp++;
}
j = k;
}
ans = Math.Max(ans, temp);
}
return ans;
}
public static void Main( string [] args)
{
int []A = { 5, 1, 6, 6, 6 };
int N = A.Length;
Console.WriteLine(findmaxsize(N, A));
}
}
|
Javascript
function findmaxsize(N, A)
{
let ans = 0, sum = 0;
for (let i = 0; i < N; i++) {
let temp = 1;
sum += A[i];
let j = i + 1;
let start = sum;
while (j < N) {
let count = 0;
let k = j;
while (k < N && count < start) {
count += A[k];
k++;
}
if (count >= start) {
start = count;
temp++;
}
j = k;
}
ans = Math.max(ans, temp);
}
return ans;
}
let A = [ 5, 1, 6, 6, 6 ];
let N = A.length;
console.log(findmaxsize(N, A));
|
Time Complexity: O(N * N)
Auxiliary space: O(1)
Related Articles:
Like Article
Suggest improvement
Share your thoughts in the comments
Please Login to comment...