Skip to content

Commit 5a6877d

Browse files
committedJun 17, 2024
2024-06-17 v. 5.9.0: added "2535. Difference Between Element Sum and Digit Sum of an Array"
1 parent b37be8e commit 5a6877d

File tree

4 files changed

+34
-1
lines changed

4 files changed

+34
-1
lines changed
 

‎README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -450,3 +450,4 @@ Profile on LeetCode: [fartem](https://leetcode.com/fartem/).
450450
| 2520. Count the Digits That Divide a Number | [Link](https://leetcode.com/problems/count-the-digits-that-divide-a-number/) | [Link](./lib/easy/2520_count_the_digits_that_divide_a_number.rb) |
451451
| 2525. Categorize Box According to Criteria | [Link](https://leetcode.com/problems/categorize-box-according-to-criteria/) | [Link](./lib/easy/2525_categorize_box_according_to_criteria.rb) |
452452
| 2529. Maximum Count of Positive Integer and Negative Integer | [Link](https://leetcode.com/problems/maximum-count-of-positive-integer-and-negative-integer/) | [Link](./lib/easy/2529_maximum_count_of_positive_integer_and_negative_integer.rb) |
453+
| 2535. Difference Between Element Sum and Digit Sum of an Array | [Link](https://leetcode.com/problems/difference-between-element-sum-and-digit-sum-of-an-array/) | [Link](./lib/easy/2535_difference_between_element_sum_and_digit_sum_of_an_array.rb) |

‎leetcode-ruby.gemspec

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ require 'English'
55
::Gem::Specification.new do |s|
66
s.required_ruby_version = '>= 3.0'
77
s.name = 'leetcode-ruby'
8-
s.version = '5.8.9'
8+
s.version = '5.9.0'
99
s.license = 'MIT'
1010
s.files = ::Dir['lib/**/*.rb'] + %w[bin/leetcode-ruby README.md LICENSE]
1111
s.executable = 'leetcode-ruby'
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
# frozen_string_literal: true
2+
3+
# https://leetcode.com/problems/difference-between-element-sum-and-digit-sum-of-an-array/
4+
# @param {Integer[]} nums
5+
# @return {Integer}
6+
def difference_of_sum(nums)
7+
sum_of_nums = 0
8+
sum_of_digits = 0
9+
nums.each do |num|
10+
sum_of_nums += num
11+
n = num
12+
13+
while n.positive?
14+
sum_of_digits += n % 10
15+
n /= 10
16+
end
17+
end
18+
19+
(sum_of_nums - sum_of_digits).abs
20+
end
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
# frozen_string_literal: true
2+
3+
require_relative '../test_helper'
4+
require_relative '../../lib/easy/2535_difference_between_element_sum_and_digit_sum_of_an_array'
5+
require 'minitest/autorun'
6+
7+
class DifferenceBetweenElementSumAndDigitSumOfAnArrayTest < ::Minitest::Test
8+
def test_default
9+
assert_equal(9, difference_of_sum([1, 15, 6, 3]))
10+
assert_equal(0, difference_of_sum([1, 2, 3, 4]))
11+
end
12+
end

0 commit comments

Comments
 (0)