forked from jainaman224/Algo_Ds_Notes
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Showing
3 changed files
with
91 additions
and
0 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,36 @@ | ||
//Program to rotate array Arr[] of size n by d Elements | ||
#include <stdio.h> | ||
|
||
void Rotate(int arr[], int r, int n) | ||
{ | ||
int temp[r]; | ||
for (int i = 0; i < r; i++) | ||
temp[i] = arr[i]; | ||
|
||
for (int i = r; i < n; i++) | ||
arr[i-r] = arr[i]; | ||
|
||
for (int i = n-r; i < n; i++) | ||
arr[i] = temp[i-(n-r)]; | ||
} | ||
|
||
int main() | ||
{ | ||
int arr[] = { 1, 2, 3, 4, 5 }; | ||
int r = 2; | ||
|
||
int n = sizeof(arr)/sizeof(arr[0]); | ||
|
||
Rotate(arr, r, n); | ||
|
||
for (int i = 0; i < n; i++) | ||
printf("%d ", arr[i]); | ||
|
||
return 0; | ||
} | ||
|
||
/* | ||
Input array: {1, 2, 3, 4, 5} | ||
Expected Output | ||
3 4 5 1 2 | ||
*/ |
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,34 @@ | ||
//Program to rotate array Arr[] of size n by d Elements | ||
#include<iostream> | ||
using namespace std; | ||
|
||
void leftRotate(int arr[], int n) | ||
{ | ||
int temp = arr[0], i; | ||
for (i = 0; i < n - 1; i++) | ||
arr[i] = arr[i + 1]; | ||
|
||
arr[i] = temp; | ||
} | ||
|
||
void Rotate(int arr[], int d, int n) | ||
{ | ||
for (int i = 0; i < d; i++) | ||
leftRotate(arr, n); | ||
|
||
for (int i = 0; i < n; i++) | ||
cout << arr[i] << " "; | ||
} | ||
|
||
int main() | ||
{ | ||
int arr[] = { 1, 2, 3, 4, 5}; | ||
int n = sizeof(arr) / sizeof(arr[0]); | ||
Rotate(arr, 2, n); | ||
return 0; | ||
} | ||
/* | ||
Input array: {1, 2, 3, 4, 5} | ||
Expected Output | ||
3 4 5 1 2 | ||
*/ |
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,21 @@ | ||
//Program to rotate array Arr[] of size n by d Elements | ||
def leftRotation(arr,n): | ||
temp = arr[0] | ||
for i in range(n-1): | ||
arr[i] = arr[i+1] | ||
arr[n-1] = temp | ||
|
||
def Rotate(arr,d,n): | ||
for i in range(d): | ||
leftRotation(arr,n) | ||
for i in range(n): | ||
print(arr[i],end=" ") | ||
|
||
arr = [1,2,3,4,5,6,7] | ||
Rotate(arr,2,7) | ||
|
||
/* | ||
Input array: {1, 2, 3, 4, 5, 6, 7 } | ||
Expected Output | ||
3 4 5 6 7 1 2 | ||
*/ |