Breaking

Friday 1 September 2017

Complete Array reverse Method

                    Complete Array reverse Method


An array of N integers is passed as the input to the program and the program must print the array after reversing it.

Complete the method reverse so that the program can execute successfully.

Input Format:
The first line contains N.
The second line contains N integer values each separated by a space.

Output Format:
The first line contains reversed values of N integers separated by a space.

Boundary Conditions:
1 <= N <= 1000

Example Input/Output 1:
Input:
5
10 22 33 45 600

Output:
600 45 33 22 10
Code:
import java.util.Scanner;

public class Hello {

    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        int N = sc.nextInt();
        int arr[] = new int[N];
        for(int index=0; index < N; index++){
            arr[index] = sc.nextInt();
        }
        
        arr = reverse(arr);

        //Printing the output
         for(int index=0; index < N; index++){
             System.out.print(arr[index]+" ");
        }
    }
static int[] reverse(int[] arr) {
   int i=0,j=arr.length-1,temp;
    while(i<j)
   {
       temp=arr[i];
       arr[i]=arr[j];
       arr[j]=temp;
       i++;
       j--;
   }
   return arr;
}
}
Please do comment If u have any Queries!

No comments:

Post a Comment

Like