Find mean of K adjacent elements on each sides for each Array element
Last Updated :
06 Jun, 2022
Given a circular array arr[] of N numbers, and an integer K. The task is to print the average for 2K+1 numbers for each elements (K from left, K from right, and the element self).
Examples:
Input: arr []= { 1, 2, 3, 4, 5, 6, 7, 8, 9 }, K = 3
Output: {4.85714, 4.57143, 4.28571, 4.0, 5.0, 6.0, 5.71429, 5.42857, 5.14286}
Explanation: For each value the averages are:
for 1 – right part:9, left part:24 & result:4.85714
for 2 – right part:12, left part:18 & result:4.57143
for 3 – right part:15, left part:12 & result:4.28571
for 4 – right part:18, left part:6 & result:4
for 5 – right part:21, left part:9 & result:5
for 6 – right part:24, left part:12 & result:6
for 7 – right part:18, left part:15 & result:5.71429
for 8 – right part:12, left part:18 & result:5.42857
for 9 – right part:6, left part:21 & result:5.14286
Input: arr[] = {2, 2, 2, 2, 2}, K = 3
Output: {2, 2, 2, 2, 2}
Naive Approach: The simplest approach to solve the problem is to traverse the required number of elements for each element of the array. Follow the steps mentioned below:
- Traverse the array and for each element do the following:
- traverse the next K and previous K elements and calculate the mean for these elements.
- Print the answer for each element.
Below is the implementation of the above approach:
C++
#include <iostream>
using namespace std;
void average( int arr[], int N, int K)
{
for ( int i = 0; i < N; i++) {
int leftSum = 0;
int rightSum = 0;
for ( int j = 1; j <= K; j++) {
rightSum += arr[(i + j) % N];
}
for ( int j = 1; j <= K; j++) {
leftSum += arr[(i - j < 0
? i - j + N
: i - j)
% N];
}
cout << ((leftSum + rightSum + arr[i])
* 1.0)
/ (2 * K + 1)
<< " " ;
}
}
int main()
{
int arr[] = { 1, 2, 3, 4, 5, 6, 7, 8, 9 };
int K = 3;
int N = sizeof (arr) / sizeof ( int );
average(arr, N, K);
return 0;
}
|
Java
import java.util.*;
public class GFG{
static void average( int [] arr, int N, int K)
{
for ( int i = 0 ; i < N; i++)
{
int leftSum = 0 ;
int rightSum = 0 ;
for ( int j = 1 ; j <= K; j++)
{
rightSum += arr[(i + j) % N];
}
for ( int j = 1 ; j <= K; j++)
{
leftSum += arr[(i - j < 0 ? i - j + N : i - j) % N];
}
System.out.print( ((leftSum + rightSum + arr[i]) * 1.0 ) /
( 2 * K + 1 ) + " " );
}
}
public static void main(String args[])
{
int [] arr = { 1 , 2 , 3 , 4 , 5 , 6 , 7 , 8 , 9 };
int K = 3 ;
int N = arr.length;
average(arr, N, K);
}
}
|
Python3
def average(arr, N, K):
for i in range (N):
leftSum = 0
rightSum = 0
for j in range ( 1 , K + 1 ):
rightSum + = arr[(i + j) % N]
for j in range ( 1 , K + 1 ):
leftSum + = arr[((i - j + N) if (i - j < 0 ) else (i - j)) % N]
print ( round (((leftSum + rightSum + arr[i]) * 1.0 ) / ( 2 * K + 1 ), 5 ), end = " " )
arr = [ 1 , 2 , 3 , 4 , 5 , 6 , 7 , 8 , 9 ]
K = 3
N = len (arr)
average(arr, N, K)
|
C#
using System;
class GFG{
static void average( int [] arr, int N, int K)
{
for ( int i = 0; i < N; i++)
{
int leftSum = 0;
int rightSum = 0;
for ( int j = 1; j <= K; j++)
{
rightSum += arr[(i + j) % N];
}
for ( int j = 1; j <= K; j++)
{
leftSum += arr[(i - j < 0 ? i - j + N : i - j) % N];
}
Console.Write( ((leftSum + rightSum + arr[i]) * 1.0) /
(2 * K + 1) + " " );
}
}
public static void Main()
{
int [] arr = { 1, 2, 3, 4, 5, 6, 7, 8, 9 };
int K = 3;
int N = arr.Length;
average(arr, N, K);
}
}
|
Javascript
<script>
function average(arr, N, K)
{
for (let i = 0; i < N; i++)
{
let leftSum = 0;
let rightSum = 0;
for (let j = 1; j <= K; j++) {
rightSum += arr[(i + j) % N];
}
for (let j = 1; j <= K; j++) {
leftSum += arr[(i - j < 0
? i - j + N
: i - j)
% N];
}
document.write((((leftSum + rightSum + arr[i])
* 1.0)
/ (2 * K + 1)).toPrecision(6)
+ " " );
}
}
let arr = [1, 2, 3, 4, 5, 6, 7, 8, 9];
let K = 3;
let N = arr.length;
average(arr, N, K);
</script>
|
Output
4.85714 4.57143 4.28571 4 5 6 5.71429 5.42857 5.14286
Time Complexity: O(N*K), as we are using nested loops to traverse N*K times.
Auxiliary Space: O(1), as we are not using any extra space.
Efficient Approach: To reduce the time complexity the concept of prefix sum can be used where prefix sum array(say preSum[]) is calculated like preSum[i] = arr[0] + . . . + arr[i]. And using the preSum[] array the mean can be calculated easily for each elements without traversing (2K + 1) elements in each iterations. Condition for calculating the sum of previous K elements (leftSum) and K next elements (rightSum) for each of the element is given below:
Calculation for sum of K-elements in right:
- If there are K-elements present in right:
rightSum = preSum[i + k] – preSum[i];
- If no elements are in right:
rightSum = preSum[k – 1];
- If some elements are in right and some needs circular traversal:
eleInRight = n – i – 1;
rightSum = presum[n – 1] – presum[i] + presum[k – eleInRight – 1];
Calculation for sum of K-elements in left:
- If there are more than K-elements present in left:
leftSum = preSum[i – 1] – preSum[i – k – 1];
- If only k-elements are present in left:
leftSum = preSum[i – 1];
- if no elements are in left:
leftSum = preSum[n – 1] – preSum[n – 1 – k];
- If some elements are in left and some needs circular traversal:
eleInLeft = i
leftSum = preSum[i – 1] + preSum[n – 1] – preSum[n – 1 – (k – eleInLeft)];
Follow the steps mentioned below to implement it:
- Iterate the array create the prefix sum array.
- For each element get the left and right sum as show in the observation and calculate the average.
Below is the implementation of the above approach:
C++
#include <bits/stdc++.h>
using namespace std;
void average( int arr[], int N, int K)
{
int presum[N];
presum[0] = arr[0];
for ( int i = 1; i < N; i++) {
presum[i] = presum[i - 1] + arr[i];
}
for ( int i = 0; i < N; i++) {
int rightSum = 0;
if (i + K < N)
rightSum = presum[i + K]
- presum[i];
else {
int eleInRight = N - i - 1;
if (eleInRight > 0) {
rightSum = presum[N - 1]
- presum[i]
+ presum[K - eleInRight - 1];
}
else {
rightSum = presum[K - eleInRight - 1];
}
}
int leftSum = 0;
if (i - K > 0)
leftSum = presum[i - 1]
- presum[i - K - 1];
else if (i - K == 0) {
leftSum = presum[i - 1];
}
else {
int eleInLeft = i;
if (eleInLeft > 0) {
leftSum = presum[i - 1]
+ presum[N - 1]
- presum[N - 1 - (K - eleInLeft)];
}
else {
leftSum = presum[N - 1]
- presum[N - 1 - K];
}
}
cout << ((arr[i] + leftSum + rightSum)
* 1.0)
/ (2 * K + 1)
<< " " ;
}
}
int main()
{
int arr[] = { 1, 2, 3, 4, 5, 6, 7, 8, 9 };
int K = 3;
int N = sizeof (arr) / sizeof ( int );
average(arr, N, K);
return 0;
}
|
Java
import java.util.*;
public class GFG
{
static void average( int arr[], int N, int K)
{
int presum[] = new int [N];
presum[ 0 ] = arr[ 0 ];
for ( int i = 1 ; i < N; i++) {
presum[i] = presum[i - 1 ] + arr[i];
}
for ( int i = 0 ; i < N; i++) {
int rightSum = 0 ;
if (i + K < N)
rightSum = presum[i + K]
- presum[i];
else {
int eleInRight = N - i - 1 ;
if (eleInRight > 0 ) {
rightSum = presum[N - 1 ]
- presum[i]
+ presum[K - eleInRight - 1 ];
}
else {
rightSum = presum[K - eleInRight - 1 ];
}
}
int leftSum = 0 ;
if (i - K > 0 )
leftSum = presum[i - 1 ]
- presum[i - K - 1 ];
else if (i - K == 0 ) {
leftSum = presum[i - 1 ];
}
else {
int eleInLeft = i;
if (eleInLeft > 0 ) {
leftSum = presum[i - 1 ]
+ presum[N - 1 ]
- presum[N - 1 - (K - eleInLeft)];
}
else {
leftSum = presum[N - 1 ]
- presum[N - 1 - K];
}
}
System.out.print(((arr[i] + leftSum + rightSum)
* 1.0 )
/ ( 2 * K + 1 )
+ " " );
}
}
public static void main(String args[])
{
int [] arr = { 1 , 2 , 3 , 4 , 5 , 6 , 7 , 8 , 9 };
int K = 3 ;
int N = arr.length;
average(arr, N, K);
}
}
|
Python3
def average(arr, N, K):
presum = [ 0 ] * N
presum[ 0 ] = arr[ 0 ]
for i in range ( 1 ,N):
presum[i] = presum[i - 1 ] + arr[i]
for i in range ( 0 ,N):
rightSum = 0
if (i + K < N):
rightSum = presum[i + K] - presum[i]
else :
eleInRight = N - i - 1
if (eleInRight > 0 ):
rightSum = presum[N - 1 ] - presum[i] + presum[K - eleInRight - 1 ]
else :
rightSum = presum[K - eleInRight - 1 ]
leftSum = 0
if (i - K > 0 ):
leftSum = presum[i - 1 ] - presum[i - K - 1 ]
elif (i - K = = 0 ):
leftSum = presum[i - 1 ]
else :
eleInLeft = i
if (eleInLeft > 0 ):
leftSum = presum[i - 1 ] + presum[N - 1 ] - presum[N - 1 - (K - eleInLeft)]
else :
leftSum = presum[N - 1 ] - presum[N - 1 - K]
print ( "{:.5f}" . format (((arr[i] + leftSum + rightSum) * 1.0 ) / ( 2 * K + 1 )),end = " " )
arr = [ 1 , 2 , 3 , 4 , 5 , 6 , 7 , 8 , 9 ]
K = 3
N = len (arr)
average(arr, N, K)
|
C#
using System;
public class GFG{
static void average( int [] arr, int N, int K)
{
int [] presum = new int [N];
presum[0] = arr[0];
for ( int i = 1; i < N; i++) {
presum[i] = presum[i - 1] + arr[i];
}
for ( int i = 0; i < N; i++) {
int rightSum = 0;
if (i + K < N)
rightSum = presum[i + K]
- presum[i];
else {
int eleInRight = N - i - 1;
if (eleInRight > 0) {
rightSum = presum[N - 1]
- presum[i]
+ presum[K - eleInRight - 1];
}
else {
rightSum = presum[K - eleInRight - 1];
}
}
int leftSum = 0;
if (i - K > 0)
leftSum = presum[i - 1]
- presum[i - K - 1];
else if (i - K == 0) {
leftSum = presum[i - 1];
}
else {
int eleInLeft = i;
if (eleInLeft > 0) {
leftSum = presum[i - 1]
+ presum[N - 1]
- presum[N - 1 - (K - eleInLeft)];
}
else {
leftSum = presum[N - 1]
- presum[N - 1 - K];
}
}
Console.Write(((arr[i] + leftSum + rightSum)
* 1.0) / (2 * K + 1) + " " );
}
}
public static void Main()
{
int [] arr = { 1, 2, 3, 4, 5, 6, 7, 8, 9 };
int K = 3;
int N = arr.Length;
average(arr, N, K);
}
}
|
Javascript
<script>
const average = (arr, N, K) => {
let presum = new Array(N).fill(0);
presum[0] = arr[0];
for (let i = 1; i < N; i++) {
presum[i] = presum[i - 1] + arr[i];
}
for (let i = 0; i < N; i++) {
let rightSum = 0;
if (i + K < N)
rightSum = presum[i + K]
- presum[i];
else {
let eleInRight = N - i - 1;
if (eleInRight > 0) {
rightSum = presum[N - 1]
- presum[i]
+ presum[K - eleInRight - 1];
}
else {
rightSum = presum[K - eleInRight - 1];
}
}
let leftSum = 0;
if (i - K > 0)
leftSum = presum[i - 1]
- presum[i - K - 1];
else if (i - K == 0) {
leftSum = presum[i - 1];
}
else {
let eleInLeft = i;
if (eleInLeft > 0) {
leftSum = presum[i - 1]
+ presum[N - 1]
- presum[N - 1 - (K - eleInLeft)];
}
else {
leftSum = presum[N - 1]
- presum[N - 1 - K];
}
}
document.write(`${((arr[i] + leftSum + rightSum)
* 1.0)
/ (2 * K + 1)} `);
}
}
let arr = [1, 2, 3, 4, 5, 6, 7, 8, 9];
let K = 3;
let N = arr.length;
average(arr, N, K);
</script>
|
Output
4.85714 4.57143 4.28571 4 5 6 5.71429 5.42857 5.14286
Time Complexity: O(N), as we are using a loop to traverse N times.
Auxiliary Space: O(N), as we are using extra space for presum.
Share your thoughts in the comments
Please Login to comment...