Skip to content

Commit

Permalink
feat(hackerrank): add solution for Modified Kaprekar Numbers
Browse files Browse the repository at this point in the history
  • Loading branch information
AndrewMamdouh committed Sep 5, 2024
1 parent 703868f commit d5c7e98
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 0 deletions.
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;
2 changes: 2 additions & 0 deletions src/Problem Solving/Algorithms/Implementation/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ import flatlandSpaceStations from '@ProblemSolving/Algorithms/Implementation/Fla
import fairRations from '@ProblemSolving/Algorithms/Implementation/Fair Rations';
import cutTheSticks from '@ProblemSolving/Algorithms/Implementation/Cut the sticks';
import cavityMap from '@ProblemSolving/Algorithms/Implementation/Cavity Map';
import kaprekarNumbers from '@ProblemSolving/Algorithms/Implementation/Modified Kaprekar Numbers';

export {
angryProfessor,
Expand All @@ -61,6 +62,7 @@ export {
gradingStudents,
jumpingOnClouds,
kangaroo,
kaprekarNumbers,
migratoryBirds,
minimumDistances,
pageCount,
Expand Down

0 comments on commit d5c7e98

Please sign in to comment.