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;

Comments

Popular posts from this blog

Java Date And Time HackerRank

Recursive Sequence Geeks For Geeks Problem Of The Day 12-02-2024

Minimum Indices HackerEarth