Skip to content

Commit 85f18c1

Browse files
committed
Day 44 : array rotation - programming pearls
1 parent d487cd3 commit 85f18c1

File tree

5 files changed

+95
-6
lines changed

5 files changed

+95
-6
lines changed

README.md

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -4,9 +4,9 @@
44

55
| Current Status| Stats |
66
| :------------: | :----------: |
7-
| Total Problems | 62 |
8-
| Current Streak | 43 |
9-
| Longest Streak | 43 ( August 17, 2015 - September 28, 2015 ) |
7+
| Total Problems | 63 |
8+
| Current Streak | 44 |
9+
| Longest Streak | 44 ( August 17, 2015 - September 29, 2015 ) |
1010

1111
</center>
1212

@@ -94,13 +94,16 @@ Include contains single header implementation of data structures and some algori
9494
| Implementation of Robin-Karp algorithm for string search | [robinKarpStringMatching.cpp](string_problmes/robinKarpStringMatching.cpp) |
9595
| Find next permutation of a given string, ie. rearrange the given string sucht a way that is next lexicographically greater string than given string | [next_permutation.cpp](string_problems/next_permutation.cpp)|
9696

97+
### Common Data Structure and logic problems
98+
| Print the contents of matrix in a spiral order | [matrix_spiral_print.cpp](common_ds_problems/matrix_spiral_print.cpp)
99+
| Given a M x N matrix, rotate it by R rotations anticlockwise, and show the resulting matrix. | [rotate_matrix.cpp](common_ds_problems/rotate_matrix.cpp)|
100+
| Rotate an array by r elements ( left or right ) | [array_rotation.cpp](common_ds_problems/array_rotation.cpp)
101+
97102
### Math Problems
98103
| Problem | Solution |
99104
| :------------ | :----------: |
100105
| Print all the permutations of a string. Example: Permutations of ABC are ABC, ACB, BCA, BAC, CAB, CBA | [string_permutations.cpp] (math_problems/string_permutations.cpp) |
101106
| Euclidean algorithm to find greatest common divisor of two numbers. (Iterative and recursive)|[gcd.cpp](math_problems/gcd.cpp)|
102-
| Print the contents of matrix in a spiral order | [matrix_spiral_print.cpp](math_problems/matrix_spiral_print.cpp)
103-
| Given a M x N matrix, rotate it by R rotations anticlockwise, and show the resulting matrix. | [rotate_matrix.cpp](math_problems/rotate_matrix.cpp)|
104107

105108
### Stack Problems
106109
| Problem | Solution |

common_ds_problems/array_rotation.cpp

Lines changed: 85 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,85 @@
1+
#include <iostream>
2+
#include <cassert>
3+
4+
5+
6+
void swap( int & a, int & b)
7+
{
8+
int temp = a;
9+
a = b;
10+
b = temp;
11+
}
12+
13+
/**
14+
* reverse an array from index 'start' to index 'end'
15+
* @param arr [The array]
16+
* @param start [The start index]
17+
* @param end [The end index]
18+
*/
19+
void reverse( int * arr, int start, int end )
20+
{
21+
while( start < end ) {
22+
swap(arr[start], arr[end]);
23+
++start;
24+
--end;
25+
}
26+
}
27+
28+
29+
/**
30+
* [leftRotate - rotate an array of length n with r rotations in left direction
31+
* @param arr - the array
32+
* @param n - size of array
33+
* @param r - n number of rotations
34+
*/
35+
void leftRotate( int* arr, int n, int r )
36+
{
37+
r = r % n;
38+
reverse( arr, 0, r-1);
39+
reverse( arr, r, n-1);
40+
reverse( arr, 0, n-1);
41+
}
42+
43+
44+
/**
45+
* [rightRotate - rotate an array of length n with r rotations in left direction
46+
* @param arr - the array
47+
* @param n - size of array
48+
* @param r - n number of rotations
49+
*/
50+
void rightRotate( int* arr, int n, int r )
51+
{
52+
r = r % n;
53+
reverse( arr, 0, n-1);
54+
reverse( arr, 0, r-1);
55+
reverse( arr, r, n-1);
56+
}
57+
58+
59+
int main()
60+
{
61+
int n, r, d;
62+
std::cout << "Enter size of array :";
63+
std::cin >> n;
64+
std::cout << "Enter the contents of array:";
65+
int * arr = new int[n];
66+
for ( int i = 0; i < n; ++i ) {
67+
std::cin >> arr[i];
68+
}
69+
std::cout << "Enter number of rotations you need:";
70+
std::cin >> r;
71+
std::cout << "Direction (left = 0, right = 1):";
72+
std::cin >> d;
73+
assert((d == 0) || (d == 1));
74+
if ( d == 0 ) {
75+
leftRotate(arr, n, r);
76+
} else {
77+
rightRotate(arr, n, r);
78+
}
79+
std::cout << "The array after rotation:\n";
80+
for ( int i = 0; i < n; ++i ) {
81+
std::cout << arr[i] << " ";
82+
}
83+
std::cout << std::endl;
84+
return 0;
85+
}

linked_list_problems/findIntersectionPointOfLists.cpp

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
*/
55

66
#include <iostream>
7+
#include <cmath>
78

89
struct Node {
910
int data;
@@ -40,7 +41,7 @@ Node * mergePoint( Node * head1, Node * head2 )
4041
Node * ptr1 = ( len1 > len2 ) ? head1 : head2;
4142
Node * ptr2 = ( len1 > len2 ) ? head2 : head1;
4243
int i = 0;
43-
while ( i < abs(len1 - len2) && ptr1 ) {
44+
while ( i < std::abs(len1 - len2) && ptr1 ) {
4445
ptr1 = ptr1->next;
4546
++i;
4647
}

0 commit comments

Comments
 (0)