File tree Expand file tree Collapse file tree 3 files changed +147
-0
lines changed Expand file tree Collapse file tree 3 files changed +147
-0
lines changed Original file line number Diff line number Diff line change
1
+ // Left rotate an Array by D
2
+ // O(nD) O(1)
3
+ // approach 1
4
+
5
+
6
+ // i/p: n=7, 1 2 3 4 5 6 7, k = 3
7
+ // o/p: 4 5 6 7 1 2 3
8
+
9
+ // we simply call left rotate by one for D times
10
+
11
+ #include < bits/stdc++.h>
12
+ using namespace std ;
13
+
14
+ void leftrotatebyone (int arr[],int n)
15
+ {
16
+ int temp=arr[0 ]; // store 0th element in temp
17
+ for (int i=1 ;i<n;i++)
18
+ {
19
+ arr[i-1 ]=arr[i]; // move other element back bye one
20
+ }
21
+ arr[n-1 ]=temp; // insert temp element at end
22
+ }
23
+
24
+ void leftRotateByD (int arr[],int n,int d)
25
+ {
26
+ for (int i=0 ;i<d;i++)
27
+ leftrotatebyone (arr,n);
28
+ }
29
+
30
+ int main ()
31
+ {
32
+ int n,d;
33
+ cout<<" Enter the number of elements in array\n " ;
34
+ cin>>n;
35
+ int arr[n];
36
+ cout<<" Enter the elements in array\n " ;
37
+ for (int i=0 ;i<n;i++)
38
+ cin>>arr[i];
39
+ cout<<" Enter the number of rotations\n " ;
40
+ cin>>d;
41
+ leftRotateByD (arr,n,d);
42
+ cout<<" Array after " <<d<<" rotation\n " ;
43
+ for (int i=0 ;i<n;i++)
44
+ cout<<arr[i]<<" " ;
45
+ return 0 ;
46
+ }
Original file line number Diff line number Diff line change
1
+ // Left rotate an Array by D
2
+ // O(n) O(d)
3
+ // approach 2
4
+
5
+ // i/p: n=7, 1 2 3 4 5 6 7, k = 3
6
+ // o/p: 4 5 6 7 1 2 3
7
+
8
+
9
+ // 1st we copy D elements in arr[D]
10
+ // 2nd we move arr[i] one by one back
11
+ // 3rd insert arr[D] in arr[i] [n-d+i]
12
+
13
+ #include < bits/stdc++.h>
14
+ using namespace std ;
15
+
16
+ int leftRotateByD (int arr[],int n,int d)
17
+ {
18
+ int temp[d];
19
+ for (int i=0 ;i<d;i++) // O(D)
20
+ temp[i]=arr[i];
21
+
22
+ for (int i=d;i<n;i++) // O(n-D)
23
+ arr[i-d]=arr[i]; // i-d will place element at 0th
24
+
25
+ for (int i=0 ;i<d;i++) // O(D)
26
+ arr[n-d+i]=temp[i]; // 5-3+1 =3, 4 ,5
27
+
28
+ // d + n - d + d = n + d = n (d is negligible)
29
+ }
30
+ int main ()
31
+ {
32
+ int n,d;
33
+ cout<<" Enter the number of elements in array\n " ;
34
+ cin>>n;
35
+ int arr[n];
36
+ cout<<" Enter the elements in array\n " ;
37
+ for (int i=0 ;i<n;i++)
38
+ cin>>arr[i];
39
+ cout<<" Enter the number of rotations\n " ;
40
+ cin>>d;
41
+ leftRotateByD (arr,n,d);
42
+ cout<<" Array after " <<d<<" rotation\n " ;
43
+ for (int i=0 ;i<n;i++)
44
+ cout<<arr[i]<<" " ;
45
+ return 0 ;
46
+ }
47
+
Original file line number Diff line number Diff line change
1
+ // Left rotate an Array by D
2
+ // O(n) O(1)
3
+ // approach 3
4
+
5
+ // i/p: n=7, 1 2 3 4 5 6 7, k = 3
6
+ // o/p: 4 5 6 7 1 2 3
7
+
8
+ // 1st we reverse D
9
+ // then reverse D to N
10
+ // and finally we reverse whole the array
11
+ // so we get as desire output
12
+
13
+
14
+ #include < bits/stdc++.h>
15
+ using namespace std ;
16
+
17
+ void reverse (int arr[],int low,int high)
18
+ {
19
+ while (low<high)
20
+ {
21
+ swap (arr[low],arr[high]); // reverse till low and high overlap
22
+ low++;
23
+ high--;
24
+ }
25
+ }
26
+
27
+ void leftRotateByD (int arr[],int n,int d)
28
+ { // 1 2 3 4 5 d=2
29
+
30
+ reverse (arr,0 ,d-1 ); // call to reverse start to D th element 2 1 3 4 5
31
+
32
+ reverse (arr,d,n-1 ); // call to reverse for D th to N-1 number 2 1 5 4 3
33
+
34
+ reverse (arr,0 ,n-1 ); // call to reverse for 0th to N-1 number 3 4 5 1 2
35
+
36
+ }
37
+
38
+ int main ()
39
+ {
40
+ int n,d;
41
+ cout<<" Enter the number of elements in array\n " ;
42
+ cin>>n;
43
+ int arr[n];
44
+ cout<<" Enter the elements in array\n " ;
45
+ for (int i=0 ;i<n;i++)
46
+ cin>>arr[i];
47
+ cout<<" Enter the number of rotations\n " ;
48
+ cin>>d;
49
+ leftRotateByD (arr,n,d);
50
+ cout<<" Array after " <<d<<" rotation\n " ;
51
+ for (int i=0 ;i<n;i++)
52
+ cout<<arr[i]<<" " ;
53
+ return 0 ;
54
+ }
You can’t perform that action at this time.
0 commit comments