Reverse An Array whats wrong with this type of code? import java.io.*;
import java.util.*;
public class reverseAnArray2
{
    public static void main(String args[])throws IOException
    {
        Scanner sc  = new Scanner(System.in);
        System.out.println(&quot;Enter size&quot;);
        int  n = sc.nextInt();
        int a [] = new int [n] ;
        int b [] = new int [n] ;
        System.out.println(&quot;Enter elements&quot;);
        for(int i=0;i&lt;n;i++)
        {
        
            a[i]=sc.nextInt() ;
        }
        int size = n;
        System.out.println(&quot;Reverse array is : &quot;);
        int p=0;
        while(n&gt;=0 &amp;&amp; p&lt;n)
        {
            
            a[--n] = a[p] ;
            ++p;
        }
        for(int i =0;i&lt;size;i++)
        {
             System.out.println(+ a[i]);
        }
       
        
}
}

The output comes correct but only up to the last $n/2$ terms, rest of it gets repeated. The output was correct when I used another fresh array to store data. But that's not an optimal code. I want to reverse it without using another array.
Notice that you increment $p$, whilst also decrementing $n$. Since your condition in the while loop is that p &lt; n, then the while loop will only work for half the elements.
Also, note that you delete the last $n/2$ values, since you modify them without first reading them.