Maximum Difference in An Array Hackerrank Java

0
637

Maximum Difference in An Array Hackerrank Java

Maximum Difference in An Array Hackerrank Java
Maximum Difference in An Array Hackerrank Java

A method of data organisation that allows for effective data storage, retrieval, and use.

This is a data structure for storing similar components (generally). It’s worth noting that although arrays are often referred to as in A documentation, the variable names you use in code should be descriptive and start with lowercase letters.

A basic data structure for storing a set of data in a contiguous block of memory is an array. The index is used to access each element in the array, and the elements are easy to find since they are stored sequentially in memory.

We usually use arrays because we know exactly how many pieces of data we’ll have since the number of elements in an array is stored as a large block of data. An array could be used to store a list of student ID numbers or the names of state capitals, for example. You will write the following code to generate an array of integers called myArray that can hold four integer values:

int[] myArray = new int[4];

This allocates a memory block capable of storing four integers. Each integer storage cell is given a unique index that ranges from 0 to one less than the array’s height, and each cell starts with a. We can store integers at indices 0,1, 2, and 3 in the case of myArray. Let’s assume we wanted to store the number 12 in the last cell; to do so, we write:

myArray[3] = 12;

Similarly, using the following code, we can print the contents of the last cell:

println(myArray[3]); System.out.println(myArray[3]);

The code prints the value stored at index 3 of myArray, which is 12 in this case (the value we previously stored there). It’s worth noting that, while Java assigns a 0 to each cell of an array of integers, this isn’t the case in all languages.

Task Maximum Difference in An Array Hackerrank Java

The following is what your editor’s code does:

Reads an integer from stdin and saves it to the vector n, which represents a set of integers.
Reads n integers from stdin, one for each of a0,a1’….an-1, and saves each integer ai to a variable, val.
Attempts to print every element of an integer array called a.
In the unlocked section of your editor, type the following code:

Make an array, a, that can hold n integers.
Modify the loop’s code so that each sequential value is saved to the array’s corresponding position. The first value, for example, must be stored in a0, the second value in a1, and so on.
Best of luck!

Input Maximum Difference in An Array Hackerrank Java

The array’s size is indicated on the first line by a single integer,.
The value of element is represented by a single integer on each of the following lines.

Output Maximum Difference in An Array Hackerrank Java

You are not liable for any output that is printed to stdout. The editor’s locked code loops through array a, printing each element sequentially on a new line.

Sample Input Maximum Difference in An Array Hackerrank Java

5
10
20
30
40
50

Sample Output Maximum Difference in An Array Hackerrank Java

10
20
30
40
50

Explanation Maximum Difference in An Array Hackerrank Java

We get a=[10,20,30,40,50] when we save each integer to its corresponding index in a. From left to right, the locked code prints each array element on a new line.

/* Difference Class – compute the maximum difference between two elements in an integer array */

public class Difference {

/* Limit the scope of the elements array to methods within the class */

private int[] elements;

/* The maximum difference is available outside of the class */

public int maximum difference;



/* Constructor for the Difference Class */

Difference(int[] array) {

/* Initialize the elements array, with the incoming array */

elements = array;

}



/* Compute the maximum difference between elements */

public void computeDifference() {

/* Initialize the maximum difference to the minimum difference 0 */

maximumDifference = 0;

/* Display the elements in the array */

for (int i = 0; i < elements.length; i++) {

/* Print the next element */

System.out.println(elements[i]);

}



/* Compare from the beginning to the next to last element of the array */

for (int i = 0; i < elements.length – 1; i++) {

/* Compare from the second element to the last element of the array */

for (int j = i+1; j < elements.length ; j++ ){

/* Display the indexes, values, and differences for each comparison */

System.out.println(“Comparing elements[” + i +

“]=” + elements[i] +

” with elements[” + j +

“]=” + elements[j] +

“, Difference=” + Math.abs(elements[i] – elements[j]));

/* If the difference is greater than the maximum difference */

if (Math.abs(elements[i] – elements[j]) > maximumDifference) {

/* Change to the new maximum difference */

maximumDifference = Math.abs(elements[i] – elements[j]);

}

}

}



return;

}



}

Arrays

LEAVE A REPLY

Please enter your comment!
Please enter your name here