


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!

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

AI Hentai Generator
Generate AI Hentai for free.

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

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

Hot Topics



In this article, we aim to explore a fascinating question about the greatest common divisor (GCD) of arrays in various programming languages, focusing on C++. We will demonstrate an algorithmic approach that utilizes pairwise element exchanges and the number of their products to verify whether it is possible to improve GCD above 1. In addition, we will provide other ways to solve this problem, each with its syntax definition. In addition to these solutions, we will also present two complete executable codes that contain these methods. Syntax To ensure a clear understanding of the code examples that follow, we must evaluate and understand the syntax used before doing so. #include<iostream>#include<vecto

The pairwise product of the set X={a,b,c} can be defined as the sum of the products of all possible set pairs. The pairs of sets are Y={a*a,a*b,a*c,b*b,b*c,c*c}, where the products are commutative. Therefore, the pairwise product of set X is the sum of the elements of set Y, which is aa+ab+ac+bb+bc+cc. In mathematical terms, the sum of possible pairwise products can be expressed as: $$\mathrm{\displaystyle\sum\limits_{i=1,j=i}^{i\leqn,j\leqn}\:(i, j)=i\timej}$$Problem StatementGiven a number n. In the range (1, n), including n and 1, find

GivenanumberNwithwehavetoproductthenumberwithitslargestodddigit.Ifthereisnoodddigitthenprint-1.LikewehaveinitializedNwith“153”andthelargestodddigitinthisnumberis5sotheresultwouldbetheproductof153with5i.e.153*5=765andifthenumberhas

How to sort an array of integers in ascending order using sort function in Go language? Go language is a simple and efficient programming language that provides a series of powerful standard library functions to meet different programming needs. Among them, the sorting function is one of the commonly used functions in the Go language. This article will introduce how to use the sort function in the Go language to sort an integer array in ascending order, with relevant code examples. In Go language, we can use the functions in the sort package to perform sorting operations. The sort package provides a variety of sorting algorithms, including fast

Data structure manipulation is now an important aspect of successful solution development in modern programming and computing. This is due to the increasing complexity presented by these structures over time. An example is performing a swap operation to minimize the sum of the largest numbers contained in two arrays, thereby lowering their overall value. In this article, we discuss two methods of accomplishing these tasks using C++, while acknowledging the advantages and disadvantages of both methods based on different perspectives. Syntax In order to effectively understand the methods and code in the C++ programming language, we need to have a solid understanding of basic syntax. This means taking a closer look at the components that are relevant to the topic at hand. Arrays:intarrayName[size];Sort

IntroductionThere are different types of data structures in Python. A tuple is a data structure, which is an ordered collection of elements. Tuples are also said to be immutable. Immutable basically means that once created, a tuple cannot be modified. In the following article, we will learn about the program to find the product of selected tuple keys in Python. This procedure is useful in problems that require multiplication of specific elements in a tuple. UnderstandingtheProblemTupleisinitializedinasimilarwayweintializethelistinpython.Tuplestoresaseque

Introduction In mathematics, we see problems that require multiplying a number by a certain power. We will understand this problem through an example. Imagine we have a list of numbers and we want to multiply the same element by itself a certain number of times. This is where the "find product of i^k in list" procedure comes in handy. In mathematics, we call this process exponentiation. It helps in solving most mathematical calculations and problems. We will write the code in python. It's easier to solve such problems using python. Python, unlike all programming languages, contains special tools such as libraries that make the world of programming easier and save time. Libraries in Python contain functions that help solve mathematical problems quickly. means no need to start from scratch

Recursion is a technique of calling functions from the same function itself. There must be some base or terminating condition to end the recursive call. Recursive procedures are very helpful for performing complex iterative solutions with less code and finding easier solutions through sub-operations. In this article, we will discuss the recursive method of performing the product (multiplication) between two numbers in C++. First we understand the basic principles, recursive function calling syntax, algorithm and source code. Multiplication using recursion In high-level languages, there are multiplication operators that can perform multiplication directly. However, we know that multiplication is actually repeated addition. So the result of A*B is the number of repeated additions of A and B, or it can be said that the number of repeated additions of B and A. Whenever there is duplication, we can do it using recursion
