Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
Skip to content

Commit bd03519

Browse files
committed
Add solution #3099
1 parent f094b28 commit bd03519

File tree

2 files changed

+23
-0
lines changed

2 files changed

+23
-0
lines changed

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2168,6 +2168,7 @@
21682168
3091|[Apply Operations to Make Sum of Array Greater Than or Equal to k](./solutions/3091-apply-operations-to-make-sum-of-array-greater-than-or-equal-to-k.js)|Medium|
21692169
3095|[Shortest Subarray With OR at Least K I](./solutions/3095-shortest-subarray-with-or-at-least-k-i.js)|Easy|
21702170
3097|[Shortest Subarray With OR at Least K II](./solutions/3097-shortest-subarray-with-or-at-least-k-ii.js)|Medium|
2171+
3099|[Harshad Number](./solutions/3099-harshad-number.js)|Easy|
21712172
3105|[Longest Strictly Increasing or Strictly Decreasing Subarray](./solutions/3105-longest-strictly-increasing-or-strictly-decreasing-subarray.js)|Easy|
21722173
3108|[Minimum Cost Walk in Weighted Graph](./solutions/3108-minimum-cost-walk-in-weighted-graph.js)|Hard|
21732174
3110|[Score of a String](./solutions/3110-score-of-a-string.js)|Easy|

solutions/3099-harshad-number.js

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
/**
2+
* 3099. Harshad Number
3+
* https://leetcode.com/problems/harshad-number/
4+
* Difficulty: Easy
5+
*
6+
* An integer divisible by the sum of its digits is said to be a Harshad number. You are given
7+
* an integer x. Return the sum of the digits of x if x is a Harshad number, otherwise, return -1.
8+
*/
9+
10+
/**
11+
* @param {number} x
12+
* @return {number}
13+
*/
14+
var sumOfTheDigitsOfHarshadNumber = function(x) {
15+
let digitSum = 0;
16+
let num = x;
17+
while (num > 0) {
18+
digitSum += num % 10;
19+
num = Math.floor(num / 10);
20+
}
21+
return x % digitSum === 0 ? digitSum : -1;
22+
};

0 commit comments

Comments
 (0)