-3
public class Solution {

public static void main(String []args) {
    Scanner in = new Scanner(System.in);
    int numberOfValues = in.nextInt();
    int valueArray[] = new int[numberOfValues];
    int sizeOfArray = valueArray.length; 

    for(int i=0; i < sizeOfArray; i++)
        valueArray[i]=in.nextInt();
   // System.out.print(valueArray.length);
    if(sizeOfArray%2==0)
        {
        //for(i=0,j=valueArray.length-1;i<(sizeOfArray/2)&&j>((sizeOfArray/2)+1);i++,j--)
        for(int i=0,j=sizeOfArray-1;i<sizeOfArray/2;i++,j--)
            {               
                int temp = valueArray[i];
                temp = valueArray[j];
                valueArray[j]=temp;
            }

        }
     for(int i=0; i < sizeOfArray; i++)
        System.out.print(valueArray[i]+" ");
    }
}

Help me to solve this. I couldnt get the reversed elements

Sample output: 4 1 4 3 2

Your Output (stdout)

1 4 3 2

Expected Output

2 3 4 1

Compiler Message

Wrong Answer

  • 2
    Possible duplicate of [How do I reverse an int array in Java?](http://stackoverflow.com/questions/2137755/how-do-i-reverse-an-int-array-in-java) – Alexander V. Feb 02 '17 at 06:02

1 Answers1

1
int temp = valueArray[i];
temp = valueArray[j];

You just set the same variable twice in a row. That's often a good sign that you're doing something wrong on those lines. I think you wanted to set valueArray[i] on that second line.

D M
  • 1,410
  • 1
  • 8
  • 12