Code360 powered by Coding Ninjas X Naukri.com. Code360 powered by Coding Ninjas X Naukri.com
Table of contents
1.
Introduction
1.1.
What is an Array data structure?
1.2.
Problem statement
2.
Approach
2.1.
Pseudo Code
2.2.
Implementation in Java
2.2.1.
Complexity Analysis
3.
Frequently Asked Questions
3.1.
Mention some advantages of Array.
3.2.
What are the default values for integer data types in an Array?
3.3.
Can we pass any negative values in Array size? If no, then specify the exception that will occur.
4.
Conclusion
Last Updated: Mar 27, 2024
Easy

Replace every array element by multiplication of previous and next elements

Author Rupal Saluja
0 upvote
Roadmap to SDE career at Amazon
Speaker
Anubhav Sinha
SDE-2 @
25 Jun, 2024 @ 01:30 PM

Introduction

In this blog, we will try to grab hold of a single-dimensional ‘Array’ data structure problem. Through that problem, we will try to understand how to approach any array data structure problem. The examples presented will help you clear your concepts, Pseudocode will provide you with an immediate solution, and the implementation part will give you a detailed understanding of every step.

What is an Array data structure?

A collection of elements stored at contiguous memory locations is an Array. The elements stored in the array are of the same type. The contiguous memory locations of the array make it easier to calculate the position of each element in the array.

The first element is at index 0. As we transverse the array, the index increases by 1 till n-1 if there are n elements in the array. 

Array

Problem statement

Firstly, we will have a look at what exactly the problem says.

Given an array of ‘n’ elements. In that array, every element should be replaced by the product of its previous and next elements.

The two exceptions to this can be:

  1. The first element will be replaced by the product of the first and the second elements.
  2. The last element will be replaced by the product of the second last and the last elements.

 

Example 1

Explanation:

In the image below, we can observe how the output array gets its elements by the multiplication of the previous and the next elements. For now, we have excluded the first and the last elements of the output array which are exceptions for the above problem statement.

 

Example 2

Explanation:

In the image below, we can observe how the output array gets its elements by the multiplication of the previous and the next elements. This time, we have included the first and the last elements of the output array which are exceptions for the above problem statement.

 

Example 3

Explanation:

In the image below, we can observe how the output array gets its elements by the multiplication of the previous and the next elements. This time, we have put zero as the first element and have included the first and the last elements of the output array which are exceptions for the above problem statement.

 

Example 4

Explanation:

In the image below, we can observe how the output array gets its elements by the multiplication of the previous and the next elements. This time, we have put zero as the last element and have included the first and the last elements of the output array which are exceptions for the above problem statement.

 

Example 5

Explanation:

In the image below, we can observe how the output array gets its elements by the multiplication of the previous and the next elements. This time, we have put zero as the middle element and have included the first and the last elements of the output array which are exceptions for the above problem statement.

 

Also see,  Rabin Karp Algorithm

Approach

Step-1:

Initially, we will create a temporary array of the same length as that of the input array. Here, we have considered an example array of 5 elements.

Temporary array, temp[] will be used to store the elements derived after the previous and the next elements of the input array is multiplied.

 

Step-2:

As the first element of the output array will be an exception, so in the output array, the first element will be the multiplication of the first and the second element of the input array.

 

Step-3:

The second element will be the multiplication of its previous and next elements. There are no exceptions here. Similarly, we will get the values for the next two elements of the output array. 

In case there are more elements than we have considered, the same procedure will be followed for them.

Step-4:

Now, for the last element, which is again an exception, that will be the multiplication of the last and the last second elements of the array.

Step-5:

The temporary array will be returned in the form of an output array.

Pseudo Code

static int[] mul(int nums[]){
        int[] temp = new int[nums.length];
        temp[0] = nums[1];
        temp[nums.length-1] = nums[nums.length-2];
        for(int i=1; i<nums.length-1; i++ )
        {
            temp[i] = nums[i-1] * nums[i+1];
        }
        return temp;
    }

Implementation in Java

//Java Program code of the problem of replacing every element of array by multiplication of previous and next elements.
import java.io.*;
import java.util.*;
import java.lang.Math;
public class Main
{
    static int[] mul(int nums[]){
        int[] temp = new int[nums.length];
        temp[0] = nums[1] * nums[0];
        temp[nums.length-1] = nums[nums.length-2] * nums[nums.length-1];
        for(int i=1; i<nums.length-1; i++ )
        {
            temp[i] = nums[i-1] * nums[i+1];
        }
        return temp;
    }
public static void main(String[] args) {
  int[] arr = {1,2,3,4,5};
  int [] temp = new int[arr.length];
  temp=mul(arr);
  for(int i=0; i < temp.length;i++){
      System.out.print(temp[i] + " ");
  }
}
}

 

Output

arr[]={2, 3, 8, 15, 20}

 

Complexity Analysis

  • Since we have just one loop running in the solution provided. The time complexity for the code mentioned above will be O(n).
  • We have not used any extra space to store any value. Hence, the space complexity for the code mentioned above will be O(1).

Check out this problem - First And Last Occurrences Of X

Get the tech career you deserve, faster!
Connect with our expert counsellors to understand how to hack your way to success
User rating 4.7/5
1:1 doubt support
95% placement record
Akash Pal
Senior Software Engineer
326% Hike After Job Bootcamp
Himanshu Gusain
Programmer Analyst
32 LPA After Job Bootcamp
After Job
Bootcamp

Frequently Asked Questions

Mention some advantages of Array.

  • It is easy to traverse.
  • Data can be manipulated and sorted very easily.
  • You can refer to a large number of elements by just specifying the index number and the array name.
  • It is easy to do the calculation in a loop while using the Array data structure.

 

What are the default values for integer data types in an Array?

If the values are not specified by the programmer, then default values to the variables are assigned according to their data type. The default values for Integer data types are mentioned below.

  • byte- 0
  • short- 0
  • int- 0
  • long- 0

 

Can we pass any negative values in Array size? If no, then specify the exception that will occur.

No, we cannot pass any negative values in Array size. If we do so, then we will get ‘NegativeArraySizeException’ at run time.

Conclusion

To summarize the talk, we looked at the problem statement, had a look at every possible condition that could occur, understood the approach, went through the Pseudocode, and saw the implementation.

We hope the above discussion helped you understand and solve the problem better and now it is your turn to code the problem in your own way.

If you want to practice problems on array then you can refer to these links:

 

For peeps out there who want to grasp more about DSA, Power programming, JavaScript, or any other technical or core topics, you can refer to guided paths on Coding Ninjas Studio. Do enroll in the courses provided by us, take mock tests and solve problems available and interview puzzles. Also, you can pay attention to interview stuff- interview experiences and an interview bundle for placement preparations. 

Do upvote our blog to help fellow ninjas grow.

Happy Coding!

Previous article
Rearrange Array such that Even Positioned are Greater than Odd
Next article
Positive elements at even and negative at odd positions
Live masterclass