Posts

Showing posts with the label Collections

Sparse Arrays HackerRank

1.USING ARRAYLIST import  java.io.*; import  java.math.*; import  java.security.*; import  java.text.*; import  java.util.*; import  java.util.concurrent.*; import  java.util.function.*; import  java.util.regex.*; import  java.util.stream.*; import   static  java.util.stream.Collectors.joining; import   static  java.util.stream.Collectors.toList; public   class  Solution {      public   static   void  main(String[] args)  throws  IOException {         Scanner sc= new  Scanner(System.in);          int  n=sc.nextInt();         String str[]= new  String[n];         ArrayList<String> aa= new  ArrayList<String>();        ...

2D Array - DS HACKKERANK

  import  java.io.*; import  java.math.*; import  java.security.*; import  java.text.*; import  java.util.*; import  java.util.concurrent.*; import  java.util.function.*; import  java.util.regex.*; import  java.util.stream.*; import   static  java.util.stream.Collectors.joining; import   static  java.util.stream.Collectors.toList; class  Result {      /*      * Complete the 'hourglass sum function below.      *      * The function is expected to return an INTEGER.      * The function accepts 2D_INTEGER_ARRAY arr as a parameter.      */          //Time O(1) as we know every time we have to iterate the loop //through value 4 as array size w...

Arrays DS HACKKERANK

  public   static  List<Integer> reverseArray(List<Integer> a) { Approach 1 Time O(N) Space O(N)        List<Integer> aa= new  ArrayList<Integer>();         for ( int  i= 0 ;i<a.size();i++){            aa.add(a.get(i));        }         Collections.reverse(aa);          return  aa;     } /* Given is the list so we have two choices 1.either to use arrays for storing data 2.either to use ArrayList for storing data The thing is we need to return the List in the end so we can use directly the ArrayList. */ Approach 2 Time O(N) Space O(1)     Collections.reverse(a);     return a;