forked from I-RoshanKumar/Beginner_Hactoberfest2022
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathLeft_rotate_of_an_array_by_d_digits.cpp
More file actions
62 lines (48 loc) · 1.01 KB
/
Left_rotate_of_an_array_by_d_digits.cpp
File metadata and controls
62 lines (48 loc) · 1.01 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
#include <bits/stdc++.h>
using namespace std;
void leftRotateByOne(int arr[], int n){
int temp = arr[0];
for(int i=1; i<n; i++){
arr[i-1] = arr[i];
}
arr[n-1] = temp;
}
void leftRotate(int arr[], int n, int d){
for(int i=0; i<d; i++){
leftRotateByOne(arr, n);
}
}
// Better Method :-
void LeftRotate(int arr[], int n, int d){
int temp[d];
for(int i=0; i<d; i++)
temp[i] = arr[i];
for(int i=d; i<n; i++){
arr[i-d] = arr[i];
}
for(int i=0; i<d; i++)
arr[n - d + i] = temp[i];
}
// Efficient method :-
void reverse(int arr[], int low, int high){
while(low < high){
swap(arr[low], arr[high]);
low++;
high--;
}
}
void LeftRotatE(int arr[], int n, int d){
reverse(arr, 0, d-1);
reverse(arr, d, n-1);
reverse(arr, 0, n-1);
}
int main()
{
int n = 5;
int d = 2;
int arr[] = {1, 2, 3, 4, 5};
LeftRotatE(arr, n, d);
for(int i=0; i<n; i++)
cout<<arr[i]<<" ";
return 0;
}