Java Program to Replace Every Array Element by Multiplication with its Next Element

In the previous article, we have seen Java Program to Replace Every Array Element by Multiplication of Previous and Next Element

In this article we are going to see how to replace every array element by multiplication with its next element using Java programming language.

Java Program to Replace Every Array Element by Multiplication with its Next Element

Array is a data structure which stores a fixed size sequential collection of values of single type. Where with every array elements/values memory location is associated. Each array elements have it’s own index where array index starts from 0.

In Array set of variables referenced by a single variable name and it’s array index position. It is also called as a container object which contains elements of similar type.

Declaration of an array:

dataType[] arrayName; (or)                              //Declaring an array
dataType []arrayName; (or)
dataType arr[];

Instantiation of an Array:

arrayName = new datatype[size];                    //Allocating memory to array

Combining both Statements in One:

dataType[] arrayName = new dataType[size] //Declaring and Instantiating array

Initialization of an Array:

arrayName[index-0]= arrayElement1             //Initializing the array

...

arrayName[index-s]= arrayElementS

Combining all Statements in One:

dataType arrayName[ ]={e1,e2,e3};               //declaration, instantiation and initialization

Let’s see different ways to replace every array element by multiplication with its next element.

Method-1: Java Program to Replace Every Array Element by Multiplication with its Next Element By Using An Extra Array

Approach:

  • Declare and initialize the original array.
  • Create another array of the size of the original array.
  • Print the original array.
  • Iterate over the array.
  • At each index, except 0th and last, update the element with the product of current and next element in the original array. And store it new array.
  • Print the new array.

Program:

import java.util.Arrays;
import java.util.Scanner;

public class Main 
{

    public static void main(String[] args) 
    {
        // create scanner class object
        Scanner sc = new Scanner(System.in);
        // take input from user for array size
        System.out.print("Enter the size of array: ");
        int n = sc.nextInt();
        // initialize array with size n
        int[] arr = new int[n];
        // take input from user for array elements
        System.out.print("Enter array elements: ");
        for (int i = 0; i < n; i++) 
        {
            arr[i] = sc.nextInt();
        }
        System.out.println("Before rearranging: " + Arrays.toString(arr));
        rearrange(arr);
    }

    public static void rearrange(int[] arr) 
    {
        int[] temp = new int[arr.length];
        for (int i = 0; i < temp.length - 1; i++) 
        {
            temp[i] = arr[i] * arr[i + 1];
        }
        temp[temp.length - 1] = arr[arr.length - 1];
        System.out.println("After rearranging: " + Arrays.toString(temp));
    }
}
Output:

Enter the size of array: 5
Enter array elements: 1 2 3 4 5
Before rearranging: [1, 2, 3, 4, 5]
After rearranging: [2, 6, 12, 20, 5]

Method-2: Java Program to Replace Every Array Element by Multiplication with its Next Element Without Using an Extra Array

Approach:

  • Ask the user to enter the array size and store it.
  • Create an empty array of the specified size.
  • Ask the user to enter the elements.
  • Print the array elements.
  • Iterate over the array.
  • At each index, except 0th and last, update the element with the product of current and next element in the original array and replace that new value in the original array.
  • Print the array.

Program:

import java.util.Arrays;
import java.util.Scanner;

public class Main
{

    public static void main(String[] args) 
    {
        // create scanner class object
        Scanner sc = new Scanner(System.in);
        // take input from user for array size
        System.out.print("Enter the size of array: ");
        int n = sc.nextInt();
        // initialize array with size n
        int[] arr = new int[n];
        // take input from user for array elements
        System.out.print("Enter array elements: ");
        for (int i = 0; i < n; i++) 
        {
            arr[i] = sc.nextInt();
        }
        System.out.println("Before rearranging: " + Arrays.toString(arr));
        rearrange(arr);
    }

    public static void rearrange(int[] arr) 
    {
        for (int i = 0; i < arr.length - 1; i++) 
        {
            arr[i] = arr[i] * arr[i + 1];
        }

        System.out.println("After rearranging: " + Arrays.toString(arr));
    }
}
Output:

Enter the size of array: 5
Enter array elements: 1 2 3 4 5
Before rearranging: [1, 2, 3, 4, 5]
After rearranging: [2, 6, 12, 20, 5]

Have you mastered basic programming topics of java and looking forward to mastering advanced topics in a java programming language? Go with these ultimate Advanced java programs examples with output & achieve your goal in improving java coding skills.

Related Java Programs: