Skip to content

Commit

Permalink
Merge pull request #436 from fartem/1863_Sum_of_All_Subset_XOR_Totals
Browse files Browse the repository at this point in the history
2023-12-25 v. 3.8.1: added "1863. Sum of All Subset XOR Totals"
  • Loading branch information
fartem authored Dec 25, 2023
2 parents 37f0a50 + 4ce20a0 commit d8045d6
Show file tree
Hide file tree
Showing 4 changed files with 26 additions and 1 deletion.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -327,3 +327,4 @@ Profile on LeetCode: [fartem](https://leetcode.com/fartem/).
| 1844. Replace All Digits with Characters | [Link](https://leetcode.com/problems/replace-all-digits-with-characters/) | [Link](./lib/easy/1844_replace_all_digits_with_characters.rb) |
| 1848. Minimum Distance to the Target Element | [Link](https://leetcode.com/problems/minimum-distance-to-the-target-element/) | [Link](./lib/easy/1848_minimum_distance_to_the_target_element.rb) |
| 1859. Sorting the Sentence | [Link](https://leetcode.com/problems/sorting-the-sentence/) | [Link](./lib/easy/1859_sorting_the_sentence.rb) |
| 1863. Sum of All Subset XOR Totals | [Link](https://leetcode.com/problems/sum-of-all-subset-xor-totals/) | [Link](./lib/easy/1863_sum_of_all_subset_xor_totals.rb) |
2 changes: 1 addition & 1 deletion leetcode-ruby.gemspec
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ require 'English'
::Gem::Specification.new do |s|
s.required_ruby_version = '>= 3.0'
s.name = 'leetcode-ruby'
s.version = '3.8.0'
s.version = '3.8.1'
s.license = 'MIT'
s.files = ::Dir['lib/**/*.rb'] + %w[bin/leetcode-ruby README.md LICENSE]
s.executable = 'leetcode-ruby'
Expand Down
11 changes: 11 additions & 0 deletions lib/easy/1863_sum_of_all_subset_xor_totals.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
# frozen_string_literal: true

# https://leetcode.com/problems/sum-of-all-subset-xor-totals/
# @param {Integer[]} nums
# @return {Integer}
def subset_xor_sum(nums)
bits = 0
nums.each { |num| bits |= num }

bits * 2**(nums.length - 1)
end
13 changes: 13 additions & 0 deletions test/easy/test_1863_sum_of_all_subset_xor_totals.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
# frozen_string_literal: true

require_relative '../test_helper'
require_relative '../../lib/easy/1863_sum_of_all_subset_xor_totals'
require 'minitest/autorun'

class SumOfAllSubsetXORTotalsTest < ::Minitest::Test
def test_default
assert_equal(6, subset_xor_sum([1, 3]))
assert_equal(28, subset_xor_sum([5, 1, 6]))
assert_equal(480, subset_xor_sum([3, 4, 5, 6, 7, 8]))
end
end

0 comments on commit d8045d6

Please sign in to comment.