Finding the group with largest elements with same digit sum in JavaScript

We are required to write a JavaScript function that takes in a positive integer, say n, as the only argument.

The function should first group the integers from 1 to n to subarray where a specific subarray contains all the elements contains a specific digit sum. Then the function should examine each subarray and return the length of that subarray which contains the most elements.

For example −

If the input number is −

const num = 15;

Then the output should be −

const output = 2;

because the groups are −

[1, 10], [2, 11], [3, 12], [4, 13], [5, 14], [6, 15], [7], [8], [9]

Example

Following is the code −

const num = 67;
const countLargestGroup = (num = 1) => {
   if(num 

Output

Following is the console output −

4
Updated on: 2021-01-27T06:31:10+05:30

176 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements