-2

Am I leaning in the right direction with my code? I'm currently working on hacker rank and am on the easy section of data structures yet I still am confused about how to reverse this array!

Out of all my attempts, I like these two that I did. Check it out.

one attempt:
one attempt

two attempts:
two attempts

Andreas
  • 154,647
  • 11
  • 152
  • 247
  • 1
    You can directly, paste your code here, which is what you've tried. It will make it easy for others to understand your problem without much navigation. – taurus05 Jan 15 '20 at 05:24
  • 1
    HackerRank is for evaluating your skills for a job interview. We use it at my company, and it's meant to test candidates fairly and unbiasedly. IMO, you should be solving this one on your own. – entpnerd Jan 15 '20 at 05:31
  • That's cool you guys use it at your company! I'm using it to practice and am not working for a company so it should be fine. – PlayerUnknown_12 Jan 15 '20 at 05:39
  • 1
    @PlayerUnknown_12 , you are cool as long as you seek suggestions not answers, from my point of view your question is valid but it has nothing that SO community can help you in this. – Vishwa Ratna Jan 15 '20 at 05:40
  • Right, I asked my questions hoping to be lead in the right direction for fixing my code and that is what my question is intended for. For future questions I'll see if there are other open forums on hackerranks website. – PlayerUnknown_12 Jan 15 '20 at 05:45

2 Answers2

-1

You should be walking down the input array, then populating the target array in the opposite direction:

public static int[] reverseArray(int[] a) {
    int[] b = new int[a.length];

    for (int i=0; i < a.length; ++i) {
        b[i] = a[a.length-i-1];
    }

    return b;
}

Just for fun, here is way to reverse the order of an original array, without using any extra storage:

// give input int[] a
for (int i=0; i < a.length / 2; ++i) {
    a[i] = a[a.length-i-1];
}

Note that this approach just walks down half of the array, and swaps elements about the median.

Tim Biegeleisen
  • 502,043
  • 27
  • 286
  • 360
-1

You can reverse array in two ways

  1. Either by fetching elements from last to start index and putting elements into new array

for example

int[] b = new int[n];
for(int i = n-1; i>=0 ; i--){
   n[i] = arrayTobeReverse[i];
}

for more you can visit : https://www.geeksforgeeks.org/reverse-an-array-in-java/

  1. By using Collection.reverse() method first you have to convert array into List using Arrays.asList(array) you can create List using array and then reverse the List. for example :

    import java.util.*; 
    
    
    
    public class ReverseDemo 
    { 
        public static void main(String[] args) 
        { 
            // Let us create a list of strings 
            List<String>  mylist = new ArrayList<String>(); 
            mylist.add("practice"); 
            mylist.add("code"); 
            mylist.add("quiz"); 
            mylist.add("geeksforgeeks"); 
    
            System.out.println("Original List : " + mylist); 
    
            // Here we are using reverse() method 
            // to reverse the element order of mylist 
            Collections.reverse(mylist); 
    
            System.out.println("Modified List: " + mylist); 
        } 
    } 
    
  • In second you have to return after the for loop because return b[i] = a[i]; will return the array without all element is reversed and the condition i > 0 its means i is iterated up to 1, not zero (index is started from 0) and started from n -1 so the last element is not included into the reversed array – Aman Kumar Soni Jan 15 '20 at 06:29