Find resultant Array after applying Convolution on given array using given mask
Given two arrays arr[] containing N integers and a mask[] of an odd size. The task is to replace every array element with the value computed by performing the same convolution on the array.
Examples:
Input: arr[] = {9, 7, 3, 9, 1, 8, 11}, mask[]={1, 2, -1}
Output: 11 20 4 20 3 6 30
Explanation: Following are the operations performed in the given array arr[]
For index 0: 0*1 + 9*2 + 7*-1 = 11
For index 1: 9*1 + 7*2 + 3*-1 = 20
For index 2: 7*1 + 3*2 + 9*-1 = 4
For index 3: 3*1 + 9*2 + 1*-1 = 20
For index 4: 9*1 + 1*2 + 8*-1 = 3
For index 5: 1*1 + 8*2 + 11*-1 = 6
For index 6: 8*1 + 11*2 + 0*-1 = 30
Input: arr[] = {13, 26, 35, 41, 23, 18, 38}, mask[]={-1, 3, 5, -3, 1}
Output: 240 117 140 233 187 4 221
Approach: The simplest approach to solve this problem is to use nested loops. The outer loop will traverse the array from left to right, i.e. from i = 0 to i < N, and an inner loop will traverse the mask from index i – K/2 to the index i + K/2 and calculate the convolution of them. Finally, print the output.
Below is the implementation of the above approach:
C++
#include <bits/stdc++.h>
using namespace std;
void ComputeSameConvolution( int arr[],
int mask[], int N,
int K)
{
int i, j, k, sum;
for (i = 0; i < N; i++) {
sum = 0;
k = 0;
for (j = i - (K / 2); j <= i + (K / 2);
j++) {
if (j < 0 || j >= N)
k++;
else
sum += arr[j] * mask[k++];
}
cout << sum << ' ' ;
}
}
int main()
{
int arr[] = { 9, 7, 3, 9, 1, 8, 11 };
int mask[] = { 1, 2, -1 };
int K = sizeof (mask) / sizeof (mask[0]);
int N = sizeof (arr) / sizeof (arr[0]);
ComputeSameConvolution(arr, mask, N, K);
return 0;
}
|
Java
class GFG {
static void ComputeSameConvolution( int [] arr,
int [] mask, int N,
int K)
{
int i, j, k, sum;
for (i = 0 ; i < N; i++) {
sum = 0 ;
k = 0 ;
for (j = i - (K / 2 ); j <= i + (K / 2 ); j++) {
if (j < 0 || j >= N)
k++;
else
sum += arr[j] * mask[k++];
}
System.out.print(sum + " " );
}
}
public static void main(String[] args)
{
int [] arr = { 9 , 7 , 3 , 9 , 1 , 8 , 11 };
int [] mask = { 1 , 2 , - 1 };
int K = mask.length;
int N = arr.length;
ComputeSameConvolution(arr, mask, N, K);
}
}
|
Python3
def ComputeSameConvolution(arr, mask, N, K):
i = None
j = None
k = None
sum = None
for i in range (N):
sum = 0 ;
k = 0 ;
for j in range (i - (K / / 2 ), i + (K / / 2 ) + 1 ):
if (j < 0 or j > = N):
k + = 1
else :
sum + = arr[j] * mask[k];
k + = 1
print ( sum , end = ' ' );
arr = [ 9 , 7 , 3 , 9 , 1 , 8 , 11 ];
mask = [ 1 , 2 , - 1 ];
K = len (mask)
N = len (arr)
ComputeSameConvolution(arr, mask, N, K);
|
C#
using System;
class GFG
{
static void ComputeSameConvolution( int []arr,
int []mask, int N,
int K)
{
int i, j, k, sum;
for (i = 0; i < N; i++) {
sum = 0;
k = 0;
for (j = i - (K / 2); j <= i + (K / 2);
j++) {
if (j < 0 || j >= N)
k++;
else
sum += arr[j] * mask[k++];
}
Console.Write(sum + " " );
}
}
public static void Main()
{
int []arr = { 9, 7, 3, 9, 1, 8, 11 };
int []mask = { 1, 2, -1 };
int K = mask.Length;
int N = arr.Length;
ComputeSameConvolution(arr, mask, N, K);
}
}
|
Javascript
<script>
function ComputeSameConvolution(arr,
mask, N, K)
{
let i, j, k, sum;
for (i = 0; i < N; i++) {
sum = 0;
k = 0;
for (j = i - Math.floor(K / 2); j <= i + Math.floor(K / 2);
j++) {
if (j < 0 || j >= N)
k++;
else
sum += arr[j] * mask[k++];
}
document.write(sum + ' ' );
}
}
let arr = [9, 7, 3, 9, 1, 8, 11];
let mask = [1, 2, -1];
let K = mask.length;
let N = arr.length;
ComputeSameConvolution(arr, mask, N, K);
</script>
|
Time Complexity: O(N*K)
Auxiliary Space: O(1)
Last Updated :
21 Jan, 2022
Like Article
Save Article
Share your thoughts in the comments
Please Login to comment...