×

DS - Basics

DS - Array

DS - Linked List

DS - Stack

DS - Queue

DS Hashing

DS Tree

DS - Graph

DS Programs Using C/C++

DS - Miscellaneous Topics

Replace every array element by multiplication of previous and next

C++ implementation to replace every array element by multiplication of previous and next using multiple approaches.
Submitted by Vikneshwar GK, on March 20, 2022

Consider an integer array, of size n. The task at hand is to replace every element with the product of its previous and next element, i.e. array[i] = array[i-1]*array[i+1].

However, if i=0, then array[i] = array[i]*array[i+1]

If i=n-1, then array[i] = array[i]*array[i-1]

Example:

Input:
array[]= {7, 3, 4, 1, 9, 5, 6}

Output:
array[] = {3, 7, 1, 9, 4, 6, 5} 

Input:
array[] = {4, 3, 6, 1, 8, 7, 2, 5}

Output:
array[] = {3, 6, 1, 8, 4, 7, 2, 5}

Solution 1: Using Temporary Array

This approach uses an auxiliary array to replace the elements. It involves the following steps:

  • Declare a temporary array of size n, where n is the length of the original array.
  • Iterate the original array.
  • If i=0, then perform array[i] = array[i]*array[i+1].
  • If i=n-1, then perform array[i] = array[i]*array[i-1].
  • Else, perform array[i] = array[i-1]*array[i+1].
  • Print the array.

C++ Implementation:

#include <iostream>

using namespace std;

// Function to print the array
void printArray(int array[], int length)
{
    for (int i = 0; i < length; i++) {
        cout << array[i] << " ";
    }
}

// Fucntion to replace the array elements
void replaceArray(int array[], int length)
{
    int temp[length];
    for (int i = 0; i < length; i++) {
        if (i == 0) {
            temp[i] = array[i] * array[i + 1];
        }
        else if (i == length - 1) {
            temp[i] = array[i] * array[i - 1];
        }
        else {
            temp[i] = array[i - 1] * array[i + 1];
        }
    }
    printArray(temp, length);
}

// Main Function
int main()
{
    int array[100];
    int N;
    
    cout << "Enter Number of elements: ";
    cin >> N;

    for (int i = 0; i < N; i++) {
        cout << "Enter element " << i + 1 << ": ";
        cin >> array[i];
    }

    replaceArray(array, N);

    return 0;
}

Output:

Enter Number of elements: 5
Enter element 1: 1
Enter element 2: 2
Enter element 3: 3
Enter element 4: 4
Enter element 5: 5
2 3 8 15 20

Time Complexity: O(n), where n is the length of the array.

Solution 2: Using Temporary Variables

This solution involves using two temporary variables to hold the previous and next elements to perform multiplication. It involves the following steps:

  • Initialize prev = array[0]
  • Set array[0]=array[0]*array[1]
  • Iterate through the array from 1 to n-2, where n is the length of the array
  • Set curr = array[i]
  • Assign array[i] = prev*array[i+1]
  • Set prev = curr
  • After the iteration, assign array[n-1] = array[n-1]*prev
  • Print the array

C++ Implementation:

#include <iostream>

using namespace std;

// Function to print the array
void printArray(int array[], int length)
{
    for (int i = 0; i < length; i++) {
        cout << array[i] << " ";
    }
}

// Fucntion to replace the array elements
void replaceArray(int array[], int length)
{
    // array contains 1 or no elements
    if (length <= 1)
        return;

    // initialize prev
    int prev = array[0];
    array[0] = array[0] * array[1];

    // Iterate through the array
    for (int i = 1; i < length - 1; i++) {
        // store the current array element
        int curr = array[i];

        // perform multiplication
        array[i] = prev * array[i + 1];

        // Update previous value
        prev = curr;
    }

    // Update last array element
    array[length - 1] = prev * array[length - 1];
}

// Main Function
int main()
{
    int array[100];
    int N;
    
    cout << "Enter Number of elements: ";
    cin >> N;

    for (int i = 0; i < N; i++) {
        cout << "Enter element " << i + 1 << ": ";
        cin >> array[i];
    }

    replaceArray(array, N);
    printArray(array, N);

    return 0;
}

Output:

Enter Number of elements: 7
Enter element 1: 1
Enter element 2: 2
Enter element 3: 3
Enter element 4: 4
Enter element 5: 5
Enter element 6: 6
Enter element 7: 7
2 3 8 15 24 35 42 

Time Complexity: O(n), where n is the length of the array.


Related Tutorials



Comments and Discussions!

Load comments ↻





Copyright © 2024 www.includehelp.com. All rights reserved.