-
Notifications
You must be signed in to change notification settings - Fork 1
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
0589fff
commit d1bdbee
Showing
3 changed files
with
145 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,55 @@ | ||
// Given an array of integers (positive and negative) find the largest continuous sum. | ||
|
||
// The time complexity is O(N) and space complexity is O(1), which are both optimal. | ||
const largestContinuousSum = (arr) => { | ||
if (arr.length === 0) { | ||
throw new Error('Please pass in a non-empty array') | ||
} | ||
if (!Array.isArray(arr)) { | ||
throw new Error('Please pass in an array'); | ||
} | ||
let maxSum = currentSum = arr[0] | ||
|
||
for(let i=0; i<arr.length; i++) { | ||
currentSum = Math.max(currentSum + arr[i], arr[i]); | ||
maxSum = Math.max(currentSum, maxSum); | ||
} | ||
return maxSum; | ||
}; | ||
|
||
|
||
// Given a sequence of positive intergers A and an interger T, return whether there is a continuous sequence of A that sums up to exactly T. | ||
// Example: | ||
// | ||
// [23,5,4,7,2,11], 20. Return True becuase 7+2+11=20 | ||
// [1,3,5,23,2], 8. Return True because 3+5=8; | ||
// [1,3,5,23,2], 7. Return False because no sequence in this array adds up to 7. | ||
|
||
// findSum runs in O(n) time and O(1) space. | ||
const findSum = (arr, target) => { | ||
if (arr.length === 0) { | ||
throw new Error('Please pass in a non-empty array') | ||
} | ||
if (!Array.isArray(arr) || !typeof target == 'number') { | ||
throw new Error('Please pass valid input'); | ||
} | ||
|
||
let sum = 0; | ||
for(let i=0, j=0; i<arr.length; ++i ){ | ||
sum += arr[i]; | ||
while (sum > target) { | ||
sum -= arr[j]; | ||
++j; | ||
} | ||
if (sum == target) { | ||
return true; | ||
} | ||
} | ||
return false; | ||
}; | ||
|
||
console.log('findSum([23,5,4,7,2,11], 20): ', findSum([23,5,4,7,2,11], 20)); // true | ||
console.log('findSum([1,3,5,23,2], 8): ', findSum([1,3,5,23,2], 8)); // true | ||
console.log('findSum([1,3,5,23,2], 7): ', findSum([1,3,5,23,2], 7)); // true | ||
|
||
//Source: https://www.careercup.com/question?id=6305076727513088 |
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,53 @@ | ||
// A prime number (or a prime) is a natural number greater than 1 that has no positive divisors other than 1 and itself. | ||
|
||
// Recursive Solution | ||
// exports.largestPrimeFactor = function(n) { | ||
|
||
// var primeNumber = 0; | ||
|
||
// function find_highest_prime_factor(n) { | ||
// var max = Math.round(Math.sqrt(n)); | ||
// for (var i = max; i >= 2; i--) { | ||
// if (n % i == 0 && find_highest_prime_factor(i) == 1) { | ||
// return i; | ||
// } | ||
// } | ||
// return 1; | ||
// } | ||
|
||
// primeNumber = find_highest_prime_factor(n); | ||
// return primeNumber; | ||
|
||
// } | ||
|
||
exports.largestPrimeFactor = function(n) { | ||
|
||
function isPrime(value) { | ||
for (var i = 2; i < value; i++) { | ||
if (value % i === 0) { | ||
return false; | ||
} | ||
} | ||
return value > 1; | ||
} | ||
|
||
var primeNumber = 0; | ||
|
||
function largestPrimeFact(n) { | ||
var i = 2 | ||
while ( i <= n ) { | ||
if ( n % i === 0 ) { | ||
n /= i; | ||
if ( isPrime(i) ) { | ||
primeNumber = i; | ||
} | ||
} | ||
i++; | ||
} | ||
return primeNumber; | ||
} | ||
|
||
primeNumber = largestPrimeFact(n); | ||
return primeNumber; | ||
|
||
} |
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,37 @@ | ||
// Prime Numbers: 2, 3, 5, 7, 11, 13, 17, 19, 23, 29, 31, ... | ||
|
||
function PrimeFinder(maxPrime) { | ||
primes = []; | ||
|
||
primes[0] = 2; | ||
primes[1] = 3; | ||
|
||
primeCount = 2; //2, and 3 are already listed as init primes | ||
|
||
//Keep checking prime numbers until maxPrime is found | ||
while (primeCount < maxPrime) { | ||
isPrime = false; // Tracks if the current candidate is prime or not | ||
candidate = primes[primeCount - 1] + 2; //contains variable that needs to be checked if it's prime or not | ||
|
||
// While isPrime is false | ||
while (!isPrime) { | ||
isPrime = true; | ||
for (var pindex = 0; pindex < primeCount; pindex++) { | ||
prime = primes[pindex]; | ||
if (candidate % prime == 0) { | ||
isPrime = false; | ||
break; | ||
} | ||
} | ||
if (!isPrime) { | ||
candidate += 2; | ||
} else { | ||
primes[primeCount] = candidate; | ||
primeCount++; | ||
} | ||
} | ||
} | ||
console.log( 'Solution: ' + primes[maxPrime - 1] ); // soultion is 104743 | ||
} | ||
|
||
PrimeFinder(10001); |