Remove minimum elements from ends of array so that sum decreases by at least K | O(N)
Last Updated :
28 Dec, 2022
Given an array arr[] consisting of N elements, the task is to remove minimum number of elements from the ends of the array such that the total sum of the array decreases by at least K. Note that K will always be less than or equal to the sum of all the elements of the array.
Examples:
Input: arr[] = {1, 11, 5, 5}, K = 11
Output: 2
After removing two elements form the left
end, the sum decreases by 1 + 11 = 12.
Thus, the answer is 2.
Input: arr[] = {1, 2, 3}, K = 6
Output: 3
Approach: A dynamic programming-based approach has already been discussed in another post. In this article, an approach using the two-pointer technique will be discussed. It can be observed that the task is to find the longest sub-array with the sum of its elements at most sum(arr) – K where sum(arr) is the sum of all the elements of the array arr[].
Let the length of such subarray be L. Thus, the minimum number of elements to be removed from the array will be equal to N – L. To find the length of longest such subarray, refer this article.
Below is the implementation of the above approach:
C++
#include <bits/stdc++.h>
using namespace std;
int minCount( int * arr, int n, int k)
{
int ans = 0;
int sum = 0;
for ( int i = 0; i < n; i++)
sum += arr[i];
sum -= k;
int l = 0;
int r = 0;
int tot = 0;
while (l < n) {
if (tot <= sum) {
ans = max(ans, r - l);
if (r == n)
break ;
tot += arr[r++];
}
else {
tot -= arr[l++];
}
}
return (n - ans);
}
int main()
{
int arr[] = { 1, 11, 5, 5 };
int n = sizeof (arr) / sizeof ( int );
int k = 11;
cout << minCount(arr, n, k);
return 0;
}
|
Java
class GFG
{
static int minCount( int arr[],
int n, int k)
{
int ans = 0 ;
int sum = 0 ;
for ( int i = 0 ; i < n; i++)
sum += arr[i];
sum -= k;
int l = 0 ;
int r = 0 ;
int tot = 0 ;
while (l < n)
{
if (tot <= sum)
{
ans = Math.max(ans, r - l);
if (r == n)
break ;
tot += arr[r++];
}
else
{
tot -= arr[l++];
}
}
return (n - ans);
}
public static void main (String[] args)
{
int arr[] = { 1 , 11 , 5 , 5 };
int n = arr.length;
int k = 11 ;
System.out.println(minCount(arr, n, k));
}
}
|
Python3
def minCount(arr, n, k) :
ans = 0 ;
sum = 0 ;
for i in range (n) :
sum + = arr[i];
sum - = k;
l = 0 ;
r = 0 ;
tot = 0 ;
while (l < n) :
if (tot < = sum ) :
ans = max (ans, r - l);
if (r = = n) :
break ;
tot + = arr[r];
r + = 1
else :
tot - = arr[l];
l + = 1
return (n - ans);
if __name__ = = "__main__" :
arr = [ 1 , 11 , 5 , 5 ];
n = len (arr);
k = 11 ;
print (minCount(arr, n, k));
|
C#
using System;
class GFG
{
static int minCount( int []arr,
int n, int k)
{
int ans = 0;
int sum = 0;
for ( int i = 0; i < n; i++)
sum += arr[i];
sum -= k;
int l = 0;
int r = 0;
int tot = 0;
while (l < n)
{
if (tot <= sum)
{
ans = Math.Max(ans, r - l);
if (r == n)
break ;
tot += arr[r++];
}
else
{
tot -= arr[l++];
}
}
return (n - ans);
}
public static void Main()
{
int []arr = { 1, 11, 5, 5 };
int n = arr.Length;
int k = 11;
Console.WriteLine(minCount(arr, n, k));
}
}
|
Javascript
<script>
function minCount(arr, n, k)
{
var ans = 0;
var sum = 0;
for ( var i = 0; i < n; i++)
sum += arr[i];
sum -= k;
var l = 0;
var r = 0;
var tot = 0;
while (l < n) {
if (tot <= sum) {
ans = Math.max(ans, r - l);
if (r == n)
break ;
tot += arr[r++];
}
else {
tot -= arr[l++];
}
}
return (n - ans);
}
var arr = [1, 11, 5, 5 ];
var n = arr.length;
var k = 11;
document.write( minCount(arr, n, k));
</script>
|
Time Complexity: O(n), where n is the size of the given 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...