2

I am trying to write a program that will reverse an array and print it in a single row with a single space separating each elements of the array.

My code:

static int[] reverseArray(int[] array) {
    int end = array.length-1;
    for(int i=end; i>=0; i--) {
        a[i] = a[i] + " ";
    }
    return String.valueOf(a);
}

The above code is throwing me an error saying:

incompatible types: String cannot be converted to int[]

Sample input:

1 4 3 2

Sample output:

 2 3 4 1
1
  • Are you trying to reverse an array or are you trying to print the contents of an array in reverse order? The code you wrote seems to try to do both Apr 11, 2018 at 9:46

8 Answers 8

2

As a starter, the variable a is undefined, so you need a declaration like so:

int[] a = new int[array.length];

Then, in the loop, you can't assign a string to an element of the int array a. Also, the method returns an array, not a String, so there is no need to use String operations.

One way of implementing that method, keeping the provided for loop, would then be:

static int[] reverseArray(int[] array) {
    int[] a = new int[array.length];
    int end = array.length - 1;
    for (int i = end; i >= 0; i--) {
        a[i] = array[end - i];
    }
    return a;
}
0
0
 a[i] = a[i] + " ";

This is where you are doing wrong. Here array a[] is a type of int where you are trying to hold String into it.

return String.valueOf(a);

In this line too, you are trying to Store an array object. Which will not return your expected results.

1
0

You code contains some errors:

  1. Where does a does come from ? What type is it ?
  2. Your method says it returns int[] but you try to return a String, be consistent.

Here's a modified version of your code:

    static String reverseArray(int[] array) {
        String result = "";
        int end = array.length-1;
        for(int i=end; i>=0; i--) {
            result += array[i] + " ";
        }
        return result;
    }
1
  • 1. Array a is an integer array and the input is taken from Scanner class and then passed to the reverseArray function. 2. My function should return int[] only, not String. Sorry for the confusion. Apr 11, 2018 at 9:54
0
static int[] reverseArray(int[] array) {
    int end = array.length-1;
    int a = new int[array.length];
    for(int i=end; i>=0; i--) {
        a[end - i] = array[i];
    }
    return a;
}
1
0

You can also create an another array and store the elements in another array using while loops and print that second array. Here is the way I tried doing :

int[] revArr = new int[n];
int i = n-1;
int j = 0;
while(i>=0) {
     while(j<n-i){
         revArr[j] = arr[i];
         System.out.print(revArr[j] +" ");
         j++;
     }
i--;
}
0
#Aray reversing in python

#!/bin/python3

if __name__ == '__main__':
    n = int(input())
    arr = list(map(int, input().rstrip().split()))
    lst_ele=len(arr)
    for i in range(len(arr)):
        lst_ele-=1
        print(arr[lst_ele],end=' ')
            
    
    
        
1
  • This is an example of array reversing in python. HACKERRANK DAY-7 challenge python. Jul 8, 2020 at 17:27
0

This code should work for Python 3:

if __name__ == '__main__':
    n = int(input())
    arr = list(map(int, input().rstrip().split()))
    k = len(arr)
    for i in range(0,int(k)):
        k-=1
        print(arr[k], end=' ')

If it doesn't, make sure the indentation is correct.

0

A more optimized version could be:

for i in reversed(range (0, len(arr))):
    print(arr[i], end=' ')

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.