We use cookies to ensure you have the best browsing experience on our website. Please read our cookie policy for more information about how we use cookies.
here is the code which worked for me. Hope it works for you as well...
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];
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]);
}
}
can you tell me how's data is declaring in this two loops
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]);*
for(int i = 0; i < n; i++){
a[i]=scan.nextInt();
}
This piece of code takes input it doesn't print in order for something to be printed we need to use System.out. Therefore this piece of code only accepts input.
for (int i = 0; i < a.length; i++) {
System.out.println(a[i]);
}
This piece of code displays each element one by one as the value of i increases. Therefore it is only this loop that prints the values and hence only one loop displays the values.
In the first loop we are inserting the values to each ith indices of the array .
In the second loop we are displaying the inserted values of the respectve ith indices starting from 0 to n.
Java 1D Array
You are viewing a single comment's thread. Return to all comments →
here is the code which worked for me. Hope it works for you as well...
import java.util.*;
public class Solution {
}
Please dont share anything you want
here is Java 1D array problem solution https://programs.programmingoneonone.com/2021/02/hackerrank-java-1-d-array-solution.html
Here are my few different solutions in Java. 100% all test cases will pass
I hope you will find my answer useful.
Hackerrank Java 1D Array Solution
updated solution is here
https://www.thecscience.com/2021/05/HackerRank-java-1D-array-problem-solution.html
thanks
can you tell me how's data is declaring in this two loops
for(int i = 0; i < n; i++){ a[i]=scan.nextInt(); } This piece of code takes input it doesn't print in order for something to be printed we need to use System.out. Therefore this piece of code only accepts input.
for (int i = 0; i < a.length; i++) { System.out.println(a[i]); } This piece of code displays each element one by one as the value of i increases. Therefore it is only this loop that prints the values and hence only one loop displays the values.
In the first loop we are inserting the values to each ith indices of the array . In the second loop we are displaying the inserted values of the respectve ith indices starting from 0 to n.
Here is my solution for better understanding https://www.codinghumans.xyz/2020/08/java-1d-array-hackerrank-solution-by.html
this code printing array element like this: 10 20 30 40 50 10 20 30 40 50
this code is giving wrong output..
i also did but my both test case failed