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