HackerRank Java Arrays- Dynamic Array




https://www.hackerrank.com/challenges/dynamic-array/problem?isFullScreen=true&h_r=next-challenge&h_v=zen



  1. import java.util.Scanner;
  2. import java.util.ArrayList;
  3. public class Solution {
  4. public static void main(String[] args) {
  5. Scanner scan = new Scanner(System.in);
  6. int N = scan.nextInt();
  7. int Q = scan.nextInt();
  8. int lastAns = 0;
  9. /* Create 2-D ArrayList */
  10. ArrayList<ArrayList<Integer>> lists = new ArrayList();
  11. for (int i = 0; i < N; i++) {
  12. lists.add(new ArrayList<Integer>());
  13. }
  14. /* Perform Q Queries */
  15. for (int i = 0; i < Q; i++) {
  16. int q = scan.nextInt();
  17. int x = scan.nextInt();
  18. int y = scan.nextInt();
  19. ArrayList<Integer> seq = lists.get((x ^ lastAns) % N);
  20. if (q == 1) {
  21. seq.add(y);
  22. } else if (q == 2) {
  23. lastAns = seq.get(y % seq.size());
  24. System.out.println(lastAns);
  25. }
  26. }
  27. scan.close();
  28. }
  29. }
download android app

Popular HackerRank Arrays-java Solutions








codesadda.com

Codesadda.com is your home of programming solutions, tutorials, video tutorials and much more. Sign Up for our weekly newsletter to get update about new content.

Like us on Facebook | Connect with us on LinkedIn | Subscribe our Channel on Youtube