通过用它们的总和替换值小于 K 的子数组来减少给定数组
给定一个由N个正整数和一个正整数K组成的数组arr[] ,任务是通过将小于K的子数组替换为该子数组中元素的总和来更新给定数组。
例子:
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
方法:给定的问题可以通过遍历数组来解决,并跟踪所有元素小于K的和并相应地更新数组。请按照以下步骤解决问题:
- 初始化变量,例如sum为0 ,它存储子数组的总和。
- 初始化向量,比如res[] ,它存储满足给定条件的更新数组arr[] 。
- 遍历给定数组,如果arr[i] < K的值,则将 sum 的值更新为sum + arr[i] 。否则,将sum的值更新为0 ,并将值sum和arr[i]添加到向量res[]中。
- 完成上述步骤后,打印向量res[]中存储的值。
下面是上述方法的实现:
C++
// C++ program for the above approach
#include
using namespace std;
// Function to replace all the subarray
// having values < K with their sum
void updateArray(vector& arr, int K)
{
// Stores the sum of subarray having
// elements less than K
int sum = 0;
// Stores the updated array
vector res;
// Traverse the array
for (int i = 0; i < (int)arr.size(); i++) {
// Update the sum
if (arr[i] < K) {
sum += arr[i];
}
// Otherwise, update the vector
else {
if (sum != 0) {
res.push_back(sum);
}
sum = 0;
res.push_back(arr[i]);
}
}
if (sum != 0)
res.push_back(sum);
// Print the result
for (auto& it : res)
cout << it << ' ';
}
// Driver Code
int main()
{
vector arr = { 200, 6, 36, 612, 121,
66, 63, 39, 668, 108 };
int K = 100;
updateArray(arr, K);
return 0;
}
Java
// Java program for the above approach
import java.util.*;
class GFG {
// Function to replace all the subarray
// having values < K with their sum
static void updateArray(int []arr, int K)
{
// Stores the sum of subarray having
// elements less than K
int sum = 0;
// Stores the updated array
ArrayList res
= new ArrayList();
// Traverse the array
for (int i = 0; i < arr.length; i++) {
// Update the sum
if (arr[i] < K) {
sum += arr[i];
}
// Otherwise, update the vector
else {
if (sum != 0) {
res.add(sum);
}
sum = 0;
res.add(arr[i]);
}
}
if (sum != 0)
res.add(sum);
// Print the result
for (int i = 0; i < res.size(); i++)
System.out.print(res.get(i) + " ");
}
// Driver Code
public static void main(String []args)
{
int []arr = {200, 6, 36, 612, 121,
66, 63, 39, 668, 108 };
int K = 100;
updateArray(arr, K);
}
}
// This code is contributed by SURENDRA_GANGWAR.
Python3
# python program for the above approach
# Function to replace all the subarray
# having values < K with their sum
def updateArray(arr, K):
# Stores the sum of subarray having
# elements less than K
sum = 0
# Stores the updated array
res = []
# Traverse the array
for i in range(0, int(len(arr))):
# Update the sum
if (arr[i] < K):
sum += arr[i]
# Otherwise, update the vector
else:
if (sum != 0):
res.append(sum)
sum = 0
res.append(arr[i])
if (sum != 0):
res.append(sum)
# Print the result
for it in res:
print(it, end=" ")
# Driver Code
if __name__ == "__main__":
arr = [200, 6, 36, 612, 121, 66, 63, 39, 668, 108]
K = 100
updateArray(arr, K)
# This code is contributed by rakeshsahni
C#
// C# program for the above approach
using System;
using System.Collections.Generic;
class GFG {
// Function to replace all the subarray
// having values < K with their sum
static void updateArray(List arr, int K)
{
// Stores the sum of subarray having
// elements less than K
int sum = 0;
// Stores the updated array
List res = new List();
// Traverse the array
for (int i = 0; i < arr.Count; i++) {
// Update the sum
if (arr[i] < K) {
sum += arr[i];
}
// Otherwise, update the vector
else {
if (sum != 0) {
res.Add(sum);
}
sum = 0;
res.Add(arr[i]);
}
}
if (sum != 0)
res.Add(sum);
// Print the result
foreach(int it in res) Console.Write(it + " ");
}
// Driver Code
public static void Main()
{
List arr
= new List{ 200, 6, 36, 612, 121,
66, 63, 39, 668, 108 };
int K = 100;
updateArray(arr, K);
}
}
// This code is contributed by ukasp.
Javascript
200 42 612 121 168 668 108
时间复杂度: O(N)
辅助空间: O(1)