用前 K 和后 K 元素的平均值替换给定 Array 的所有元素
给定一个包含N个正整数和一个整数K的数组arr[] 。任务是用前K个和后K个元素的平均值替换每个数组元素。此外,如果不存在K个元素,则调整使用前后可用的最大元素数。
例子:
Input: arr[] = {9, 7, 3, 9, 1, 8, 11}, K=2
Output: 5 7 6 4 7 7 4
Explanation: For i = 0, average = (7 + 3)/2 = 5
For i = 1, average = (9 + 3 + 9)/3 = 7
For i = 2, average = (9 + 7 + 9 + 1)/4 = 6
For i = 3, average = (7 + 3 + 1 + 8)/4 = 4
For i = 4, average = (3 + 9 + 8 + 11)/4 = 7
For i = 5, average = (9 + 1 + 11)/3 = 7
For i = 6, average = (1 + 8)/2 = 4
Input: arr[] = {13, 26, 35, 41, 23, 18, 38}, K=3
Output: 34 28 24 25 31 34 27
天真的方法:最简单的方法是使用嵌套循环。外循环将从左到右遍历数组,即从i = 0到i < N ,内循环将从索引i - K到除i之外的索引i + K遍历子数组并计算它们的平均值。
下面是上述方法的实现:
C++
// C++ program for the above approach
#include
using namespace std;
// Function to replace all array elements
// with the average of previous and
// next K elements
void findAverage(int arr[], int N, int K)
{
int start, end;
for (int i = 0; i < N; i++) {
int sum = 0;
// Start limit is max(i-K, 0)
start = max(i - K, 0);
// End limit in min(i+K, N-1)
end = min(i + K, N - 1);
int cnt = end - start;
for (int j = start; j <= end; j++) {
// Skipping the current element
if (j == i) {
continue;
}
sum += arr[j];
}
cout << sum / cnt << ' ';
}
}
// Driver Code
int main()
{
int arr[] = { 9, 7, 3, 9, 1, 8, 11 };
int N = sizeof(arr) / sizeof(arr[0]);
int K = 2;
findAverage(arr, N, K);
return 0;
}
Java
// Java program to implement
// the above approach
class GFG {
// Function to replace all array elements
// with the average of previous and
// next K elements
static void findAverage(int[] arr, int N, int K)
{
int start, end;
for (int i = 0; i < N; i++) {
int sum = 0;
// Start limit is max(i-K, 0)
start = Math.max(i - K, 0);
// End limit in min(i+K, N-1)
end = Math.min(i + K, N - 1);
int cnt = end - start;
for (int j = start; j <= end; j++) {
// Skipping the current element
if (j == i) {
continue;
}
sum += arr[j];
}
System.out.print(sum / cnt + " ");
}
}
// Driver Code
public static void main(String[] args)
{
int[] arr = { 9, 7, 3, 9, 1, 8, 11 };
int N = arr.length;
int K = 2;
findAverage(arr, N, K);
}
}
// This code is contributed by ukasp.
Python3
# Python code for the above approach
# Function to replace all array elements
# with the average of previous and
# next K elements
def findAverage(arr, N, K):
start = None
end = None
for i in range(N):
sum = 0
# Start limit is max(i-K, 0)
start = max(i - K, 0)
# End limit in min(i+K, N-1)
end = min(i + K, N - 1)
cnt = end - start
for j in range(start, end + 1):
# Skipping the current element
if j == i:
continue
sum += arr[j]
print((sum // cnt), end= " ")
# Driver Code
arr = [9, 7, 3, 9, 1, 8, 11]
N = len(arr)
K = 2
findAverage(arr, N, K)
# This code is contributed by gfgking
C#
// C# program to implement
// the above approach
using System;
class GFG
{
// Function to replace all array elements
// with the average of previous and
// next K elements
static void findAverage(int []arr, int N, int K)
{
int start, end;
for (int i = 0; i < N; i++) {
int sum = 0;
// Start limit is max(i-K, 0)
start = Math.Max(i - K, 0);
// End limit in min(i+K, N-1)
end = Math.Min(i + K, N - 1);
int cnt = end - start;
for (int j = start; j <= end; j++) {
// Skipping the current element
if (j == i) {
continue;
}
sum += arr[j];
}
Console.Write(sum / cnt + " ");
}
}
// Driver Code
public static void Main()
{
int []arr = { 9, 7, 3, 9, 1, 8, 11 };
int N = arr.Length;
int K = 2;
findAverage(arr, N, K);
}
}
// This code is contributed by Samim Hossain Mondal.
Javascript
C++
// C++ program for the above approach
#include
using namespace std;
// Function to replace all array elements
// with the average of previous and
// next K elements
void findAverage(int arr[], int N, int K)
{
int i, sum = 0, next, prev, update;
int cnt = 0;
// Calculate initial sum of K+1 elements
for (i = 0; i <= K and i < N; i++) {
sum += arr[i];
cnt += 1;
}
// Using the sliding window technique
for (i = 0; i < N; i++) {
update = sum - arr[i];
cout << update / (cnt - 1) << " ";
next = i + K + 1;
prev = i - K;
if (next < N) {
sum += arr[next];
cnt += 1;
}
if (prev >= 0) {
sum -= arr[prev];
cnt-=1;
}
}
}
// Driver Code
int main()
{
int arr[] = { 9, 7, 3, 9, 1, 8, 11 };
int N = sizeof(arr) / sizeof(arr[0]);
int K = 2;
findAverage(arr, N, K);
return 0;
}
Java
// Java program for the above approach
import java.util.*;
public class GFG
{
// Function to replace all array elements
// with the average of previous and
// next K elements
static void findAverage(int arr[], int N, int K)
{
int i, sum = 0, next = 0, prev = 0, update = 0;
int cnt = 0;
// Calculate initial sum of K+1 elements
for (i = 0; i <= K && i < N; i++) {
sum += arr[i];
cnt += 1;
}
// Using the sliding window technique
for (i = 0; i < N; i++) {
update = sum - arr[i];
System.out.print(update / (cnt - 1) + " ");
next = i + K + 1;
prev = i - K;
if (next < N) {
sum += arr[next];
cnt += 1;
}
if (prev >= 0) {
sum -= arr[prev];
cnt-=1;
}
}
}
// Driver Code
public static void main(String[] args)
{
int arr[] = { 9, 7, 3, 9, 1, 8, 11 };
int N = arr.length;
int K = 2;
findAverage(arr, N, K);
}
}
// This code is contributed by Samim Hossain Mondal
Python3
# Python program for the above approach
# Function to replace all array elements
# with the average of previous and
# next K elements
def findAverage(arr, N, K):
sum = 0; next = 0; prev = 0; update = 0;
cnt = 0;
# Calculate initial sum of K+1 elements
for i in range(0, K + 1, 1):
if(i >= N):
break
sum += arr[i];
cnt += 1;
# Using the sliding window technique
for i in range(0, N):
update = sum - arr[i];
print(update // (cnt - 1), end=" ");
next = i + K + 1;
prev = i - K;
if (next < N):
sum += arr[next];
cnt += 1;
if (prev >= 0):
sum -= arr[prev];
cnt -= 1;
# Driver Code
if __name__ == '__main__':
arr = [9, 7, 3, 9, 1, 8, 11];
N = len(arr);
K = 2;
findAverage(arr, N, K);
# This code is contributed by 29AjayKumar
C#
// C# program for the above approach
using System;
public class GFG
{
// Function to replace all array elements
// with the average of previous and
// next K elements
static void findAverage(int []arr, int N, int K)
{
int i, sum = 0, next = 0, prev = 0, update = 0;
int cnt = 0;
// Calculate initial sum of K+1 elements
for (i = 0; i <= K && i < N; i++) {
sum += arr[i];
cnt += 1;
}
// Using the sliding window technique
for (i = 0; i < N; i++) {
update = sum - arr[i];
Console.Write(update / (cnt - 1) + " ");
next = i + K + 1;
prev = i - K;
if (next < N) {
sum += arr[next];
cnt += 1;
}
if (prev >= 0) {
sum -= arr[prev];
cnt-=1;
}
}
}
// Driver Code
public static void Main(String[] args)
{
int []arr = { 9, 7, 3, 9, 1, 8, 11 };
int N = arr.Length;
int K = 2;
findAverage(arr, N, K);
}
}
// This code is contributed by 29AjayKumar
Javascript
5 7 6 4 7 7 4
时间复杂度: O(N 2 )
辅助空间: O(1)
高效的方法:这种方法使用滑动窗口的方法。按照下面提到的步骤来实现这个概念:
- 考虑每个元素都有K个下一个和前一个元素,并采用大小为 2*K + 1 的窗口来覆盖整个范围。
- 现在首先找到前 (K+1) 个元素的总和。
- 遍历数组时:
- 通过将总和除以(window-1 的大小)来计算平均值。
- 在当前窗口的最右端之后添加下一个元素。
- 移除当前窗口最左边的元素。这会将窗口向右移动一个位置
- 打印结果数组。
下面是上述方法的实现:
C++
// C++ program for the above approach
#include
using namespace std;
// Function to replace all array elements
// with the average of previous and
// next K elements
void findAverage(int arr[], int N, int K)
{
int i, sum = 0, next, prev, update;
int cnt = 0;
// Calculate initial sum of K+1 elements
for (i = 0; i <= K and i < N; i++) {
sum += arr[i];
cnt += 1;
}
// Using the sliding window technique
for (i = 0; i < N; i++) {
update = sum - arr[i];
cout << update / (cnt - 1) << " ";
next = i + K + 1;
prev = i - K;
if (next < N) {
sum += arr[next];
cnt += 1;
}
if (prev >= 0) {
sum -= arr[prev];
cnt-=1;
}
}
}
// Driver Code
int main()
{
int arr[] = { 9, 7, 3, 9, 1, 8, 11 };
int N = sizeof(arr) / sizeof(arr[0]);
int K = 2;
findAverage(arr, N, K);
return 0;
}
Java
// Java program for the above approach
import java.util.*;
public class GFG
{
// Function to replace all array elements
// with the average of previous and
// next K elements
static void findAverage(int arr[], int N, int K)
{
int i, sum = 0, next = 0, prev = 0, update = 0;
int cnt = 0;
// Calculate initial sum of K+1 elements
for (i = 0; i <= K && i < N; i++) {
sum += arr[i];
cnt += 1;
}
// Using the sliding window technique
for (i = 0; i < N; i++) {
update = sum - arr[i];
System.out.print(update / (cnt - 1) + " ");
next = i + K + 1;
prev = i - K;
if (next < N) {
sum += arr[next];
cnt += 1;
}
if (prev >= 0) {
sum -= arr[prev];
cnt-=1;
}
}
}
// Driver Code
public static void main(String[] args)
{
int arr[] = { 9, 7, 3, 9, 1, 8, 11 };
int N = arr.length;
int K = 2;
findAverage(arr, N, K);
}
}
// This code is contributed by Samim Hossain Mondal
Python3
# Python program for the above approach
# Function to replace all array elements
# with the average of previous and
# next K elements
def findAverage(arr, N, K):
sum = 0; next = 0; prev = 0; update = 0;
cnt = 0;
# Calculate initial sum of K+1 elements
for i in range(0, K + 1, 1):
if(i >= N):
break
sum += arr[i];
cnt += 1;
# Using the sliding window technique
for i in range(0, N):
update = sum - arr[i];
print(update // (cnt - 1), end=" ");
next = i + K + 1;
prev = i - K;
if (next < N):
sum += arr[next];
cnt += 1;
if (prev >= 0):
sum -= arr[prev];
cnt -= 1;
# Driver Code
if __name__ == '__main__':
arr = [9, 7, 3, 9, 1, 8, 11];
N = len(arr);
K = 2;
findAverage(arr, N, K);
# This code is contributed by 29AjayKumar
C#
// C# program for the above approach
using System;
public class GFG
{
// Function to replace all array elements
// with the average of previous and
// next K elements
static void findAverage(int []arr, int N, int K)
{
int i, sum = 0, next = 0, prev = 0, update = 0;
int cnt = 0;
// Calculate initial sum of K+1 elements
for (i = 0; i <= K && i < N; i++) {
sum += arr[i];
cnt += 1;
}
// Using the sliding window technique
for (i = 0; i < N; i++) {
update = sum - arr[i];
Console.Write(update / (cnt - 1) + " ");
next = i + K + 1;
prev = i - K;
if (next < N) {
sum += arr[next];
cnt += 1;
}
if (prev >= 0) {
sum -= arr[prev];
cnt-=1;
}
}
}
// Driver Code
public static void Main(String[] args)
{
int []arr = { 9, 7, 3, 9, 1, 8, 11 };
int N = arr.Length;
int K = 2;
findAverage(arr, N, K);
}
}
// This code is contributed by 29AjayKumar
Javascript
5 7 6 4 7 7 4
时间复杂度: O(N)
辅助空间: O(1)