Find resultant Array after applying Convolution on given array using given mask Last Updated : 21 Jan, 2022 Comments Improve Suggest changes Like Article Like Report 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 = 11For index 1: 9*1 + 7*2 + 3*-1 = 20For index 2: 7*1 + 3*2 + 9*-1 = 4For index 3: 3*1 + 9*2 + 1*-1 = 20For index 4: 9*1 + 1*2 + 8*-1 = 3For index 5: 1*1 + 8*2 + 11*-1 = 6For 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++ // C++ program for the above approach #include <bits/stdc++.h> using namespace std; // Function to perform same convolution void ComputeSameConvolution(int arr[], int mask[], int N, int K) { int i, j, k, sum; // Nested loops 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++]; } // Print the required sum cout << sum << ' '; } } // Driver Code 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]); // Function Call ComputeSameConvolution(arr, mask, N, K); return 0; } Java // Java program for the above approach class GFG { // Function to perform same convolution static void ComputeSameConvolution(int[] arr, int[] mask, int N, int K) { int i, j, k, sum; // Nested loops 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++]; } // Print the required sum System.out.print(sum + " "); } } // Driver Code 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; // Function Call ComputeSameConvolution(arr, mask, N, K); } } // This code is contributed by ukasp. Python3 # Python code for the above approach # Function to perform same convolution def ComputeSameConvolution(arr, mask, N, K): i = None j = None k = None sum = None # Nested loops 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 the required sum print(sum, end=' '); # Driver Code arr = [9, 7, 3, 9, 1, 8, 11]; mask = [1, 2, -1]; K = len(mask) N = len(arr) # Function Call ComputeSameConvolution(arr, mask, N, K); # This code is contributed by gfgking C# // C# program for the above approach using System; class GFG { // Function to perform same convolution static void ComputeSameConvolution(int []arr, int []mask, int N, int K) { int i, j, k, sum; // Nested loops 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++]; } // Print the required sum Console.Write(sum + " "); } } // Driver Code 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; // Function Call ComputeSameConvolution(arr, mask, N, K); } } // This code is contributed by Samim Hossain Mondal. JavaScript <script> // JavaScript code for the above approach // Function to perform same convolution function ComputeSameConvolution(arr, mask, N, K) { let i, j, k, sum; // Nested loops 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++]; } // Print the required sum document.write(sum + ' '); } } // Driver Code let arr = [9, 7, 3, 9, 1, 8, 11]; let mask = [1, 2, -1]; let K = mask.length; let N = arr.length; // Function Call ComputeSameConvolution(arr, mask, N, K); // This code is contributed by Potta Lokesh </script> Output11 20 4 20 3 6 30 Time Complexity: O(N*K)Auxiliary Space: O(1) Comment More infoAdvertise with us Next Article Find resultant Array after applying Convolution on given array using given mask pintusaini Follow Improve Article Tags : Mathematical Algo Geek DSA Arrays Algo-Geek 2021 +1 More Practice Tags : ArraysMathematical Similar Reads Program to generate an array having convolution of two given arrays Given two arrays A[] and B[] consisting of N and M integers respectively, the task is to construct a convolution array C[] of size (N + M - 1). The convolution of 2 arrays is defined as C[i + j] = ?(a[i] * b[j]) for every i and j. Note: If the value for any index becomes very large, then print it to 15+ min read Emulating a 2-d array using 1-d array How to convert a 2-d array of size (m x n) into 1-d array and how to store the element at position [i, j] of 2-d array in 1-d array? Clearly, the size of 1-d array is the number of elements in 2-d array i.e. m x n). If the elements in the 2-d array are stored in row-major order. Then, the element at 5 min read Calculating the address of an element in an N-dimensional array N-Dimensional Arrays: The N-Dimensional array is basically an array of arrays. As 1-D arrays are identified as a single index, 2-D arrays are identified using two indices, similarly, N-Dimensional arrays are identified using N indices. A multi-dimensional array is declared as follows: int NDA[S1][S2 4 min read Minimum number that can be obtained by applying '+' and '*' operations on array elements Given an array arr[] consisting of N positive integers and a string S of length (N - 1), containing characters '+' and '*', the task is to find the smallest number that can be obtained after applying the arithmetic operations mentioned in the string S on the array elements in any order. Examples: In 11 min read Find the Prefix-MEX Array for given Array Given an array A[] of N elements, the task is to create a Prefix-MEX array for this given array. Prefix-MEX array B[] of an array A[] is created such that MEX of A[0] till A[i] is B[i]. MEX of an array refers to the smallest missing non-negative integer of the array. Examples: Input: A[] = {1, 0, 2, 13 min read Find last remaining Array element by multiplying boundary elements based on given rules Given an array arr[], the task is to find the only remaining element in the array after applying the below operation till there is only one element left in the array. In an operation, multiply the boundary elements of this array and if the size of the array is: Even: Insert the product in the middle 5 min read Find all subarray index ranges in given Array with set bit sum equal to X Given an array arr (1-based indexing) of length N and an integer X, the task is to find and print all index ranges having a set bit sum equal to X in the array. Examples: Input: A[] = {1 4 3 5 7}, X = 4Output: (1, 3), (3, 4)Explanation: In the above array subarray having set bit sum equal to X (= 4) 15 min read Find all good indices in the given Array Given an array A[] of integers. The task is to print all indices i of this array such that after removing the ith element from the array, the array becomes a good array. Note: An array is good if there is an element in the array that equals to the sum of all other elements.1-based indexing is consid 6 min read Convert given upper triangular Matrix to 1D Array Given an upper triangular matrix M[][] of dimensions N * N, the task is to convert it into an one-dimensional array storing only non-zero elements from the matrix. Examples: Input: M[][] = {{1, 2, 3, 4}, {0, 5, 6, 7}, {0, 0, 8, 9}, {0, 0, 0, 10}}Output: Row-wise: {1, 2, 3, 4, 5, 6, 7, 8, 9, 10} Colu 12 min read Find all indices of Array having value same as average of other elements Given an array arr[] of N integers, the task is to find all indices in the array, such that for each index i the arithmetic mean of all elements except arr[i] is equal to the value of the element at that index. Examples : Input: N = 5, arr[] = {1, 2, 3, 4, 5}Output : {2}Explanation : Upon removing a 6 min read Like