-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(hackerrank): add solution for Modified Kaprekar Numbers
- Loading branch information
1 parent
703868f
commit d5c7e98
Showing
2 changed files
with
30 additions
and
0 deletions.
There are no files selected for viewing
28 changes: 28 additions & 0 deletions
28
src/Problem Solving/Algorithms/Implementation/Modified Kaprekar Numbers/index.ts
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,28 @@ | ||
/** | ||
* Prints the modified Kaprekar numbers in the range between (p) and (q), inclusive. If no modified Kaprekar numbers exist in the given range, print "INVALID RANGE". | ||
* @author Andrew Mamdouh <[email protected]> | ||
* @param {number} p - The range lower limit | ||
* @param {number} q - The range upper limit | ||
* @returns {string} The list of modified Kaprekar numbers, space-separated on one line and in ascending order or "INVALID RANGE" If no modified Kaprekar numbers exist in the given range. | ||
*/ | ||
const kaprekarNumbers = (p: number, q: number): string => { | ||
const modifiedKaprekarArr: number[] = []; | ||
for (let i = p; i <= q; i++) { | ||
const poweredStr = Math.pow(i, 2).toString(); | ||
if ( | ||
(parseInt( | ||
poweredStr.slice(0, poweredStr.length - i.toString().length) | ||
) || 0) + | ||
(parseInt( | ||
poweredStr.slice(poweredStr.length - i.toString().length) | ||
) || 0) === | ||
i | ||
) | ||
modifiedKaprekarArr.push(i); | ||
} | ||
return modifiedKaprekarArr.length | ||
? modifiedKaprekarArr.join(' ') | ||
: 'INVALID RANGE'; | ||
}; | ||
|
||
export default kaprekarNumbers; |
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