Reduce given array by replacing subarrays with values less than K with their sum
Last Updated :
19 Oct, 2022
Given an array arr[] consisting of N positive integers and a positive integer K, the task is to update the given array by replacing the subarrays that are less than K with the sum of the elements in that subarray.
Examples:
Input: arr[] = {200, 6, 36, 612, 121, 66, 63, 39, 668, 108}, K = 100
Output: 200 42 612 121 168 668 108
Explanation:
The subarray arr[1, 2] i.e., {6, 36} have elements less than K(= 100). So, adding and insert them into array as 6 + 36 = 42.
The subarray arr[5, 7] i.e., {66, 63, 39} have elements less than K(= 100). So, adding and insert them into array as 66 + 63 + 39 = 168.
The modified array is {200, 42, 612, 121, 168, 668, 108}
Input: arr[] = {50, 25, 90, 21, 30}, K = 95
Output: 216
Approach: The given problem can be solved by traversing the array and keep the track of the sum having all elements less than K and update the array accordingly. Follow the steps below to solve the problem:
- Initialize the variable, say sum as 0 that stores the sum of the subarray.
- Initialize the vector, say res[] that stores the updated array arr[] satisfying the given criteria.
- Traverse the given array and if the value of arr[i] < K, then update the value of sum as the sum + arr[i]. Otherwise, update the value of sum as 0 and add the value sum and arr[i] to the vector res[].
- After completing the above steps, print the value stored in the vector res[].
Below is the implementation of the above approach:
C++
#include <bits/stdc++.h>
using namespace std;
void updateArray(vector< int >& arr, int K)
{
int sum = 0;
vector< int > res;
for ( int i = 0; i < ( int )arr.size(); i++) {
if (arr[i] < K) {
sum += arr[i];
}
else {
if (sum != 0) {
res.push_back(sum);
}
sum = 0;
res.push_back(arr[i]);
}
}
if (sum != 0)
res.push_back(sum);
for ( auto & it : res)
cout << it << ' ' ;
}
int main()
{
vector< int > arr = { 200, 6, 36, 612, 121,
66, 63, 39, 668, 108 };
int K = 100;
updateArray(arr, K);
return 0;
}
|
Java
import java.util.*;
class GFG {
static void updateArray( int []arr, int K)
{
int sum = 0 ;
ArrayList<Integer> res
= new ArrayList<Integer>();
for ( int i = 0 ; i < arr.length; i++) {
if (arr[i] < K) {
sum += arr[i];
}
else {
if (sum != 0 ) {
res.add(sum);
}
sum = 0 ;
res.add(arr[i]);
}
}
if (sum != 0 )
res.add(sum);
for ( int i = 0 ; i < res.size(); i++)
System.out.print(res.get(i) + " " );
}
public static void main(String []args)
{
int []arr = { 200 , 6 , 36 , 612 , 121 ,
66 , 63 , 39 , 668 , 108 };
int K = 100 ;
updateArray(arr, K);
}
}
|
Python3
def updateArray(arr, K):
sum = 0
res = []
for i in range ( 0 , int ( len (arr))):
if (arr[i] < K):
sum + = arr[i]
else :
if ( sum ! = 0 ):
res.append( sum )
sum = 0
res.append(arr[i])
if ( sum ! = 0 ):
res.append( sum )
for it in res:
print (it, end = " " )
if __name__ = = "__main__" :
arr = [ 200 , 6 , 36 , 612 , 121 , 66 , 63 , 39 , 668 , 108 ]
K = 100
updateArray(arr, K)
|
C#
using System;
using System.Collections.Generic;
class GFG {
static void updateArray(List< int > arr, int K)
{
int sum = 0;
List< int > res = new List< int >();
for ( int i = 0; i < arr.Count; i++) {
if (arr[i] < K) {
sum += arr[i];
}
else {
if (sum != 0) {
res.Add(sum);
}
sum = 0;
res.Add(arr[i]);
}
}
if (sum != 0)
res.Add(sum);
foreach ( int it in res) Console.Write(it + " " );
}
public static void Main()
{
List< int > arr
= new List< int >{ 200, 6, 36, 612, 121,
66, 63, 39, 668, 108 };
int K = 100;
updateArray(arr, K);
}
}
|
Javascript
<script>
function updateArray(arr, K) {
let sum = 0;
let res = [];
for (let i = 0; i < arr.length; i++) {
if (arr[i] < K) {
sum += arr[i];
}
else {
if (sum != 0) {
res.push(sum);
}
sum = 0;
res.push(arr[i]);
}
}
if (sum != 0)
res.push(sum);
for (let it of res)
document.write(it + ' ' );
}
let arr = [200, 6, 36, 612, 121,
66, 63, 39, 668, 108];
let K = 100;
updateArray(arr, K);
</script>
|
Output:
200 42 612 121 168 668 108
Time Complexity: O(N)
Auxiliary Space: O(N)
Share your thoughts in the comments
Please Login to comment...