Skip to content

Commit 096755a

Browse files
committed
Add day 24
1 parent 4148c81 commit 096755a

File tree

5 files changed

+96
-0
lines changed

5 files changed

+96
-0
lines changed

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,7 @@ Read [CONTRIBUTING.md](./CONTRIBUTING.md) for contribution guidelines.
4747
21. [Day 21 -- Pair Sum N and Max Subarray Sum](./day21) -- [http://codetoexpress.tech/dc/day21/](http://codetoexpress.tech/dc/day21/)
4848
22. [Day 22 -- Common Elements Search](./day22) -- [http://codetoexpress.tech/dc/day22/](http://codetoexpress.tech/dc/day22/)
4949
23. [Day 23 -- Combination Sum](./day23) -- [http://codetoexpress.tech/dc/day23/](http://codetoexpress.tech/dc/day23/)
50+
24. [Day 24 -- Array Circular Rotation](./day24) -- [http://codetoexpress.tech/dc/day24/](http://codetoexpress.tech/dc/day24/)
5051

5152
## [More Problems](./BONUS/README.md)
5253

day24/Daily Codes.png

139 KB
Loading

day24/JavaScript/isRotation_madhav.js

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
/**
2+
* @author MadhavBahlMD
3+
* @date 22/01/2019
4+
* Method - Circular Search -- Time = O(n)
5+
*/
6+
7+
function isRotation (arr1, arr2) {
8+
let len1 = arr1.length,
9+
len2 = arr2.length;
10+
11+
// return false if lengths are different
12+
if (len1 !== len2) return false;
13+
14+
// find position of first element of array2 in array 2
15+
let isRotated = true,
16+
pos = arr1.indexOf (arr2[0]);
17+
18+
// Return false if element not found
19+
if (pos < 0) return false;
20+
21+
// Check all the other numbers
22+
for (let i=0; i<len1; i++) {
23+
if (arr1[(pos+i)%len1] !== arr2[i]) {
24+
isRotated = false;
25+
break;
26+
}
27+
}
28+
29+
return isRotated;
30+
}
31+
32+
console.log (isRotation ([1, 2, 3, 4, 5, 6, 7], [4, 5, 6, 7, 1, 2, 3])); // true
33+
console.log (isRotation ([1, 2, 3, 4, 5, 6, 7], [7, 1, 2, 3])); // false
34+
console.log (isRotation ([1, 2, 3, 4, 5, 6], [6, 5, 4, 3, 2, 1])); // false

day24/README.md

Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
![cover](./cover.png)
2+
3+
# Day 24 - Array Series Part 7: Array Circular rotation
4+
5+
**Question** -- Given 2 arrays (with no duplicate entries in any array), find whether one can be formed by rotating the other by 'n' elements. n < array length
6+
7+
**Example**
8+
9+
```
10+
input: arr1 = [1, 2, 3, 4, 5, 6, 7], arr2 = [4, 5, 6, 7, 1, 2, 3]
11+
output: true
12+
13+
input: arr1 = [1, 2, 3, 4, 5, 6], arr2 = [6, 5, 4, 3, 2, 1]
14+
output: false
15+
16+
input: arr1 = [1, 4, 2, 3, 6, 7, 9], arr2 = [5, 6, 2, 1, 9, 4]
17+
output: false
18+
```
19+
20+
![ques](./ques.png)
21+
22+
## JavaScript Implementation
23+
24+
### [Solution](./JavaScript/isRotation_madhav.js)
25+
26+
```js
27+
/**
28+
* @author MadhavBahlMD
29+
* @date 22/01/2019
30+
* Method - Circular Search -- Time = O(n)
31+
*/
32+
33+
function isRotation (arr1, arr2) {
34+
let len1 = arr1.length,
35+
len2 = arr2.length;
36+
37+
// return false if lengths are different
38+
if (len1 !== len2) return false;
39+
40+
// find position of first element of array2 in array 2
41+
let isRotated = true,
42+
pos = arr1.indexOf (arr2[0]);
43+
44+
// Return false if element not found
45+
if (pos < 0) return false;
46+
47+
// Check all the other numbers
48+
for (let i=0; i<len1; i++) {
49+
if (arr1[(pos+i)%len1] !== arr2[i]) {
50+
isRotated = false;
51+
break;
52+
}
53+
}
54+
55+
return isRotated;
56+
}
57+
58+
console.log (isRotation ([1, 2, 3, 4, 5, 6, 7], [4, 5, 6, 7, 1, 2, 3])); // true
59+
console.log (isRotation ([1, 2, 3, 4, 5, 6, 7], [7, 1, 2, 3])); // false
60+
console.log (isRotation ([1, 2, 3, 4, 5, 6], [6, 5, 4, 3, 2, 1])); // false
61+
```

day24/carbon.png

802 KB
Loading

0 commit comments

Comments
 (0)