Skip to content

Commit

Permalink
Add day 24
Browse files Browse the repository at this point in the history
  • Loading branch information
MadhavBahl committed Jan 22, 2019
1 parent 4148c81 commit 096755a
Show file tree
Hide file tree
Showing 5 changed files with 96 additions and 0 deletions.
1 change: 1 addition & 0 deletions README.md
Expand Up @@ -47,6 +47,7 @@ Read [CONTRIBUTING.md](./CONTRIBUTING.md) for contribution guidelines.
21. [Day 21 -- Pair Sum N and Max Subarray Sum](./day21) -- [http://codetoexpress.tech/dc/day21/](http://codetoexpress.tech/dc/day21/)
22. [Day 22 -- Common Elements Search](./day22) -- [http://codetoexpress.tech/dc/day22/](http://codetoexpress.tech/dc/day22/)
23. [Day 23 -- Combination Sum](./day23) -- [http://codetoexpress.tech/dc/day23/](http://codetoexpress.tech/dc/day23/)
24. [Day 24 -- Array Circular Rotation](./day24) -- [http://codetoexpress.tech/dc/day24/](http://codetoexpress.tech/dc/day24/)

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

Expand Down
Binary file added day24/Daily Codes.png
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
34 changes: 34 additions & 0 deletions day24/JavaScript/isRotation_madhav.js
@@ -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
61 changes: 61 additions & 0 deletions day24/README.md
@@ -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
```
Binary file added day24/carbon.png
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.

0 comments on commit 096755a

Please sign in to comment.