Class Solution
java.lang.Object
g2501_2600.s2588_count_the_number_of_beautiful_subarrays.Solution
2588 - Count the Number of Beautiful Subarrays.<p>Medium</p>
<p>You are given a <strong>0-indexed</strong> integer array <code>nums</code>. In one operation, you can:</p>
<ul>
<li>Choose two different indices <code>i</code> and <code>j</code> such that <code>0 <= i, j < nums.length</code>.</li>
<li>Choose a non-negative integer <code>k</code> such that the <code>k<sup>th</sup></code> bit ( <strong>0-indexed</strong> ) in the binary representation of <code>nums[i]</code> and <code>nums[j]</code> is <code>1</code>.</li>
<li>Subtract <code>2<sup>k</sup></code> from <code>nums[i]</code> and <code>nums[j]</code>.</li>
</ul>
<p>A subarray is <strong>beautiful</strong> if it is possible to make all of its elements equal to <code>0</code> after applying the above operation any number of times.</p>
<p>Return <em>the number of <strong>beautiful subarrays</strong> in the array</em> <code>nums</code>.</p>
<p>A subarray is a contiguous <strong>non-empty</strong> sequence of elements within an array.</p>
<p><strong>Example 1:</strong></p>
<p><strong>Input:</strong> nums = [4,3,1,2,4]</p>
<p><strong>Output:</strong> 2</p>
<p><strong>Explanation:</strong> There are 2 beautiful subarrays in nums: [4,<ins>3,1,2</ins>,4] and [<ins>4,3,1,2,4</ins>].</p>
<ul>
<li>We can make all elements in the subarray [3,1,2] equal to 0 in the following way:
<ul>
<li>Choose [<ins>3</ins>, 1, <ins>2</ins>] and k = 1. Subtract 2<sup>1</sup> from both numbers. The subarray becomes [1, 1, 0].</li>
<li>Choose [<ins>1</ins>, <ins>1</ins>, 0] and k = 0. Subtract 2<sup>0</sup> from both numbers. The subarray becomes [0, 0, 0].</li>
</ul>
</li>
<li>We can make all elements in the subarray [4,3,1,2,4] equal to 0 in the following way:
<ul>
<li>Choose [<ins>4</ins>, 3, 1, 2, <ins>4</ins>] and k = 2. Subtract 2<sup>2</sup> from both numbers. The subarray becomes [0, 3, 1, 2, 0].</li>
<li>Choose [0, <ins>3</ins>, <ins>1</ins>, 2, 0] and k = 0. Subtract 2<sup>0</sup> from both numbers. The subarray becomes [0, 2, 0, 2, 0].</li>
<li>Choose [0, <ins>2</ins>, 0, <ins>2</ins>, 0] and k = 1. Subtract 2<sup>1</sup> from both numbers. The subarray becomes [0, 0, 0, 0, 0].</li>
</ul>
</li>
</ul>
<p><strong>Example 2:</strong></p>
<p><strong>Input:</strong> nums = [1,10,4]</p>
<p><strong>Output:</strong> 0</p>
<p><strong>Explanation:</strong> There are no beautiful subarrays in nums.</p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= nums.length <= 10<sup>5</sup></code></li>
<li><code>0 <= nums[i] <= 10<sup>6</sup></code></li>
</ul>
-
Constructor Summary
Constructors -
Method Summary
-
Constructor Details
-
Solution
public Solution()
-
-
Method Details
-
beautifulSubarrays
public long beautifulSubarrays(int[] nums)
-