Geeks For Geeks Reverse Array
Clear------ 0.40 sec
Approach 1 : O(n) O(n) taking additional array
public static void main(String arg[]){
Scanner sc=new Scanner(System.in);
int t=sc.nextInt();
while(t -->0){
System.out.println("Enter size of Array : ");
int n=sc.nextInt();
int arr[]=new int[n];
System.out.println("Enter Elements : ");
for(int i=0;i<n;i++){
arr[i]=sc.nextInt();
}
System.out.println("Actual Array is : ");
for(int i=0;i<n;i++){
System.out.print(arr[i]+" ");
}
System.out.println();
int index=0;
int arr1[]=new int[n];
for(int i=n-1;i>-1;i--){
arr1[index]=arr[i];
index++;
}
System.out.println("Reversed Array is : ");
for(int i=0;i<n;i++){
System.out.print(arr1[i]+" ");
}
System.out.println();
}
}
<----------------------------------------------------------------------------------------------------------------------->
Cleared 0.37 sec
Approach 2 O(n) O(1) swapping by double pointers (one from start and one from end)
public static void main(String arg[]){
Scanner sc=new Scanner(System.in);
int t=sc.nextInt();
while(t -->0){
System.out.println("Enter size of Array : ");
int n=sc.nextInt();
int arr[]=new int[n];
System.out.println("Enter Elements : ");
for(int i=0;i<n;i++){
arr[i]=sc.nextInt();
}
System.out.println("Actual Array is : ");
for(int i=0;i<n;i++){
System.out.print(arr[i]+" ");
}
System.out.println();
int start=0;
int end=n-1;
while(start<end){
int temp=0;
temp=arr[end];
arr[end]=arr[start];
arr[start]=temp;
start++;
end--;
}
System.out.println("Reversed Array is : ");
for(int i=0;i<n;i++){
System.out.print(arr[i]+" ");
}
System.out.println();
}
}
<------------------------------------------------------------------------------------------------------------------------->
Cleared 0.47 sec
Approach 3 O(n) O(n) ArrayList and Collections.reverse
public static void main(String arg[]){
Scanner sc=new Scanner(System.in);
int t =sc.nextInt();
while(t -->0){
System.out.println("Enter size of Array : ");
int n=sc.nextInt();
int arr[]=new int[n];
System.out.println("Enter Elements : ");
for(int i=0;i<n;i++){
arr[i]=sc.nextInt();
}
System.out.println("Actual Array is : ");
for(int i=0;i<n;i++){
System.out.print(arr[i]+" ");
}
System.out.println();
ArrayList<Integer> aa=new ArrayList<Integer>();
for(int i=0;i<arr.length;i++){
aa.add(arr[i]);
}
Collections.reverse(aa);
for(int i=0;i<aa.size();i++){
System.out.print(aa.get(i)+" ");
}
System.out.println();
}
}
<------------------------------------------------------------------------------------------------------------------------->
Cleared 0.40 sec
Approach 4 O(n) O(1) swapping till half
public static void main(String arg[]){
Scanner sc=new Scanner(System.in);
int t=sc.nextInt();
while(t -->0){
System.out.println("Enter size of Array : ");
int n=sc.nextInt();
int arr[]=new int[n];
System.out.println("Enter Elements : ");
for(int i=0;i<n;i++){
arr[i]=sc.nextInt();
}
System.out.println("Actual Array is : ");
for(int i=0;i<n;i++){
System.out.print(arr[i]+" ");
}
System.out.println();
for(int i=0;i<n/2;i++){
int temp=0;
temp=arr[n-1-i];
arr[n-1-i]=arr[i];
arr[i]=temp;
}
System.out.println("Reverse Array is : ");
for(int i=0;i<n;i++){
System.out.print(arr[i]+" ");
}
System.out.println();
}
}
Comments
Post a Comment