


JAVA program to replace elements of integer array with product of other elements
An integer array refers to an array in which all elements are of integer type. It is also known as an array of integers.
As per the problem statement we have to create an Integer array and display the array elements where all the array elements are the product of other elements of the array.
In this article, we will see how to replace array elements by the product of other array elements by using Java programming language.
To show you some instances −
Instance-1
int arr[] = { 2, 3, 1, 4, 6 } At Index-0 value will be = 3 * 1 * 4 * 6 = 72 At Index-1 value will be = 2 * 1 * 4 * 6 = 48 At Index-2 value will be = 2 * 3 * 4 * 6 = 144 At Index-3 value will be = 2 * 3 * 1 * 6 = 36 At Index-4 value will be = 2 * 3 * 1 * 4 = 24 So, the updated array elements are {72, 48, 144, 36, 24}
Instance-2
int arr[] = { 1, 3, 2, 4 } At Index-0 value will be = 3 * 2 * 4 = 24 At Index-1 value will be = 1 * 2 * 4 = 8 At Index-2 value will be = 1 * 3 * 4 = 12 At Index-3 value will be = 1 * 3 * 2 = 6 So, the updated array elements are {24, 8, 12, 6}
Algorithm
Algorithm-1
Step-1 − Declare and initialize an integer array.
Step 2 - Find the product of all array elements.
Step-3 − Divide the product value with the value of the respective index and replace the result. Continue this step till you reach the last array element.
Step-4 − Print the updated array.
Algorithm-2
Step-1 − Declare and initialize an integer array.
Step-2 − Find the product of left sub array elements. The elements before the respective element to be replaced.
Step-3 − Find the product of the right subarray elements. The element after the element to be replaced.
Step-4 − Find the sum of the product values of the left subarray and the right subarray, and replace it with the result value.
Step-5 − Continue Step-2, 3 and 4 till you reach the last array element.
Step-6 − Print the updated array.
Syntax
To get the length of an array (number of elements in that array), there is an inbuilt property of array i.e length.
Below refers to the syntax of it −
array.length
where, ‘array’ refers to the array reference.
To get the String representation of the respective array we can use the toString() method of Arrays class in Java.
Arrays.toString(A)
Multiple methods−
We have provided the solution in different approaches.
By dividing the respective index elements by the total product value
By finding the product value of the left subarray and the right subarray
Let's look at the program and its output one by one.
Approach-1 − By Dividing the respective Index Element with the Total Product Value
In this method we will replace the array elements by dividing the total product value by the corresponding element to be replaced.
Example
import java.util.Arrays; public class Main { public static void main(String args[]) { //Declare and initialize the array elements int arr[] = { 2, 3, 1, 4, 6 }; // Print the array elements System.out.println("Array elements are: "+Arrays.toString(arr)); //Declare an int variable 'product' to hold the product value int product = 1; //iterate the array elements and find the product of all elements for(int i=0;i<arr.length;i++) { product*=arr[i]; } //Divide the total product value with the array element to be replaced for(int i=0;i<arr.length;i++) { arr[i] = product/arr[i]; } //print the new array System.out.println("Updated array: "+Arrays.toString(arr)); } }
Output
Array elements are: [2, 3, 1, 4, 6] Updated array: [72, 48, 144, 36, 24]
Approach-2 − By Finding the Product Values of Left and Right Sub Array
In this approach, we will replace the array element finding the sum of product of left subarray elements and product of right sub array elements.
Example
import java.util.Arrays; public class Main{ public static void main(String args[]) { int arr[] = {4,2,3,1}; int size=arr.length; int temp[]= new int[size]; int sum=1; System.out.println("Array elements are: "+Arrays.toString(arr)); for(int index=0; index<arr.length; index++) { int leftProduct=1; int rightProduct=1; int i=0; if(i<index) { for(i=0;i<index;i++) { leftProduct*=arr[i]; } } for(i=index+1;i<arr.length;i++){ rightProduct*=arr[i]; } sum=leftProduct*rightProduct; temp[index]=sum; } arr=temp; System.out.println("Updated array: "+Arrays.toString(arr)); } }
Output
Array elements are: [4, 2, 3, 1] Updated array: [6, 12, 8, 24]
In this article, we explored how to replace array elements with the product of other array elements in Java using different methods.
The above is the detailed content of JAVA program to replace elements of integer array with product of other elements. For more information, please follow other related articles on the PHP Chinese website!

Start Spring using IntelliJIDEAUltimate version...

When using MyBatis-Plus or other ORM frameworks for database operations, it is often necessary to construct query conditions based on the attribute name of the entity class. If you manually every time...

Java...

How does the Redis caching solution realize the requirements of product ranking list? During the development process, we often need to deal with the requirements of rankings, such as displaying a...

Conversion of Java Objects and Arrays: In-depth discussion of the risks and correct methods of cast type conversion Many Java beginners will encounter the conversion of an object into an array...

Solutions to convert names to numbers to implement sorting In many application scenarios, users may need to sort in groups, especially in one...

Detailed explanation of the design of SKU and SPU tables on e-commerce platforms This article will discuss the database design issues of SKU and SPU in e-commerce platforms, especially how to deal with user-defined sales...

How to set the SpringBoot project default run configuration list in Idea using IntelliJ...


Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

DVWA
Damn Vulnerable Web App (DVWA) is a PHP/MySQL web application that is very vulnerable. Its main goals are to be an aid for security professionals to test their skills and tools in a legal environment, to help web developers better understand the process of securing web applications, and to help teachers/students teach/learn in a classroom environment Web application security. The goal of DVWA is to practice some of the most common web vulnerabilities through a simple and straightforward interface, with varying degrees of difficulty. Please note that this software

VSCode Windows 64-bit Download
A free and powerful IDE editor launched by Microsoft

SublimeText3 Mac version
God-level code editing software (SublimeText3)

SAP NetWeaver Server Adapter for Eclipse
Integrate Eclipse with SAP NetWeaver application server.

Dreamweaver Mac version
Visual web development tools