Hello Programmers/Coders, Today we are going to share solutions of Programming problems of HackerRank of Programming Language Java . At Each Problem with Successful submission with all Test Cases Passed, you will get an score or marks. And after solving maximum problems, you will be getting stars. This will highlight your profile to the recruiters.
In this post, you will find the solution for Java 1D Array-HackerRank Problem. We are providing the correct and tested solutions of coding problems present on HackerRank. If you are not able to solve any problem, then you can take help from our Blog/website.
Use “Ctrl+F” To Find Any Questions Answer. & For Mobile User, You Just Need To Click On Three dots In Your Browser & You Will Get A “Find” Option There. Use These Option to Get Any Random Questions Answer.
Introduction To Java
JAVA was developed by James Gosling at Sun Microsystems Inc in the year 1991, later acquired by Oracle Corporation. It is a simple programming language. Java makes writing, compiling, and debugging programming easy. It helps to create reusable code and modular programs.
Java is a class-based, object-oriented programming language and is designed to have as few implementation dependencies as possible. A general-purpose programming language made for developers to write once run anywhere that is compiled Java code can run on all platforms that support Java. Java applications are compiled to byte code that can run on any Java Virtual Machine. The syntax of Java is similar to c/c++.
Link for the Problem – Java 1D Array – Hacker Rank Solution
Java 1D Array – Hacker Rank Solution
Problem :
An array is a simple data structure used to store a collection of data in a contiguous block of memory. Each element in the collection is accessed using an index, and the elements are easy to find because they’re stored sequentially in memory.
Because the collection of elements in an array is stored as a big block of data, we typically use arrays when we know exactly how many pieces of data we’re going to have. For example, you might use an array to store a list of student ID numbers, or the names of state capitals. To create an array of integers named that can hold four integer values, you would write the following code:
int[] myArray = new int[4];
This sets aside a block of memory that’s capable of storing integers. Each integer storage cell is assigned a unique index ranging from to one less than the size of the array, and each cell initially contains a . In the case of , we can store integers at indices , , , and . Let’s say we wanted the last cell to store the number ; to do this, we write:
myArray[3] = 12;
Similarly, we can print the contents of the last cell with the following code:
System.out.println(myArray[3]);
The code above prints the value stored at index of , which is (the value we previously stored there). It’s important to note that while Java initializes each cell of an array of integers with a , not all languages do this.
Task
The code in your editor does the following:
- Reads an integer from stdin and saves it to a variable, , denoting some number of integers.
- Reads integers corresponding to from stdin and saves each integer to a variable, .
- Attempts to print each element of an array of integers named .
Write the following code in the unlocked portion of your editor:
- Create an array, , capable of holding integers.
- Modify the code in the loop so that it saves each sequential value to its corresponding location in the array. For example, the first value must be stored in , the second value must be stored in , and so on.
Good luck!
Input Format
The first line contains a single integer, , denoting the size of the array.
Each line of the subsequent lines contains a single integer denoting the value of element .
Output Format
You are not responsible for printing any output to stdout. Locked code in the editor loops through array and prints each sequential element on a new line.
Sample Input
5 10 20 30 40 50
Sample Output
10 20 30 40 50
Explanation
When we save each integer to its corresponding index in , we get . The locked code prints each array element on a new line from left to right.
Java 1D Array – Hacker Rank Solution
import java.util.*; public class Solution { public static void main(String[] args) { Scanner scan = new Scanner(System.in); int n = scan.nextInt(); int[]a=new int[n]; //inserting values at each ith indices for(int i=0;i<n;i++) { a[i]=scan.nextInt(); } scan.close(); // Prints each sequential element in array a for (int i = 0; i < a.length; i++) { System.out.println(a[i]); } } }