Posted on by Kalkicode
Code Array

# Replace array element by multiplication of previous and next

The problem of replacing array elements with the product of their previous and next neighbors is a common coding challenge. This task involves modifying an array in place, where each element is replaced by the product of its adjacent elements. In this article, we will delve into this problem, provide a comprehensive explanation, present a step-by-step solution, and analyze the time complexity of the algorithm.

## Problem Statement

Given an array of integers, we need to replace each element with the product of its previous and next neighbors. The first and last elements should be replaced by the product of their adjacent elements.

## Description

Imagine you have an array of numbers, and you want to transform it by replacing each element with the product of its neighbors. For the first and last elements, there is only one neighbor, so they should be replaced by the product of that single neighbor.

Let's illustrate this problem with a few examples:

## Example 1

``````
Original Array   : [3,  2, 5, 3,  6,  1, -2,  3]

[3x2]
ↆ
[6, 2, 5, 3, 6, 1, -2, 3]

[3x5]
ↆ
[6,10, 5, 3, 6, 1, -2, 3]

[2x3]
ↆ
[6,10, 6, 3, 6, 1, -2, 3]

[5x6]
ↆ
[6,10, 6, 30, 6, 1, -2, 3]

[3x1]
ↆ
[6,10, 6, 30, 3, 1, -2, 3]

[6x-2]
ↆ
[6,10, 6, 30, 3,-12, -2, 3]

[1x3]
ↆ
[6,10, 6, 30, 3,-12, 3, 3]

[-2x3]
ↆ
[6,10, 6, 30, 3,-12, 3, -6]

Transformed Array: [6, 15, 6, 30, 3, -12, 3, -6]

``````

In this example, each element in the transformed array is the product of its neighbors in the original array. For instance, the first element is 3, which is replaced by the product of its neighbors 2 and 5, resulting in 6.

## Example 2

``````Original Array: [1, 4, 6, 2]
Transformed Array: [4, 6, 8, 12]``````

Here, we replace each element with the product of its neighbors. For example, the second element, 4, is replaced by the product of 1 and 6, which is 4.

## Idea to Solve the Problem

To solve this problem, we can iterate through the array while keeping track of the previous element. At each step, we calculate the product of the previous and next elements and update the current element accordingly. To handle the first and last elements, we set them to the product of their adjacent elements before entering the loop.

## Pseudocode

``````productNextAndPrevious(arr, n):
if n <= 1:
return

// Handle the first element
arr[0] = arr[0] * arr[1]

for i from 1 to n-2:
// Calculate the product of previous and next elements
arr[i] = arr[i-1] * arr[i+1]

// Handle the last element
arr[n-1] = arr[n-1] * arr[n-2]``````

## Algorithm Explanation

1. Handle the first element by setting it to the product of the first and second elements.

2. Iterate through the array from the second element to the second-to-last element.

3. At each step, calculate the product of the previous and next elements and update the current element.

4. Handle the last element by setting it to the product of the last and second-to-last elements.

## Code Solution

``````/*
C program for
Replace array element by multiplication of previous and next
*/
#include <stdio.h>

// Display array elements
void printArray(int arr[], int n)
{
for (int i = 0; i < n; ++i)
{
printf("  %d", arr[i]);
}
}
void productNextAndPrevious(int arr[], int n)
{
if (n <= 1)
{
return;
}
// Auxiliary variable back and auxiliary
int back = arr[0];
int auxiliary = 0;
// First element is product of start two elements
arr[0] = arr[1] *back;
for (int i = 1; i < n - 1; ++i)
{
// Get actual value of previous element
auxiliary = back;
// Get current element
back = arr[i];
// Multiplication of next and previous element
arr[i] = arr[i + 1] *auxiliary;
}
// Last element is product of two last element
arr[n - 1] = arr[n - 1] *back;
}
int main(int argc, char
const *argv[])
{
int arr1[] = {
3 , 2 , 5 , 3 , 6 , 1 , -2 , 3
};
int arr2[] = {
1 , 4 , 6 , 2
};
int arr3[] = {
3 , 6 , 1 , 2 , 9 , 3
};
// Get the size of arrays
int l = sizeof(arr1) / sizeof(arr1[0]);
int m = sizeof(arr2) / sizeof(arr2[0]);
int n = sizeof(arr3) / sizeof(arr3[0]);
// Test A
printf("\n Before Replace Array arr1 \n");
printArray(arr1, l);
productNextAndPrevious(arr1, l);
printf("\n After Replace Array arr1 \n");
printArray(arr1, l);
// Test B
printf("\n Before Replace Array arr2 \n");
printArray(arr2, m);
productNextAndPrevious(arr2, m);
printf("\n After Replace Array arr2 \n");
printArray(arr2, m);
// Test C
printf("\n Before Replace Array arr3 \n");
printArray(arr3, n);
productNextAndPrevious(arr3, n);
printf("\n After Replace Array arr3 \n");
printArray(arr3, n);
return 0;
}``````

#### Output

`````` Before Replace Array arr1
3  2  5  3  6  1  -2  3
After Replace Array arr1
6  15  6  30  3  -12  3  -6
Before Replace Array arr2
1  4  6  2
After Replace Array arr2
4  6  8  12
Before Replace Array arr3
3  6  1  2  9  3
After Replace Array arr3
18  3  12  9  6  27``````
``````/*
Java program for
Find all common elements in given 3 sorted arrays
*/
public class Product
{
// Display array elements
public void printArray(int[] arr, int n)
{
for (int i = 0; i < n; ++i)
{
System.out.print(" " + arr[i]);
}
}
public void productNextAndPrevious(int[] arr, int n)
{
if (n <= 1)
{
return;
}
// Auxiliary variable back and auxiliary
int back = arr[0];
int auxiliary = 0;
// First element is product of start two elements
arr[0] = arr[1] * back;
for (int i = 1; i < n - 1; ++i)
{
// Get actual value of previous element
auxiliary = back;
// Get current element
back = arr[i];
// Multiplication of next and previous element
arr[i] = arr[i + 1] * auxiliary;
}
// Last element is product of two last element
arr[n - 1] = arr[n - 1] * back;
}
public static void main(String[] args)
{
Product task = new Product();
int[] arr1 = {
3 , 2 , 5 , 3 , 6 , 1 , -2 , 3
};
int[] arr2 = {
1 , 4 , 6 , 2
};
int[] arr3 = {
3 , 6 , 1 , 2 , 9 , 3
};
// Get the size of arrays
int l = arr1.length;
int m = arr2.length;
int n = arr3.length;
// Test A
System.out.print("\n Before Replace Array arr1 \n");
System.out.print("\n After Replace Array arr1 \n");
// Test B
System.out.print("\n Before Replace Array arr2 \n");
System.out.print("\n After Replace Array arr2 \n");
// Test C
System.out.print("\n Before Replace Array arr3 \n");
System.out.print("\n After Replace Array arr3 \n");
}
}``````

#### Output

`````` Before Replace Array arr1
3 2 5 3 6 1 -2 3
After Replace Array arr1
6 15 6 30 3 -12 3 -6
Before Replace Array arr2
1 4 6 2
After Replace Array arr2
4 6 8 12
Before Replace Array arr3
3 6 1 2 9 3
After Replace Array arr3
18 3 12 9 6 27``````
``````// Include header file
#include <iostream>
using namespace std;
/*
C++ program for
Find all common elements in given 3 sorted arrays
*/
class Product
{
public:
// Display array elements
void printArray(int arr[], int n)
{
for (int i = 0; i < n; ++i)
{
cout << " " << arr[i];
}
}
void productNextAndPrevious(int arr[], int n)
{
if (n <= 1)
{
return;
}
// Auxiliary variable back and auxiliary
int back = arr[0];
int auxiliary = 0;
// First element is product of start two elements
arr[0] = arr[1] * back;
for (int i = 1; i < n - 1; ++i)
{
// Get actual value of previous element
auxiliary = back;
// Get current element
back = arr[i];
// Multiplication of next and previous element
arr[i] = arr[i + 1] * auxiliary;
}
// Last element is product of two last element
arr[n - 1] = arr[n - 1] * back;
}
};
int main()
{
Product *task = new Product();
int arr1[] = {
3 , 2 , 5 , 3 , 6 , 1 , -2 , 3
};
int arr2[] = {
1 , 4 , 6 , 2
};
int arr3[] = {
3 , 6 , 1 , 2 , 9 , 3
};
// Get the size of arrays
int l = sizeof(arr1) / sizeof(arr1[0]);
int m = sizeof(arr2) / sizeof(arr2[0]);
int n = sizeof(arr3) / sizeof(arr3[0]);
// Test A
cout << "\n Before Replace Array arr1 \n";
cout << "\n After Replace Array arr1 \n";
// Test B
cout << "\n Before Replace Array arr2 \n";
cout << "\n After Replace Array arr2 \n";
// Test C
cout << "\n Before Replace Array arr3 \n";
cout << "\n After Replace Array arr3 \n";
return 0;
}``````

#### Output

`````` Before Replace Array arr1
3 2 5 3 6 1 -2 3
After Replace Array arr1
6 15 6 30 3 -12 3 -6
Before Replace Array arr2
1 4 6 2
After Replace Array arr2
4 6 8 12
Before Replace Array arr3
3 6 1 2 9 3
After Replace Array arr3
18 3 12 9 6 27``````
``````// Include namespace system
using System;
/*
Csharp program for
Find all common elements in given 3 sorted arrays
*/
public class Product
{
// Display array elements
public void printArray(int[] arr, int n)
{
for (int i = 0; i < n; ++i)
{
Console.Write(" " + arr[i]);
}
}
public void productNextAndPrevious(int[] arr, int n)
{
if (n <= 1)
{
return;
}
// Auxiliary variable back and auxiliary
int back = arr[0];
int auxiliary = 0;
// First element is product of start two elements
arr[0] = arr[1] * back;
for (int i = 1; i < n - 1; ++i)
{
// Get actual value of previous element
auxiliary = back;
// Get current element
back = arr[i];
// Multiplication of next and previous element
arr[i] = arr[i + 1] * auxiliary;
}
// Last element is product of two last element
arr[n - 1] = arr[n - 1] * back;
}
public static void Main(String[] args)
{
Product task = new Product();
int[] arr1 = {
3 , 2 , 5 , 3 , 6 , 1 , -2 , 3
};
int[] arr2 = {
1 , 4 , 6 , 2
};
int[] arr3 = {
3 , 6 , 1 , 2 , 9 , 3
};
// Get the size of arrays
int l = arr1.Length;
int m = arr2.Length;
int n = arr3.Length;
// Test A
Console.Write("\n Before Replace Array arr1 \n");
Console.Write("\n After Replace Array arr1 \n");
// Test B
Console.Write("\n Before Replace Array arr2 \n");
Console.Write("\n After Replace Array arr2 \n");
// Test C
Console.Write("\n Before Replace Array arr3 \n");
Console.Write("\n After Replace Array arr3 \n");
}
}``````

#### Output

`````` Before Replace Array arr1
3 2 5 3 6 1 -2 3
After Replace Array arr1
6 15 6 30 3 -12 3 -6
Before Replace Array arr2
1 4 6 2
After Replace Array arr2
4 6 8 12
Before Replace Array arr3
3 6 1 2 9 3
After Replace Array arr3
18 3 12 9 6 27``````
``````package main
import "fmt"
/*
Go program for
Find all common elements in given 3 sorted arrays
*/

// Display array elements
func printArray(arr[] int, n int) {
for i := 0 ; i < n ; i++ {
fmt.Print(" ", arr[i])
}
}
func productNextAndPrevious(arr[] int, n int) {
if n <= 1 {
return
}
// Auxiliary variable back and auxiliary
var back int = arr[0]
var auxiliary int = 0
// First element is product of start two elements
arr[0] = arr[1] * back
for i := 1 ; i < n - 1 ; i++ {
// Get actual value of previous element
auxiliary = back
// Get current element
back = arr[i]
// Multiplication of next and previous element
arr[i] = arr[i + 1] * auxiliary
}
// Last element is product of two last element
arr[n - 1] = arr[n - 1] * back
}
func main() {

var arr1 = [] int { 3 , 2 , 5 , 3 , 6 , 1 , -2 , 3}
var arr2 = [] int { 1 , 4 , 6 , 2}
var arr3 = [] int { 3 , 6 , 1 , 2 , 9 , 3}
// Get the size of arrays
var l int = len(arr1)
var m int = len(arr2)
var n int = len(arr3)
// Test A
fmt.Print("\n Before Replace Array arr1 \n")
printArray(arr1, l)
productNextAndPrevious(arr1, l)
fmt.Print("\n After Replace Array arr1 \n")
printArray(arr1, l)
// Test B
fmt.Print("\n Before Replace Array arr2 \n")
printArray(arr2, m)
productNextAndPrevious(arr2, m)
fmt.Print("\n After Replace Array arr2 \n")
printArray(arr2, m)
// Test C
fmt.Print("\n Before Replace Array arr3 \n")
printArray(arr3, n)
productNextAndPrevious(arr3, n)
fmt.Print("\n After Replace Array arr3 \n")
printArray(arr3, n)
}``````

#### Output

`````` Before Replace Array arr1
3 2 5 3 6 1 -2 3
After Replace Array arr1
6 15 6 30 3 -12 3 -6
Before Replace Array arr2
1 4 6 2
After Replace Array arr2
4 6 8 12
Before Replace Array arr3
3 6 1 2 9 3
After Replace Array arr3
18 3 12 9 6 27``````
``````<?php
/*
Php program for
Find all common elements in given 3 sorted arrays
*/
class Product
{
// Display array elements
public	function printArray(\$arr, \$n)
{
for (\$i = 0; \$i < \$n; ++\$i)
{
echo(" ".\$arr[\$i]);
}
}
public	function productNextAndPrevious(&\$arr, \$n)
{
if (\$n <= 1)
{
return;
}
// Auxiliary variable back and auxiliary
\$back = \$arr[0];
\$auxiliary = 0;
// First element is product of start two elements
\$arr[0] = \$arr[1] * \$back;
for (\$i = 1; \$i < \$n - 1; ++\$i)
{
// Get actual value of previous element
\$auxiliary = \$back;
// Get current element
\$back = \$arr[\$i];
// Multiplication of next and previous element
\$arr[\$i] = \$arr[\$i + 1] * \$auxiliary;
}
// Last element is product of two last element
\$arr[\$n - 1] = \$arr[\$n - 1] * \$back;
}
}

function main()
{
\$task = new Product();
\$arr1 = array(3, 2, 5, 3, 6, 1, -2, 3);
\$arr2 = array(1, 4, 6, 2);
\$arr3 = array(3, 6, 1, 2, 9, 3);
// Get the size of arrays
\$l = count(\$arr1);
\$m = count(\$arr2);
\$n = count(\$arr3);
// Test A
echo("\n Before Replace Array arr1 \n");
echo("\n After Replace Array arr1 \n");
// Test B
echo("\n Before Replace Array arr2 \n");
echo("\n After Replace Array arr2 \n");
// Test C
echo("\n Before Replace Array arr3 \n");
echo("\n After Replace Array arr3 \n");
}
main();``````

#### Output

`````` Before Replace Array arr1
3 2 5 3 6 1 -2 3
After Replace Array arr1
6 15 6 30 3 -12 3 -6
Before Replace Array arr2
1 4 6 2
After Replace Array arr2
4 6 8 12
Before Replace Array arr3
3 6 1 2 9 3
After Replace Array arr3
18 3 12 9 6 27``````
``````/*
Node JS program for
Find all common elements in given 3 sorted arrays
*/
class Product
{
// Display array elements
printArray(arr, n)
{
for (var i = 0; i < n; ++i)
{
process.stdout.write(" " + arr[i]);
}
}
productNextAndPrevious(arr, n)
{
if (n <= 1)
{
return;
}
// Auxiliary variable back and auxiliary
var back = arr[0];
var auxiliary = 0;
// First element is product of start two elements
arr[0] = arr[1] * back;
for (var i = 1; i < n - 1; ++i)
{
// Get actual value of previous element
auxiliary = back;
// Get current element
back = arr[i];
// Multiplication of next and previous element
arr[i] = arr[i + 1] * auxiliary;
}
// Last element is product of two last element
arr[n - 1] = arr[n - 1] * back;
}
}

function main()
{
var task = new Product();
var arr1 = [3, 2, 5, 3, 6, 1, -2, 3];
var arr2 = [1, 4, 6, 2];
var arr3 = [3, 6, 1, 2, 9, 3];
// Get the size of arrays
var l = arr1.length;
var m = arr2.length;
var n = arr3.length;
// Test A
process.stdout.write("\n Before Replace Array arr1 \n");
process.stdout.write("\n After Replace Array arr1 \n");
// Test B
process.stdout.write("\n Before Replace Array arr2 \n");
process.stdout.write("\n After Replace Array arr2 \n");
// Test C
process.stdout.write("\n Before Replace Array arr3 \n");
process.stdout.write("\n After Replace Array arr3 \n");
}
main();``````

#### Output

`````` Before Replace Array arr1
3 2 5 3 6 1 -2 3
After Replace Array arr1
6 15 6 30 3 -12 3 -6
Before Replace Array arr2
1 4 6 2
After Replace Array arr2
4 6 8 12
Before Replace Array arr3
3 6 1 2 9 3
After Replace Array arr3
18 3 12 9 6 27``````
``````#    Python 3 program for
#    Find all common elements in given 3 sorted arrays
class Product :
#  Display list elements
def printArray(self, arr, n) :
i = 0
while (i < n) :
print(" ", arr[i], end = "")
i += 1

def productNextAndPrevious(self, arr, n) :
if (n <= 1) :
return

#  Auxiliary variable back and auxiliary
back = arr[0]
auxiliary = 0
#  First element is product of start two elements
arr[0] = arr[1] * back
i = 1
while (i < n - 1) :
#  Get actual value of previous element
auxiliary = back
#  Get current element
back = arr[i]
#  Multiplication of next and previous element
arr[i] = arr[i + 1] * auxiliary
i += 1

#  Last element is product of two last element
arr[n - 1] = arr[n - 1] * back

def main() :
arr1 = [3, 2, 5, 3, 6, 1, -2, 3]
arr2 = [1, 4, 6, 2]
arr3 = [3, 6, 1, 2, 9, 3]
#  Get the size of lists
l = len(arr1)
m = len(arr2)
n = len(arr3)
#  Test A
print("\n Before Replace Array arr1 ")
print("\n After Replace Array arr1 ")
#  Test B
print("\n Before Replace Array arr2 ")
print("\n After Replace Array arr2 ")
#  Test C
print("\n Before Replace Array arr3 ")
print("\n After Replace Array arr3 ")

if __name__ == "__main__": main()``````

#### Output

`````` Before Replace Array arr1
3  2  5  3  6  1  -2  3
After Replace Array arr1
6  15  6  30  3  -12  3  -6
Before Replace Array arr2
1  4  6  2
After Replace Array arr2
4  6  8  12
Before Replace Array arr3
3  6  1  2  9  3
After Replace Array arr3
18  3  12  9  6  27``````
``````#    Ruby program for
#    Find all common elements in given 3 sorted arrays
class Product
#  Display array elements
def printArray(arr, n)
i = 0
while (i < n)
print(" ", arr[i])
i += 1
end

end

def productNextAndPrevious(arr, n)
if (n <= 1)
return
end

#  Auxiliary variable back and auxiliary
back = arr[0]
auxiliary = 0
#  First element is product of start two elements
arr[0] = arr[1] * back
i = 1
while (i < n - 1)
#  Get actual value of previous element
auxiliary = back
#  Get current element
back = arr[i]
#  Multiplication of next and previous element
arr[i] = arr[i + 1] * auxiliary
i += 1
end

#  Last element is product of two last element
arr[n - 1] = arr[n - 1] * back
end

end

def main()
arr1 = [3, 2, 5, 3, 6, 1, -2, 3]
arr2 = [1, 4, 6, 2]
arr3 = [3, 6, 1, 2, 9, 3]
#  Get the size of arrays
l = arr1.length
m = arr2.length
n = arr3.length
#  Test A
print("\n Before Replace Array arr1 \n")
print("\n After Replace Array arr1 \n")
#  Test B
print("\n Before Replace Array arr2 \n")
print("\n After Replace Array arr2 \n")
#  Test C
print("\n Before Replace Array arr3 \n")
print("\n After Replace Array arr3 \n")
end

main()``````

#### Output

`````` Before Replace Array arr1
3 2 5 3 6 1 -2 3
After Replace Array arr1
6 15 6 30 3 -12 3 -6
Before Replace Array arr2
1 4 6 2
After Replace Array arr2
4 6 8 12
Before Replace Array arr3
3 6 1 2 9 3
After Replace Array arr3
18 3 12 9 6 27``````
``````/*
Scala program for
Find all common elements in given 3 sorted arrays
*/
class Product()
{
// Display array elements
def printArray(arr: Array[Int], n: Int): Unit = {
var i: Int = 0;
while (i < n)
{
print(" " + arr(i));
i += 1;
}
}
def productNextAndPrevious(arr: Array[Int], n: Int): Unit = {
if (n <= 1)
{
return;
}
// Auxiliary variable back and auxiliary
var back: Int = arr(0);
var auxiliary: Int = 0;
// First element is product of start two elements
arr(0) = arr(1) * back;
var i: Int = 1;
while (i < n - 1)
{
// Get actual value of previous element
auxiliary = back;
// Get current element
back = arr(i);
// Multiplication of next and previous element
arr(i) = arr(i + 1) * auxiliary;
i += 1;
}
// Last element is product of two last element
arr(n - 1) = arr(n - 1) * back;
}
}
object Main
{
def main(args: Array[String]): Unit = {
var task: Product = new Product();
var arr1: Array[Int] = Array(3, 2, 5, 3, 6, 1, -2, 3);
var arr2: Array[Int] = Array(1, 4, 6, 2);
var arr3: Array[Int] = Array(3, 6, 1, 2, 9, 3);
// Get the size of arrays
var l: Int = arr1.length;
var m: Int = arr2.length;
var n: Int = arr3.length;
// Test A
print("\n Before Replace Array arr1 \n");
print("\n After Replace Array arr1 \n");
// Test B
print("\n Before Replace Array arr2 \n");
print("\n After Replace Array arr2 \n");
// Test C
print("\n Before Replace Array arr3 \n");
print("\n After Replace Array arr3 \n");
}
}``````

#### Output

`````` Before Replace Array arr1
3 2 5 3 6 1 -2 3
After Replace Array arr1
6 15 6 30 3 -12 3 -6
Before Replace Array arr2
1 4 6 2
After Replace Array arr2
4 6 8 12
Before Replace Array arr3
3 6 1 2 9 3
After Replace Array arr3
18 3 12 9 6 27``````
``````import Foundation;
/*
Swift 4 program for
Find all common elements in given 3 sorted arrays
*/
class Product
{
// Display array elements
func printArray(_ arr: [Int], _ n: Int)
{
var i: Int = 0;
while (i < n)
{
print(" ", arr[i], terminator: "");
i += 1;
}
}
func productNextAndPrevious(_ arr: inout[Int], _ n: Int)
{
if (n <= 1)
{
return;
}
// Auxiliary variable back and auxiliary
var back: Int = arr[0];
var auxiliary: Int = 0;
// First element is product of start two elements
arr[0] = arr[1] * back;
var i: Int = 1;
while (i < n - 1)
{
// Get actual value of previous element
auxiliary = back;
// Get current element
back = arr[i];
// Multiplication of next and previous element
arr[i] = arr[i + 1] * auxiliary;
i += 1;
}
// Last element is product of two last element
arr[n - 1] = arr[n - 1] * back;
}
}
func main()
{
let task: Product = Product();
var arr1: [Int] = [3, 2, 5, 3, 6, 1, -2, 3];
var arr2: [Int] = [1, 4, 6, 2];
var arr3: [Int] = [3, 6, 1, 2, 9, 3];
// Get the size of arrays
let l: Int = arr1.count;
let m: Int = arr2.count;
let n: Int = arr3.count;
// Test A
print("\n Before Replace Array arr1 ");
print("\n After Replace Array arr1 ");
// Test B
print("\n Before Replace Array arr2 ");
print("\n After Replace Array arr2 ");
// Test C
print("\n Before Replace Array arr3 ");
print("\n After Replace Array arr3 ");
}
main();``````

#### Output

`````` Before Replace Array arr1
3  2  5  3  6  1  -2  3
After Replace Array arr1
6  15  6  30  3  -12  3  -6
Before Replace Array arr2
1  4  6  2
After Replace Array arr2
4  6  8  12
Before Replace Array arr3
3  6  1  2  9  3
After Replace Array arr3
18  3  12  9  6  27``````
``````/*
Kotlin program for
Find all common elements in given 3 sorted arrays
*/
class Product
{
// Display array elements
fun printArray(arr: Array < Int > , n: Int): Unit
{
var i: Int = 0;
while (i < n)
{
print(" " + arr[i]);
i += 1;
}
}
fun productNextAndPrevious(arr: Array < Int > , n: Int): Unit
{
if (n <= 1)
{
return;
}
// Auxiliary variable back and auxiliary
var back: Int = arr[0];
var auxiliary: Int;
// First element is product of start two elements
arr[0] = arr[1] * back;
var i: Int = 1;
while (i < n - 1)
{
// Get actual value of previous element
auxiliary = back;
// Get current element
back = arr[i];
// Multiplication of next and previous element
arr[i] = arr[i + 1] * auxiliary;
i += 1;
}
// Last element is product of two last element
arr[n - 1] = arr[n - 1] * back;
}
}
fun main(args: Array < String > ): Unit
{
val task: Product = Product();
val arr1: Array < Int > = arrayOf(3, 2, 5, 3, 6, 1, -2, 3);
val arr2: Array < Int > = arrayOf(1, 4, 6, 2);
val arr3: Array < Int > = arrayOf(3, 6, 1, 2, 9, 3);
// Get the size of arrays
val l: Int = arr1.count();
val m: Int = arr2.count();
val n: Int = arr3.count();
// Test A
print("\n Before Replace Array arr1 \n");
print("\n After Replace Array arr1 \n");
// Test B
print("\n Before Replace Array arr2 \n");
print("\n After Replace Array arr2 \n");
// Test C
print("\n Before Replace Array arr3 \n");
print("\n After Replace Array arr3 \n");
}``````

#### Output

`````` Before Replace Array arr1
3 2 5 3 6 1 -2 3
After Replace Array arr1
6 15 6 30 3 -12 3 -6
Before Replace Array arr2
1 4 6 2
After Replace Array arr2
4 6 8 12
Before Replace Array arr3
3 6 1 2 9 3
After Replace Array arr3
18 3 12 9 6 27``````

## Time Complexity

The time complexity of this solution is O(n) because we iterate through the array once, performing a constant amount of work at each step. Therefore, the algorithm is efficient and has a linear time complexity.

## Comment

Please share your knowledge to improve code and content standard. Also submit your doubts, and test case. We improve by your feedback. We will try to resolve your query as soon as possible.

Categories
Relative Post