Skip to main content

Replace array element by multiplication of previous and next

Here given code implementation process.

/*
    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");
		task.printArray(arr1, l);
		task.productNextAndPrevious(arr1, l);
		System.out.print("\n After Replace Array arr1 \n");
		task.printArray(arr1, l);
		// Test B
		System.out.print("\n Before Replace Array arr2 \n");
		task.printArray(arr2, m);
		task.productNextAndPrevious(arr2, m);
		System.out.print("\n After Replace Array arr2 \n");
		task.printArray(arr2, m);
		// Test C
		System.out.print("\n Before Replace Array arr3 \n");
		task.printArray(arr3, n);
		task.productNextAndPrevious(arr3, n);
		System.out.print("\n After Replace Array arr3 \n");
		task.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
// 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";
	task->printArray(arr1, l);
	task->productNextAndPrevious(arr1, l);
	cout << "\n After Replace Array arr1 \n";
	task->printArray(arr1, l);
	// Test B
	cout << "\n Before Replace Array arr2 \n";
	task->printArray(arr2, m);
	task->productNextAndPrevious(arr2, m);
	cout << "\n After Replace Array arr2 \n";
	task->printArray(arr2, m);
	// Test C
	cout << "\n Before Replace Array arr3 \n";
	task->printArray(arr3, n);
	task->productNextAndPrevious(arr3, n);
	cout << "\n After Replace Array arr3 \n";
	task->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
// 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");
		task.printArray(arr1, l);
		task.productNextAndPrevious(arr1, l);
		Console.Write("\n After Replace Array arr1 \n");
		task.printArray(arr1, l);
		// Test B
		Console.Write("\n Before Replace Array arr2 \n");
		task.printArray(arr2, m);
		task.productNextAndPrevious(arr2, m);
		Console.Write("\n After Replace Array arr2 \n");
		task.printArray(arr2, m);
		// Test C
		Console.Write("\n Before Replace Array arr3 \n");
		task.printArray(arr3, n);
		task.productNextAndPrevious(arr3, n);
		Console.Write("\n After Replace Array arr3 \n");
		task.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
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");
	$task->printArray($arr1, $l);
	$task->productNextAndPrevious($arr1, $l);
	echo("\n After Replace Array arr1 \n");
	$task->printArray($arr1, $l);
	// Test B
	echo("\n Before Replace Array arr2 \n");
	$task->printArray($arr2, $m);
	$task->productNextAndPrevious($arr2, $m);
	echo("\n After Replace Array arr2 \n");
	$task->printArray($arr2, $m);
	// Test C
	echo("\n Before Replace Array arr3 \n");
	$task->printArray($arr3, $n);
	$task->productNextAndPrevious($arr3, $n);
	echo("\n After Replace Array arr3 \n");
	$task->printArray($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");
	task.printArray(arr1, l);
	task.productNextAndPrevious(arr1, l);
	process.stdout.write("\n After Replace Array arr1 \n");
	task.printArray(arr1, l);
	// Test B
	process.stdout.write("\n Before Replace Array arr2 \n");
	task.printArray(arr2, m);
	task.productNextAndPrevious(arr2, m);
	process.stdout.write("\n After Replace Array arr2 \n");
	task.printArray(arr2, m);
	// Test C
	process.stdout.write("\n Before Replace Array arr3 \n");
	task.printArray(arr3, n);
	task.productNextAndPrevious(arr3, n);
	process.stdout.write("\n After Replace Array arr3 \n");
	task.printArray(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() :
	task = Product()
	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 ")
	task.printArray(arr1, l)
	task.productNextAndPrevious(arr1, l)
	print("\n After Replace Array arr1 ")
	task.printArray(arr1, l)
	#  Test B
	print("\n Before Replace Array arr2 ")
	task.printArray(arr2, m)
	task.productNextAndPrevious(arr2, m)
	print("\n After Replace Array arr2 ")
	task.printArray(arr2, m)
	#  Test C
	print("\n Before Replace Array arr3 ")
	task.printArray(arr3, n)
	task.productNextAndPrevious(arr3, n)
	print("\n After Replace Array arr3 ")
	task.printArray(arr3, n)

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() 
	task = Product.new()
	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")
	task.printArray(arr1, l)
	task.productNextAndPrevious(arr1, l)
	print("\n After Replace Array arr1 \n")
	task.printArray(arr1, l)
	#  Test B
	print("\n Before Replace Array arr2 \n")
	task.printArray(arr2, m)
	task.productNextAndPrevious(arr2, m)
	print("\n After Replace Array arr2 \n")
	task.printArray(arr2, m)
	#  Test C
	print("\n Before Replace Array arr3 \n")
	task.printArray(arr3, n)
	task.productNextAndPrevious(arr3, n)
	print("\n After Replace Array arr3 \n")
	task.printArray(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");
		task.printArray(arr1, l);
		task.productNextAndPrevious(arr1, l);
		print("\n After Replace Array arr1 \n");
		task.printArray(arr1, l);
		// Test B
		print("\n Before Replace Array arr2 \n");
		task.printArray(arr2, m);
		task.productNextAndPrevious(arr2, m);
		print("\n After Replace Array arr2 \n");
		task.printArray(arr2, m);
		// Test C
		print("\n Before Replace Array arr3 \n");
		task.printArray(arr3, n);
		task.productNextAndPrevious(arr3, n);
		print("\n After Replace Array arr3 \n");
		task.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
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 ");
	task.printArray(arr1, l);
	task.productNextAndPrevious(&arr1, l);
	print("\n After Replace Array arr1 ");
	task.printArray(arr1, l);
	// Test B
	print("\n Before Replace Array arr2 ");
	task.printArray(arr2, m);
	task.productNextAndPrevious(&arr2, m);
	print("\n After Replace Array arr2 ");
	task.printArray(arr2, m);
	// Test C
	print("\n Before Replace Array arr3 ");
	task.printArray(arr3, n);
	task.productNextAndPrevious(&arr3, n);
	print("\n After Replace Array arr3 ");
	task.printArray(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
/*
    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");
	task.printArray(arr1, l);
	task.productNextAndPrevious(arr1, l);
	print("\n After Replace Array arr1 \n");
	task.printArray(arr1, l);
	// Test B
	print("\n Before Replace Array arr2 \n");
	task.printArray(arr2, m);
	task.productNextAndPrevious(arr2, m);
	print("\n After Replace Array arr2 \n");
	task.printArray(arr2, m);
	// Test C
	print("\n Before Replace Array arr3 \n");
	task.printArray(arr3, n);
	task.productNextAndPrevious(arr3, n);
	print("\n After Replace Array arr3 \n");
	task.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




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.

New Comment