1.Array rotation
Array rotation
Write a function rotate(ar[], d, n) that rotates arr[] of size n by d elements.

Rotation of the above array by 2 will make array

Input:
7
2
1 2 3 4 5 6 7
Expected Output:
3 4 5 6 7 1 2
Method 1:
1. store d elements in temporary array of size d.
2. Shift all other elements.
3. Restore temp array at the last.
Time complexity O(n)
Auxiliary Space: O(d)
Code:
-------------------------------------------------------------------------------------------------------
import java.util.*;
import java.lang.*;
import java.io.*;
class ArrayRotation
{
public static void main (String[] args) throws java.lang.Exception
{
Scanner in = new Scanner(System.in);
int n = in.nextInt();
int d = in.nextInt();
int arr[] = new int[n];
int temp[] = new int[d];
int count = 0;
for(int i=0; i<n; i++){
arr[i] = in.nextInt();
}
for (int i=0; i<n; i++){
if(i<d)
temp[i]=arr[i];
else if(i>=d && i< n-d)
arr[i-d]= arr[i];
else {//i is greater than or equal to n-d
arr[i-d] = arr[i];
arr[i] = temp[count];
count ++;
}
}
for(int i=0; i<n; i++){
System.out.print(arr[i]+" ");
}
}
}
-------------------------------------------------------------------------------------------------------
Method 2 :
Rotate the array one by one. that is
1. save first element in temp.
2. shift left i.e, arr[i-1] = arr[i] all elements.
3. Restore first element at the last.
Time complexity: O(n*d)
Auxiliary Space: O(1)
Code:
-------------------------------------------------------------------------------------------------------
import java.util.*;
import java.lang.*;
import java.io.*;
class ArrayRotation
{
public static void main (String[] args) throws java.lang.Exception
{
Scanner in = new Scanner(System.in);
int n = in.nextInt();
int d = in.nextInt();
int arr[] = new int[n];
for(int i=0; i<n; i++){
arr[i] = in.nextInt();
}
for(int j=0; j<d; j++){
int temp = arr[0];
for(int i=0; i<n-1; i++){
arr[i] = arr[i+1];
}
arr[n-1] = temp;
}
for(int i=0; i<n; i++){
System.out.print(arr[i]+" ");
}
}
}
-----------------------------------------------------------------------------------------------------------
Write a function rotate(ar[], d, n) that rotates arr[] of size n by d elements.
Rotation of the above array by 2 will make array
Input:
7
2
1 2 3 4 5 6 7
Expected Output:
3 4 5 6 7 1 2
Method 1:
1. store d elements in temporary array of size d.
2. Shift all other elements.
3. Restore temp array at the last.
Time complexity O(n)
Auxiliary Space: O(d)
Code:
-------------------------------------------------------------------------------------------------------
import java.util.*;
import java.lang.*;
import java.io.*;
class ArrayRotation
{
public static void main (String[] args) throws java.lang.Exception
{
Scanner in = new Scanner(System.in);
int n = in.nextInt();
int d = in.nextInt();
int arr[] = new int[n];
int temp[] = new int[d];
int count = 0;
for(int i=0; i<n; i++){
arr[i] = in.nextInt();
}
for (int i=0; i<n; i++){
if(i<d)
temp[i]=arr[i];
else if(i>=d && i< n-d)
arr[i-d]= arr[i];
else {//i is greater than or equal to n-d
arr[i-d] = arr[i];
arr[i] = temp[count];
count ++;
}
}
for(int i=0; i<n; i++){
System.out.print(arr[i]+" ");
}
}
}
Method 2 :
Rotate the array one by one. that is
1. save first element in temp.
2. shift left i.e, arr[i-1] = arr[i] all elements.
3. Restore first element at the last.
Time complexity: O(n*d)
Auxiliary Space: O(1)
Code:
-------------------------------------------------------------------------------------------------------
import java.util.*;
import java.lang.*;
import java.io.*;
class ArrayRotation
{
public static void main (String[] args) throws java.lang.Exception
{
Scanner in = new Scanner(System.in);
int n = in.nextInt();
int d = in.nextInt();
int arr[] = new int[n];
for(int i=0; i<n; i++){
arr[i] = in.nextInt();
}
for(int j=0; j<d; j++){
int temp = arr[0];
for(int i=0; i<n-1; i++){
arr[i] = arr[i+1];
}
arr[n-1] = temp;
}
for(int i=0; i<n; i++){
System.out.print(arr[i]+" ");
}
}
}
-----------------------------------------------------------------------------------------------------------
Comments
Post a Comment