Replace each element of array by multiplication of previous and next in C++

From the title of the problem, it is clear that for a given array, we have to Replace each element by multiplication of previous and next element. Means for each index number i, the arr[i] will be equal to multiplication of arr[i – 1] and arr[i + 1].

You may ask a question that what about the first and last element of the array as for the first element there is no previous element and for the last element, there is no next element. well, for them we can multiply that element with previous or next element depending on the availability. That is, for the first element arr[0], we can multiply the first and next element (arr[0] * arr[1]) and save it to arr[0]. and for the last element arr[n], we can multiply the last element and previous element (arr[n – 1] * arr[n]) save to arr[n].

Let’s take an example to understand the problem in a better way.

Example: 

input array : arr[] = {2, 5, 8, 6, 7, 3, 4}

Output array will be : arr[] = {2*5, 2*8, 5*6, 8*7, 6*3, 7*4, 3*4}

 Therefore, arr[] = {10, 16, 30, 56, 18, 28, 12}

Algorithm to replace each element of an array by multiplication of previous and next

  1. Declare and initialize an array.
  2. For the first element multiply arr[0] and arr[1] and save to arr[0].
  3. For the last element multiply arr[n – 1] and arr[n – 2] and save to arr[n – 1]
  4. declare a variable for the previous element. Consider previous.
  5. For rest elements, start iteration from i = 0 to size of the array. Save each arr[i] to a variable for the current reference. consider current.
  6. multiply the previous and arr[i + 1] and save to arr[i].
  7. update previous by current.
  8. end the iteration
  9. print the elements of  modified array.

C++ program to replace each element of an array by multiplication of previous and next

#include<iostream> 
using namespace std; 
  
void update(int arr[], int n) 
{  
  
    //save current value of array i.e arr[0] at initial
    int previous = arr[0]; 

    //updating the corrent value of array
    arr[0] = arr[0] * arr[1]; 
  
    //updating remaining of the array element befor the last element 
    for (int i = 1; i < n - 1; i++){

        int current = arr[i]; //saving the current element for next iteretion

        //multiply previous and next element and save to current element
        arr[i] = previous * arr[i + 1]; 
  
        previous = current; //updating previous value for next iteration
    } 
  
    //for last element 
    arr[n-1] = previous * arr[n-1]; 
} 
  
int main() 
{ 
    int arr[50], n;

    cout<<"Enter the size of array: ";
    cin>>n;

    if(n <= 1){
        cout<<"Undefined size for this problem!";
        exit(0);
    }
    
    cout<<"Input in the array: ";
    for(int i = 0; i < n; i++)
        cin>>arr[i];

    update(arr, n);

    //printing the modified array 
    cout<<"Modified array: ";
    for (int i=0; i<n; i++) 
      cout << arr[i] << " "; 

    return 0; 
}

Output: 

Enter the size of array: 7
Input in the array: 2 5 8 6 7 3 4
Modified array: 10 16 30 56 18 28 12

Time Complexity: O(n), where n is a number of elements in the array.

 

you may also read: 

  1. How to Reverse the Elements of an Array in C++
  2. Maximum subarray sum of a given array using c++

Leave a Reply