diff --git a/Mock Test/1.md b/Mock Test/1.md deleted file mode 100644 index dd48114..0000000 --- a/Mock Test/1.md +++ /dev/null @@ -1,61 +0,0 @@ -// IMPORT LIBRARY PACKAGES NEEDED BY YOUR PROGRAM -// SOME FUNCTIONALITY WITHIN A PACKAGE MAY BE RESTRICTED -// DEFINE ANY FUNCTION NEEDED -// FUNCTION SIGNATURE BEGINS, THIS FUNCTION IS REQUIRED -``` -function cellCompete(states, days) -{ - let ar = [] - while(days--){ - for(var i=0;iTestcase 1:
Input:
[1, 0, 0, 0, 0, 1, 0, 0], 1

Expected Return Value:
0 1 0 0 1 0 1 0


Testcase 2:
Input:
[1, 1, 1, 0, 1, 1, 1, 1], 2

Expected Return Value:
0 0 0 0 0 1 1 0
\ No newline at end of file diff --git a/Mock Test/2.md b/Mock Test/2.md deleted file mode 100644 index 2cd3dbf..0000000 --- a/Mock Test/2.md +++ /dev/null @@ -1,32 +0,0 @@ - -// IMPORT LIBRARY PACKAGES NEEDED BY YOUR PROGRAM -// SOME FUNCTIONALITY WITHIN A PACKAGE MAY BE RESTRICTED -// DEFINE ANY FUNCTION NEEDED -// FUNCTION SIGNATURE BEGINS, THIS FUNCTION IS REQUIRED -``` -function gcd(a,b) { - if(b === 0) { - return a; - } - return gcd(b, a%b) - } - function generalizedGCD(num, arr) - { - let f = arr[num-1]; - - for(let i=num-2;i>=0;i--) { - f = gcd(f,arr[i]); - } - return f; - } - // FUNCTION SIGNATURE ENDS -``` -
-

The current selected programming language is JavaScript. We emphasize the submission of a fully working code over partially correct but efficient code. Once Test is submitted, you cannot review this problem again. You can use console.log to debug your code. We use NodeJS 10.13 to evaluate your code.

-

-

The greatest common divisor (GCD), also called highest common factor (HCF) of N numbers is the largest positive integer that divides all numbers without giving a remainder.

Write an algorithm to determine the GCD of N positive integers.

Input
The input to the function/method consists of two arguments - 
num, an integer representing the number of positive integers (N). 
arr, a list of positive integers.

-

Output
Return an integer representing the GCD of the given positive integers.

Example
Input:
num = 5 
arr = [2, 4, 6, 8, 10]

-

Output:
2

-

Explanation:
The largest positive integer that divides all the positive integers 2, 4, 6, 8, 10 without a remainder is 2.
So, the output is 2.

- -
\ No newline at end of file diff --git a/README.md b/README.md index 4f2f53b..5d0d1de 100644 --- a/README.md +++ b/README.md @@ -1,2 +1,7 @@ # Hackerrank-JS-Solutions Solved entire easy problems & few medium problems in data structures category + +one step at a time + +# Contributor +Contributors always welcome diff --git a/absolute-permutation.js b/absolute-permutation.js new file mode 100644 index 0000000..5f7e4f1 --- /dev/null +++ b/absolute-permutation.js @@ -0,0 +1,65 @@ +'use strict'; + +const fs = require('fs'); + +process.stdin.resume(); +process.stdin.setEncoding('utf-8'); + +let inputString = ''; +let currentLine = 0; + +process.stdin.on('data', inputStdin => { + inputString += inputStdin; +}); + +process.stdin.on('end', _ => { + inputString = inputString.replace(/\s*$/, '') + .split('\n') + .map(str => str.replace(/\s*$/, '')); + + main(); +}); + +function readLine() { + return inputString[currentLine++]; +} + +// Complete the absolutePermutation function below. +const absolutePermutation = (n, k) => { + if (k === 0) { + return Array.from(Array(n), (_, i) => i + 1) + } + else if ((n / k) % 2 !== 0) { + return [-1]; + } + else { + let add = true, perm = []; + for (let i = 1; i < n + 1; i++) { + add ? perm.push(i + k) : perm.push(i - k); + if (i % k === 0) { + add = add ? false : true; + } + } + return perm; + } +} + +function main() { + const ws = fs.createWriteStream(process.env.OUTPUT_PATH); + + const t = parseInt(readLine(), 10); + + for (let tItr = 0; tItr < t; tItr++) { + const nk = readLine().split(' '); + + const n = parseInt(nk[0], 10); + + const k = parseInt(nk[1], 10); + + let result = absolutePermutation(n, k); + + ws.write(result.join(" ") + "\n"); + } + + ws.end(); +} diff --git a/adhoc/incre-decre-sqaure-sort.js b/adhoc/incre-decre-sqaure-sort.js deleted file mode 100644 index d5ef0f8..0000000 --- a/adhoc/incre-decre-sqaure-sort.js +++ /dev/null @@ -1,22 +0,0 @@ -const fun = (n, val) => { - if (val >= 10 && parseInt((val + "").split("").sort((x, y) => x < y).join("")) === n) - return 1; - if ((val - 1) === n) { - return 1; - } - if (val > n) { - return 9999; - } - if (val === n) { - return 0; - } - let a = 1 + fun(n, val + 1); - let b = 1 + fun(n, Math.pow(val, 2)); - return Math.min(a, b); -} - -console.log(fun(3, 2) + 2); -console.log(fun(5, 2) + 2); -console.log(fun(15, 2) + 2); -console.log(fun(61, 2) + 2); -console.log(fun(18, 2) + 2); \ No newline at end of file diff --git a/almost-sorted.js b/almost-sorted.js new file mode 100644 index 0000000..10d17fb --- /dev/null +++ b/almost-sorted.js @@ -0,0 +1,96 @@ +'use strict'; + +process.stdin.resume(); +process.stdin.setEncoding('utf-8'); + +let inputString = ''; +let currentLine = 0; + +process.stdin.on('data', inputStdin => { + inputString += inputStdin; +}); + +process.stdin.on('end', _ => { + inputString = inputString.replace(/\s*$/, '') + .split('\n') + .map(str => str.replace(/\s*$/, '')); + + main(); +}); + +function readLine() { + return inputString[currentLine++]; +} + +// Complete the almostSorted function below. +function almostSorted(arr) { + const isSorted = arr => arr.every((v, i, a) => !i || a[i - 1] <= v); + const swap = (arr, i, j) => { + let t = arr[i]; + arr[i] = arr[j]; + arr[j] = t; + }; + const reverse = (arr, i, j) => { + let split = arr.slice(i, j + 1); + split.reverse(); + arr.splice(i, j, ...split); + } + + let peak = [], valley = []; + for (let i = 1; i < arr.length - 1; i++) { + let prev = arr[i - 1], + current = arr[i], + next = arr[i + 1]; + if (current > prev && current > next && next > prev) { + // console.log("peak", i + 1, current) + peak.push(i + 1) + } + else if (prev > current && current < next && next > prev) { + // console.log("valley", i + 1, current) + + valley.push(i + 1) + } + } + // console.log(valley, peak) + if (arr.length === 2) { + if (arr[0] > arr[1]) + console.log(`yes\nswap 1 2`); + } + // case 2 + else if (valley.length === 1 && peak.length === 1) { + swap(arr, peak[0] - 1, valley[0] - 1); + if (isSorted(arr)) { + console.log(`yes\nswap ${peak[0]} ${valley[0]}`); + return; + } + swap(arr, peak[0] - 1, valley[0] - 1); + reverse(arr, peak[0] - 1, valley[0] - 1); + // swap + if (isSorted(arr)) { + console.log(`yes\nreverse ${peak[0]} ${valley[0]}`); + } else { + console.log('no'); + } + } + // case 1 + else if (valley.length === 1) + console.log("no"); + // case 4 + else if (valley.length === 0 && peak.length === 0) { + reverse(arr, 0, arr.length - 1) + if (isSorted(arr)) { + console.log(`yes\nreverse 1 ${arr.length}`) + } else { console.log('no') } + } + else { + console.log("no"); + } +} + +function main() { + const n = parseInt(readLine(), 10); + + const arr = readLine().split(' ').map(arrTemp => parseInt(arrTemp, 10)); + + almostSorted(arr); +} diff --git a/coin-change.js b/coin-change.js new file mode 100644 index 0000000..84c5d33 --- /dev/null +++ b/coin-change.js @@ -0,0 +1,93 @@ +'use strict'; + +const fs = require('fs'); + +process.stdin.resume(); +process.stdin.setEncoding('utf-8'); + +let inputString = ''; +let currentLine = 0; + +process.stdin.on('data', function (inputStdin) { + inputString += inputStdin; +}); + +process.stdin.on('end', function () { + inputString = inputString.split('\n'); + + main(); +}); + +function readLine() { + return inputString[currentLine++]; +} + +/* + * Complete the 'getWays' function below. + * + * The function is expected to return a LONG_INTEGER. + * The function accepts following parameters: + * 1. INTEGER n + * 2. LONG_INTEGER_ARRAY c + */ + +function getWays(n, c) { + // Write your code here + return count(c, c.length, n); +} +function readLine() { + return inputString[currentLine++]; +} +let memory = {} +function count(S, m, n) { + // If n is 0 then there is 1 solution + // (do not include any coin) + if (n === 0) + return 1; + + // If n is less than 0 then no + // solution exists + if (n < 0) + return 0; + + // If there are no coins and n + // is greater than 0, then no + // solution exist + if (m <= 0 && n >= 1) + return 0; + + // count is sum of solutions (i) + // including S[m-1] (ii) excluding S[m-1] + // return count(S, m - 1, n) + count(S, m, n - S[m - 1]); + + let leftsubtree = m - 1 + '-' + n; + let rightsubtree = m + '-' + (n - S[m - 1]); + + if (typeof memory[leftsubtree] === 'undefined') + memory[leftsubtree] = count(S, m - 1, n); + + if (typeof memory[rightsubtree] === 'undefined') + memory[rightsubtree] = count(S, m, n - S[m - 1]); + + return memory[leftsubtree] + memory[rightsubtree]; +} + +function main() { + const ws = fs.createWriteStream(process.env.OUTPUT_PATH); + + const firstMultipleInput = readLine().replace(/\s+$/g, '').split(' '); + + const n = parseInt(firstMultipleInput[0], 10); + + const m = parseInt(firstMultipleInput[1], 10); + + const c = readLine().replace(/\s+$/g, '').split(' ').map(cTemp => parseInt(cTemp, 10)); + + // Print the number of ways of making change for 'n' units using coins having the values given by 'c' + + const ways = getWays(n, c); + + ws.write(ways + '\n'); + + ws.end(); +} diff --git a/encryption.js b/encryption.js new file mode 100644 index 0000000..80da723 --- /dev/null +++ b/encryption.js @@ -0,0 +1,69 @@ +'use strict'; + +const fs = require('fs'); + +process.stdin.resume(); +process.stdin.setEncoding('utf-8'); + +let inputString = ''; +let currentLine = 0; + +process.stdin.on('data', inputStdin => { + inputString += inputStdin; +}); + +process.stdin.on('end', _ => { + inputString = inputString.replace(/\s*$/, '') + .split('\n') + .map(str => str.replace(/\s*$/, '')); + + main(); +}); + +function readLine() { + return inputString[currentLine++]; +} +function getMatrixDimension(s) { + let len = Math.floor(Math.sqrt(s.length)); + let m = len; + let n = len + 1; + if ((m * n) < s.length) { + m = n = len + 1; + } + else if (Math.sqrt(s.length) % 1 === 0) { + m = n = Math.sqrt(s.length); + } + return [m, n]; +} +// Complete the encryption function below. +function encryption(s) { + let [m, n] = getMatrixDimension(s); + let strindex = 0; + let grid = []; + for (let i = 0; i < m; i++) { + grid[i] = []; + for (let j = 0; j < n; j++) { + grid[i][j] = s[strindex] || ''; + strindex++; + } + } + let out = ""; + for (let i = 0; i < n; i++) { + for (let j = 0; j < m; j++) { + out += grid[j][i]; + } out += " "; + } + return out; +} + +function main() { + const ws = fs.createWriteStream(process.env.OUTPUT_PATH); + + const s = readLine(); + + let result = encryption(s); + + ws.write(result + "\n"); + + ws.end(); +} diff --git a/fibonacci-modified.js b/fibonacci-modified.js new file mode 100644 index 0000000..411ecd2 --- /dev/null +++ b/fibonacci-modified.js @@ -0,0 +1,53 @@ +'use strict'; + +const fs = require('fs'); + +process.stdin.resume(); +process.stdin.setEncoding('utf-8'); + +let inputString = ''; +let currentLine = 0; + +process.stdin.on('data', inputStdin => { + inputString += inputStdin; +}); + +process.stdin.on('end', _ => { + inputString = inputString.replace(/\s*$/, '') + .split('\n') + .map(str => str.replace(/\s*$/, '')); + + main(); +}); + +function readLine() { + return inputString[currentLine++]; +} + +function fibonacciModified(t1, t2, n) { + let hash = {}; + hash[1] = BigInt(t1); + hash[2] = BigInt(t2); + for (let i = 3; i < n + 1; i++) { + hash[i] = (hash[i - 1] * hash[i - 1]) + hash[i - 2]; + } + return hash[n] +} + +function main() { + const ws = fs.createWriteStream(process.env.OUTPUT_PATH); + + const t1T2n = readLine().split(' '); + + const t1 = parseInt(t1T2n[0], 10); + + const t2 = parseInt(t1T2n[1], 10); + + const n = parseInt(t1T2n[2], 10); + + let result = fibonacciModified(t1, t2, n); + + ws.write(result + "\n"); + + ws.end(); +} diff --git a/high-security-strings.js b/high-security-strings.js new file mode 100644 index 0000000..936c163 --- /dev/null +++ b/high-security-strings.js @@ -0,0 +1,47 @@ +'use strict'; + +const fs = require('fs'); + +process.stdin.resume(); +process.stdin.setEncoding('utf-8'); + +let inputString = ''; +let currentLine = 0; + +process.stdin.on('data', function(inputStdin) { + inputString += inputStdin; +}); + +process.stdin.on('end', function() { + inputString = inputString.split('\n'); + + main(); +}); + +function readLine() { + return inputString[currentLine++]; +} + +function getStrength(password, weight_a) { + password = password.toLowerCase(); + let c = 0; + for(let i in password){ + let value = (password[i].charCodeAt() - 97) + weight_a; + c+= (value <=25) ? value : (value - 26); + } + return c; +} + +function main() { + const ws = fs.createWriteStream(process.env.OUTPUT_PATH); + + const password = readLine(); + + const weight_a = parseInt(readLine().trim(), 10); + + const strength = getStrength(password, weight_a); + + ws.write(strength + '\n'); + + ws.end(); +} diff --git a/larrys-array.js b/larrys-array.js new file mode 100644 index 0000000..3f782f9 --- /dev/null +++ b/larrys-array.js @@ -0,0 +1,54 @@ +'use strict'; + +const fs = require('fs'); + +process.stdin.resume(); +process.stdin.setEncoding('utf-8'); + +let inputString = ''; +let currentLine = 0; + +process.stdin.on('data', inputStdin => { + inputString += inputStdin; +}); + +process.stdin.on('end', _ => { + inputString = inputString.replace(/\s*$/, '') + .split('\n') + .map(str => str.replace(/\s*$/, '')); + + main(); +}); + +function readLine() { + return inputString[currentLine++]; +} + +// Complete the larrysArray function below. +function larrysArray(A) { + let count = 0; + for (let i = 1; i < A.length; i++) { + for (let k = 0; k < i; k++) { + if (A[k] > A[i]) count++; + } + } + return count % 2 === 0 ? "YES": "NO"; +} + +function main() { + const ws = fs.createWriteStream(process.env.OUTPUT_PATH); + + const t = parseInt(readLine(), 10); + + for (let tItr = 0; tItr < t; tItr++) { + const n = parseInt(readLine(), 10); + + const A = readLine().split(' ').map(ATemp => parseInt(ATemp, 10)); + + let result = larrysArray(A); + + ws.write(result + "\n"); + } + + ws.end(); +} \ No newline at end of file diff --git a/maximum-sum-10-1.js b/maximum-sum-10-1.js new file mode 100644 index 0000000..b9501ca --- /dev/null +++ b/maximum-sum-10-1.js @@ -0,0 +1,63 @@ +//https://www.hackerrank.com/contests/hack-the-interview-vi-asia-pacific/challenges/maximum-sum-10-1 +'use strict'; + +const fs = require('fs'); + +process.stdin.resume(); +process.stdin.setEncoding('utf-8'); + +let inputString = ''; +let currentLine = 0; + +process.stdin.on('data', function(inputStdin) { + inputString += inputStdin; +}); + +process.stdin.on('end', function() { + inputString = inputString.split('\n'); + + main(); +}); + +function readLine() { + return inputString[currentLine++]; +} + +/* + * Complete the 'performOperations' function below. + * + * The function is expected to return a LONG_INTEGER_ARRAY. + * The function accepts following parameters: + * 1. INTEGER N + * 2. INTEGER_ARRAY op + */ +function performOperations(N, op) { + let out = []; + let first = 1, last = N; + let sum = Math.floor( ((N-2) * (N+1)) / 2); + for(let i =0;i { + inputString += inputStdin; +}); + +process.stdin.on('end', _ => { + inputString = inputString.replace(/\s*$/, '') + .split('\n') + .map(str => str.replace(/\s*$/, '')); + + main(); +}); + +function readLine() { + return inputString[currentLine++]; +} + +// Complete the organizingContainers function below. +function organizingContainers(container) { + let containerCount = new Array(container.length).fill(0); + let typeCount = new Array(container.length).fill(0) + container.forEach((type, i_index) => { + type.forEach((val, j_index) => { + containerCount[i_index] += val; + typeCount[j_index] += val; + }); + }); + let pts = "Possible"; + containerCount = containerCount.sort(); + typeCount = typeCount.sort(); + for (let i = 0; i < containerCount.length; i++) { + if (containerCount[i] !== typeCount[i]) { + pts = "Impossible"; + break; + } + } + return pts; +} + +function main() { + const ws = fs.createWriteStream(process.env.OUTPUT_PATH); + + const q = parseInt(readLine(), 10); + + for (let qItr = 0; qItr < q; qItr++) { + const n = parseInt(readLine(), 10); + + let container = Array(n); + + for (let i = 0; i < n; i++) { + container[i] = readLine().split(' ').map(containerTemp => parseInt(containerTemp, 10)); + } + + let result = organizingContainers(container); + + ws.write(result + "\n"); + } + + ws.end(); +} diff --git a/richie-rich.js b/richie-rich.js new file mode 100644 index 0000000..f404bb8 --- /dev/null +++ b/richie-rich.js @@ -0,0 +1,88 @@ +'use strict'; + +const fs = require('fs'); + +process.stdin.resume(); +process.stdin.setEncoding('utf-8'); + +let inputString = ''; +let currentLine = 0; + +process.stdin.on('data', inputStdin => { + inputString += inputStdin; +}); + +process.stdin.on('end', _ => { + inputString = inputString.replace(/\s*$/, '') + .split('\n') + .map(str => str.replace(/\s*$/, '')); + + main(); +}); + +function readLine() { + return inputString[currentLine++]; +} + +// Complete the highestValuePalindrome function below. +const isValidPalindrome = (s) => { + let m = Math.ceil(s.length / 2); + for (let i = 0, j = s.length - 1; i < m, j >= m; i++ , j--) { + if (s[i] !== s[j]) { return false; } + } + return true; +} +function highestValuePalindrome(s, n, k) { + let lives = k; + let mod = new Array(n).fill(false); + let temp = s.split(''); + for (let i = 0; i < n / 2; i++) { + let j = n - i - 1; + if (temp[i] != temp[j]) { + mod[i] = true; + lives--; + } + if (temp[i] < temp[j]) + temp[i] = temp[j]; + else if (temp[i] > temp[j]) + temp[j] = temp[i]; + if (lives < 0) + return "-1"; + } + let j = 0; + while ((lives > 0) && (j < n / 2)) { + if (temp[j] != '9') { + if (mod[j]) + lives++; + if (lives > 1) { + temp[j] = '9'; + temp[n - j - 1] = '9'; + lives -= 2; + } + } + j++; + } + if (n % 2 == 1) { + if (lives > 0) + temp[Math.floor(n / 2)] = '9'; + } + return temp.join(''); +} + +function main() { + const ws = fs.createWriteStream(process.env.OUTPUT_PATH); + + const nk = readLine().split(' '); + + const n = parseInt(nk[0], 10); + + const k = parseInt(nk[1], 10); + + const s = readLine(); + + let result = highestValuePalindrome(s, n, k); + + ws.write(result + "\n"); + + ws.end(); +} diff --git a/sherlock-and-anagrams.js b/sherlock-and-anagrams.js new file mode 100644 index 0000000..e663eee --- /dev/null +++ b/sherlock-and-anagrams.js @@ -0,0 +1,61 @@ +'use strict'; + +const fs = require('fs'); + +process.stdin.resume(); +process.stdin.setEncoding('utf-8'); + +let inputString = ''; +let currentLine = 0; + +process.stdin.on('data', inputStdin => { + inputString += inputStdin; +}); + +process.stdin.on('end', _ => { + inputString = inputString.replace(/\s*$/, '') + .split('\n') + .map(str => str.replace(/\s*$/, '')); + + main(); +}); + +function readLine() { + return inputString[currentLine++]; +} + +function sherlockAndAnagrams(s) { + let hash = {}; + for (let i = 0; i < s.length; i++) { + for (let j = i + 1; j < s.length + 1; j++) { + let key = s.slice(i, j).split('').sort().join(''); + hash[key] = hash[key] ? hash[key] + 1 : 1; + } + + } + let count = 0; + for (let key in hash) { + if (key !== s) { + // n * n-1 /2 + count += hash[key] * (hash[key] - 1) / 2; + }; + } + return count; + +} + +function main() { + const ws = fs.createWriteStream(process.env.OUTPUT_PATH); + + const q = parseInt(readLine(), 10); + + for (let qItr = 0; qItr < q; qItr++) { + const s = readLine(); + + let result = sherlockAndAnagrams(s); + + ws.write(result + "\n"); + } + + ws.end(); +} diff --git a/the-grid-search.js b/the-grid-search.js new file mode 100644 index 0000000..da47592 --- /dev/null +++ b/the-grid-search.js @@ -0,0 +1,101 @@ +'use strict'; + +const fs = require('fs'); + +process.stdin.resume(); +process.stdin.setEncoding('utf-8'); + +let inputString = ''; +let currentLine = 0; + +process.stdin.on('data', inputStdin => { + inputString += inputStdin; +}); + +process.stdin.on('end', _ => { + inputString = inputString.replace(/\s*$/, '') + .split('\n') + .map(str => str.replace(/\s*$/, '')); + + main(); +}); + +function readLine() { + return inputString[currentLine++]; +} + +const getIndices = (word, pattern) => { + let indices = []; + let i = 0; + let index = word.indexOf(pattern, i); + while (index >= 0) { + indices.push(index); + i++; + index = word.indexOf(pattern, i); + } + return Array.from(new Set(indices)); +} +function gridSearch(G, P) { + let gridLength = G.length; + let patternLength = P.length; + for (let gridIndex = 0; gridIndex < gridLength; gridIndex++) { + let patternIndex = 0; + let gridRow = G[gridIndex + patternIndex]; + let patternRow = P[patternIndex]; + let indices = getIndices(gridRow, patternRow); + for (let index in indices) { + let pos = indices[index]; + for (patternIndex = 1; patternIndex < patternLength; patternIndex++) { + gridRow = G[gridIndex + patternIndex]; + patternRow = P[patternIndex]; + let patternRowIndices = getIndices(gridRow, patternRow); + if (patternRowIndices.indexOf(pos) === -1) { + break; + } + } + if (patternIndex === patternLength) + return "YES"; + } + } + return "NO"; +} + +function main() { + const ws = fs.createWriteStream(process.env.OUTPUT_PATH); + + const t = parseInt(readLine(), 10); + + for (let tItr = 0; tItr < t; tItr++) { + const RC = readLine().split(' '); + + const R = parseInt(RC[0], 10); + + const C = parseInt(RC[1], 10); + + let G = []; + + for (let i = 0; i < R; i++) { + const GItem = readLine(); + G.push(GItem); + } + + const rc = readLine().split(' '); + + const r = parseInt(rc[0], 10); + + const c = parseInt(rc[1], 10); + + let P = []; + + for (let i = 0; i < r; i++) { + const PItem = readLine(); + P.push(PItem); + } + + let result = gridSearch(G, P); + + ws.write(result + "\n"); + } + + ws.end(); +} diff --git a/the-power-sum.js b/the-power-sum.js new file mode 100644 index 0000000..aeb281a --- /dev/null +++ b/the-power-sum.js @@ -0,0 +1,76 @@ +'use strict'; + +const fs = require('fs'); + +process.stdin.resume(); +process.stdin.setEncoding('utf-8'); + +let inputString = ''; +let currentLine = 0; + +process.stdin.on('data', inputStdin => { + inputString += inputStdin; +}); + +process.stdin.on('end', _ => { + inputString = inputString.replace(/\s*$/, '') + .split('\n') + .map(str => str.replace(/\s*$/, '')); + + main(); +}); + +function readLine() { + return inputString[currentLine++]; +} + +function count(S, m, n, power) { + // If n is 0 then there is 1 solution + // (do not include any coin) + if (n === 0) + return 1; + + // If n is less than 0 then no + // solution exists + if (n < 0) + return 0; + + // If there are no coins and n + // is greater than 0, then no + // solution exist + if (m <= 0 && n >= 1) + return 0; + + // count is sum of solutions (i) + // including S[m-1] (ii) excluding S[m-1] + return count(S, m - 1, n, power) + count(S, m - 1, n - Math.pow(S[m - 1], power), power); +} + +// Complete the powerSum function below. +function powerSum(X, N) { + let m = 1; + let sums = [] + for (let i = 1; i < X; i++) { + if (Math.pow(i, N) > X) { + break; + } else { + sums.push(i); + } + } + console.log(sums) + return count(sums, sums.length, X, N); +} + +function main() { + const ws = fs.createWriteStream(process.env.OUTPUT_PATH); + + const X = parseInt(readLine(), 10); + + const N = parseInt(readLine(), 10); + + let result = powerSum(X, N); + + ws.write(result + "\n"); + + ws.end(); +}