-
Notifications
You must be signed in to change notification settings - Fork 153
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
4148c81
commit 096755a
Showing
5 changed files
with
96 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
/** | ||
* @author MadhavBahlMD | ||
* @date 22/01/2019 | ||
* Method - Circular Search -- Time = O(n) | ||
*/ | ||
|
||
function isRotation (arr1, arr2) { | ||
let len1 = arr1.length, | ||
len2 = arr2.length; | ||
|
||
// return false if lengths are different | ||
if (len1 !== len2) return false; | ||
|
||
// find position of first element of array2 in array 2 | ||
let isRotated = true, | ||
pos = arr1.indexOf (arr2[0]); | ||
|
||
// Return false if element not found | ||
if (pos < 0) return false; | ||
|
||
// Check all the other numbers | ||
for (let i=0; i<len1; i++) { | ||
if (arr1[(pos+i)%len1] !== arr2[i]) { | ||
isRotated = false; | ||
break; | ||
} | ||
} | ||
|
||
return isRotated; | ||
} | ||
|
||
console.log (isRotation ([1, 2, 3, 4, 5, 6, 7], [4, 5, 6, 7, 1, 2, 3])); // true | ||
console.log (isRotation ([1, 2, 3, 4, 5, 6, 7], [7, 1, 2, 3])); // false | ||
console.log (isRotation ([1, 2, 3, 4, 5, 6], [6, 5, 4, 3, 2, 1])); // false |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,61 @@ | ||
![cover](./cover.png) | ||
|
||
# Day 24 - Array Series Part 7: Array Circular rotation | ||
|
||
**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 | ||
|
||
**Example** | ||
|
||
``` | ||
input: arr1 = [1, 2, 3, 4, 5, 6, 7], arr2 = [4, 5, 6, 7, 1, 2, 3] | ||
output: true | ||
input: arr1 = [1, 2, 3, 4, 5, 6], arr2 = [6, 5, 4, 3, 2, 1] | ||
output: false | ||
input: arr1 = [1, 4, 2, 3, 6, 7, 9], arr2 = [5, 6, 2, 1, 9, 4] | ||
output: false | ||
``` | ||
|
||
![ques](./ques.png) | ||
|
||
## JavaScript Implementation | ||
|
||
### [Solution](./JavaScript/isRotation_madhav.js) | ||
|
||
```js | ||
/** | ||
* @author MadhavBahlMD | ||
* @date 22/01/2019 | ||
* Method - Circular Search -- Time = O(n) | ||
*/ | ||
|
||
function isRotation (arr1, arr2) { | ||
let len1 = arr1.length, | ||
len2 = arr2.length; | ||
|
||
// return false if lengths are different | ||
if (len1 !== len2) return false; | ||
|
||
// find position of first element of array2 in array 2 | ||
let isRotated = true, | ||
pos = arr1.indexOf (arr2[0]); | ||
|
||
// Return false if element not found | ||
if (pos < 0) return false; | ||
|
||
// Check all the other numbers | ||
for (let i=0; i<len1; i++) { | ||
if (arr1[(pos+i)%len1] !== arr2[i]) { | ||
isRotated = false; | ||
break; | ||
} | ||
} | ||
|
||
return isRotated; | ||
} | ||
|
||
console.log (isRotation ([1, 2, 3, 4, 5, 6, 7], [4, 5, 6, 7, 1, 2, 3])); // true | ||
console.log (isRotation ([1, 2, 3, 4, 5, 6, 7], [7, 1, 2, 3])); // false | ||
console.log (isRotation ([1, 2, 3, 4, 5, 6], [6, 5, 4, 3, 2, 1])); // false | ||
``` |
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.