Reduce a given array by replacing all subarrays with values less than a given number K by their sum



In this article, we will learn how to reduce a given array by replacing all subarrays with values less than a given number K by their sum. This process simplifies the array by merging smaller values.

Given an array of integers and a value K, we need to find all contiguous subarrays whose values are less than K and replace them with their sum. After performing this reduction, the resulting array will have fewer elements, as the smaller subarrays will be combined into one.

Let's consider an example. Given the array:

[1, 2, 3, 5, 1, 4, 6, 8] and K = 4.
  • The subarray [1, 2, 3] has values less than K, so we replace it with the sum 6.
  • The subarray [1] also has a value less than K, so we replace it with the sum 1.

The final result after applying the operation would be: [6, 5, 4, 6, 8].

Approach to Solve the Problem

The general approach to solve this problem is simple:

  • Traverse the array.
  • Identify contiguous subarrays less than K.
  • Replace with their sum.
  • Continue processing the entire array.

Here are the steps we take to implement this -

  • First, we create an empty array to store the reduced numbers.
  • Next, we track the running sum of smaller numbers.
  • Then, we go through the original array. If a number is less than K, we add it to the running sum.
  • If a number is x greater than or equal to Kwe add the previous sum (if any) to the result, and we add the current number to the result.
  • After the loop, if any sum is left, we add it to the result.
  • Finally, we print the result array.

Example

Here's a complete C++ code where we go through the array, add up all the numbers smaller than K, and replace them with their sum. When we find a number greater than or equal to K, we add the sum to the result and start a new sum.

#include <iostream>
#include <vector>
using namespace std;

vector<int> reduceArray(vector<int>& arr, int K) {
   // Prepare an empty result array
   vector<int> result;
   
   // Variable to keep track of sum of small numbers
   int sum = 0;
   
   // Go through each number in the array
   for (int num : arr) {
      // If number is less than K, add to sum
      if (num < K) {
         sum += num;
      } 
      // If number is >= K
      else {
         // If we have a non-zero sum from previous small numbers
         if (sum > 0) {
            // Add that sum to result
            result.push_back(sum);
            // Reset sum
            sum = 0;
         }
         // Add the current number to result
         result.push_back(num);
      }
   }
   
   // After going through all numbers, 
   // check if there's any remaining sum
   if (sum > 0) {
      result.push_back(sum);
   }
   
   // Return the reduced array
   return result;
}


int main() {
   vector<int> arr = {1, 2, 3, 5, 1, 4, 6, 8};
   int K = 4;
   
   cout << "Original Array: ";
   for (int num : arr) cout << num << " ";
   cout << "\n";
   
   vector<int> reducedArray = reduceArray(arr, K);
   
   cout << "Reduced Array (K = " << K << "): ";
   for (int num : reducedArray) cout << num << " ";
   cout << endl;
   
   return 0;
}

Here's the output of the above code, where subarrays with values less than K are replaced by their sum.

Original Array: 1 2 3 5 1 4 6 8 
Reduced Array (K = 4): 6 5 1 4 6 

Time Complexity: O(n), where n is the size of the input array.

Space Complexity: O(n) because we store the modified array in a new vector, which can be of size n in the worst case.

Conclusion

In this article, we learned how to reduce a given array by replacing contiguous subarrays with values less than K with their sum. This technique simplifies large arrays by removing unnecessary elements and creating a more efficient version of the original array.

Updated on: 2025-03-27T15:13:53+05:30

46 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements