-
Notifications
You must be signed in to change notification settings - Fork 8
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
e64c51f
commit a3536b9
Showing
5 changed files
with
171 additions
and
68 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
/* | ||
* ARRAY ROTATION USING TEMP ARRAY | ||
* LEFT ROTATE TO 2 | ||
*/ | ||
import java.util.*; | ||
public class ArrayRotation1 | ||
{ | ||
public static void main(String[] args) | ||
{ | ||
Scanner in = new Scanner(System.in); | ||
int[] arr = {1,2,3,4,5,6,7}; | ||
int shift=2; | ||
|
||
rotate(arr,shift,arr.length); | ||
|
||
in.close(); | ||
} | ||
public static void rotate(int[] arr,int d,int n) | ||
{ | ||
int[] temp = new int[d]; | ||
//copy arr 2 elements to temp | ||
for(int i=0;i<temp.length;i++) | ||
{ | ||
temp[i] = arr[i]; | ||
} | ||
//shifting array | ||
for(int i=0;i<arr.length-d;i++) | ||
{ | ||
arr[i]=arr[i+d]; | ||
} | ||
//copy temp array to array | ||
for(int i=0;i<d;i++) | ||
{ | ||
arr[i+arr.length-d] =temp[i]; | ||
} | ||
for(int i=0;i<arr.length;i++) | ||
{ | ||
System.out.print(arr[i]+" "); | ||
} | ||
System.out.println(); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
/* | ||
* ARRAY ROTATION USING ONE ELEMENT | ||
* LEFT ROTATE BY 2 | ||
*/ | ||
import java.util.*; | ||
public class ArrayRotation2 | ||
{ | ||
public static void main(String[] args) | ||
{ | ||
Scanner in = new Scanner(System.in); | ||
int[] arr={1,2,3,4,5,6,7}; | ||
int shift=3; | ||
rotate(arr, shift, arr.length); | ||
|
||
//printing array | ||
for(int i=0;i<arr.length;i++) | ||
{ | ||
System.out.print(arr[i]+" "); | ||
} | ||
in.close(); | ||
} | ||
public static void rotate(int[] arr,int d,int n) | ||
{ | ||
//we can shift one by one | ||
for(int i=0;i<d;i++) | ||
{ | ||
int temp = arr[0]; | ||
for(int j=0;j<n-1;j++) | ||
{ | ||
arr[j]=arr[j+1]; | ||
} | ||
arr[n-1]=temp; | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,44 @@ | ||
abstract class jugglingAlgo | ||
{ | ||
public static void main(String[] args) | ||
{ | ||
int[] arr = {1,2,3,4,5,6,7,8,9}; | ||
leftRotation(arr,3); | ||
for(int i=0;i<arr.length;i++) | ||
{ | ||
System.out.print(arr[i] + " "); | ||
} | ||
System.out.println(); | ||
} | ||
public static int[] leftRotation(int[] arr,int k) | ||
{ | ||
int set = gcd(arr.length, k); | ||
|
||
for(int i=0;i<set;i++) | ||
{ | ||
int temp = arr[i]; | ||
int j=i; | ||
while(true) | ||
{ | ||
int d = (j+k) % arr.length; | ||
|
||
if(d==i) | ||
{ | ||
break; | ||
} | ||
arr[j] = arr[d]; | ||
j=d; | ||
} | ||
arr[j] = temp; | ||
} | ||
return arr; | ||
} | ||
public static int gcd(int a,int b) | ||
{ | ||
if(b==0) | ||
{ | ||
return a; | ||
} | ||
return gcd(b,a%b); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.