Skip to content
Programmingoneonone
Programmingoneonone

Learn everything about programming

  • Home
  • CS Subjects
    • Internet of Things (IoT)
    • Digital Communication
    • Human Values
    • Cybersecurity
  • Programming Tutorials
    • C Programming
    • Data structures and Algorithms
    • 100+ Java Programs
    • 100+ C Programs
  • HackerRank Solutions
    • HackerRank Algorithms Solutions
    • HackerRank C problems solutions
    • HackerRank C++ problems solutions
    • HackerRank Java problems solutions
    • HackerRank Python problems solutions
Programmingoneonone
Programmingoneonone

Learn everything about programming

HackerRank Java 1D Array problem solution

YASH PAL, 31 July 202416 January 2026

HackerRank Java 1D Array problem solution – In this HackerRank java 1D Array problem in the java programming language 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. 

Task

The code in your editor does the following:

  1. Reads an integer from stdin and saves it to a variable, n, denoting some number of integers.
  2. Reads n integers corresponding to a0,a1,…,an-1 from stdin and saves each integer ai to a variable, val.
  3. Attempts to print each element of an array of integers named a.

Write the following code in the unlocked portion of your editor:

  1. Create an array, a, capable of holding n integers.
  2. 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 a0, the second value must be stored in a1, and so on.
HackerRank Java 1D Array problem solution

HackerRank Java 1D Array problem 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];
    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]);
        }
    }
}

Second 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];
        for(int i = 0 ; i < n; i++){
          int val = scan.nextInt();
            a[i] = val;
      }
        scan.close();

        // Prints each sequential element in array a
        for (int i = 0; i < a.length; i++) {
            System.out.println(a[i]);
        }
    }
}
coding problems solutions Hackerrank Problems Solutions Java solutions HackerRankjava

Post navigation

Previous post
Next post

Leave a Reply

Your email address will not be published. Required fields are marked *

HackerRank Java Solutions
Welcome to Java! problem solution
Java Stdin and Stdout I problem solution
Java If-Else problem solution
Java Stdin and Stdout II problem solution
Java Output Formatting problem solution
Java Loops I problem solution
Java Loops II problem solution
Java Datatypes problem solution
Java End-of-file problem solution
Java Static Initializer Block solution
Java Int to String problem solution
Java Date and Time problem solution
Java Currency Formatter problem solution
Java Strings Introduction problem solution
Java Substring problem solution
Java Substring Comparisons problem solution
Java String Reverse problem solution
Java Anagrams problem solution
Java String Tokens problem solution
Pattern Syntax Checker solution
Java Regex problem solution
Java Regex 2 — Duplicate Words solution
Valid Username Regular Expression solution
Java ArrayList problem solution
Java BigDecimal problem solution
Java Primality Test problem solution
Java BigInteger problem solution
Java 1D Array problem solution
Java 2D Array problem solution
java Subarray problem solution
Java Arraylist problem solution
Java 1D Array (Part 2) problem solution
Java List problem solution
Java Map problem solution
Java Stack problem solution
Java Hashset problem solution
Java Generics problem solution
Java Comparator problem solution
Java Sort problem solution
Java Dequeue problem solution
Java BitSet problem solution
Java Priority Queue problem solution
Java Inheritance I problem solution
Java Inheritance II problem solution
Java Abstract Class problem solution
Java Interface problem solution
Java Method Overriding problem solution
Java Method Overriding 2 (Super Keyword) solution
Java Instanceof keyword problem solution
Java Iterator problem solution
Java Exception Handling (Try-catch) solution
Java Exception Handling problem solution
Java Varargs — Simple Addition solution
Java Reflection — Attributes problem solution
Can You Access? problem solution
Prime Checker problem solution
Java Factory Pattern problem solution
Java Singleton Pattern problem solution
Java Visitor Pattern problem solution
Java Annotations problem solution
Covariant Return Types solution
Java Lambda Expressions problem solution
Java MD5 problem solution
Java SHA-256 problem solution

Pages

  • About US
  • Contact US
  • Privacy Policy

Follow US

  • YouTube
  • LinkedIn
  • Facebook
  • Pinterest
  • Instagram
©2026 Programmingoneonone | WordPress Theme by SuperbThemes