Skip to content

Commit 1eb7920

Browse files
committed
Add solution #2843
1 parent 7884591 commit 1eb7920

File tree

2 files changed

+41
-1
lines changed

2 files changed

+41
-1
lines changed

README.md

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
# 1,240 LeetCode solutions in JavaScript
1+
# 1,241 LeetCode solutions in JavaScript
22

33
[https://leetcodejavascript.com](https://leetcodejavascript.com)
44

@@ -1216,6 +1216,7 @@
12161216
2727|[Is Object Empty](./solutions/2727-is-object-empty.js)|Easy|
12171217
2780|[Minimum Index of a Valid Split](./solutions/2780-minimum-index-of-a-valid-split.js)|Medium|
12181218
2818|[Apply Operations to Maximize Score](./solutions/2818-apply-operations-to-maximize-score.js)|Hard|
1219+
2843|[Count Symmetric Integers](./solutions/2843-count-symmetric-integers.js)|Easy|
12191220
2873|[Maximum Value of an Ordered Triplet I](./solutions/2873-maximum-value-of-an-ordered-triplet-i.js)|Easy|
12201221
2874|[Maximum Value of an Ordered Triplet II](./solutions/2874-maximum-value-of-an-ordered-triplet-ii.js)|Medium|
12211222
2948|[Make Lexicographically Smallest Array by Swapping Elements](./solutions/2948-make-lexicographically-smallest-array-by-swapping-elements.js)|Medium|
Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
/**
2+
* 2843. Count Symmetric Integers
3+
* https://leetcode.com/problems/count-symmetric-integers/
4+
* Difficulty: Easy
5+
*
6+
* You are given two positive integers low and high.
7+
*
8+
* An integer x consisting of 2 * n digits is symmetric if the sum of the first n digits of x is
9+
* equal to the sum of the last n digits of x. Numbers with an odd number of digits are never
10+
* symmetric.
11+
*
12+
* Return the number of symmetric integers in the range [low, high].
13+
*/
14+
15+
/**
16+
* @param {number} low
17+
* @param {number} high
18+
* @return {number}
19+
*/
20+
var countSymmetricIntegers = function(low, high) {
21+
let result = 0;
22+
23+
for (let number = low; number <= high; number++) {
24+
if (isSymmetric(number)) result++;
25+
}
26+
27+
return result;
28+
29+
function isSymmetric(number) {
30+
const digits = number.toString().split('');
31+
const length = digits.length;
32+
if (length % 2 !== 0) return false;
33+
const mid = length / 2;
34+
const firstHalfSum = digits.slice(0, mid).reduce((sum, digit) => sum + Number(digit), 0);
35+
const secondHalfSum = digits.slice(mid).reduce((sum, digit) => sum + Number(digit), 0);
36+
37+
return firstHalfSum === secondHalfSum;
38+
}
39+
};

0 commit comments

Comments
 (0)