Skip to content

Commit 8d9f017

Browse files
committed
Create README - LeetHub
1 parent 16ffbb2 commit 8d9f017

File tree

1 file changed

+43
-0
lines changed

1 file changed

+43
-0
lines changed
Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
<h2><a href="https://leetcode.com/problems/find-if-array-can-be-sorted/">3011. Find if Array Can Be Sorted</a></h2><h3>Medium</h3><hr><div><p>You are given a <strong>0-indexed</strong> array of <strong>positive</strong> integers <code>nums</code>.</p>
2+
3+
<p>In one <strong>operation</strong>, you can swap any two <strong>adjacent</strong> elements if they have the <strong>same</strong> number of <span data-keyword="set-bit">set bits</span>. You are allowed to do this operation <strong>any</strong> number of times (<strong>including zero</strong>).</p>
4+
5+
<p>Return <code>true</code> <em>if you can sort the array, else return </em><code>false</code>.</p>
6+
7+
<p>&nbsp;</p>
8+
<p><strong class="example">Example 1:</strong></p>
9+
10+
<pre><strong>Input:</strong> nums = [8,4,2,30,15]
11+
<strong>Output:</strong> true
12+
<strong>Explanation:</strong> Let's look at the binary representation of every element. The numbers 2, 4, and 8 have one set bit each with binary representation "10", "100", and "1000" respectively. The numbers 15 and 30 have four set bits each with binary representation "1111" and "11110".
13+
We can sort the array using 4 operations:
14+
- Swap nums[0] with nums[1]. This operation is valid because 8 and 4 have one set bit each. The array becomes [4,8,2,30,15].
15+
- Swap nums[1] with nums[2]. This operation is valid because 8 and 2 have one set bit each. The array becomes [4,2,8,30,15].
16+
- Swap nums[0] with nums[1]. This operation is valid because 4 and 2 have one set bit each. The array becomes [2,4,8,30,15].
17+
- Swap nums[3] with nums[4]. This operation is valid because 30 and 15 have four set bits each. The array becomes [2,4,8,15,30].
18+
The array has become sorted, hence we return true.
19+
Note that there may be other sequences of operations which also sort the array.
20+
</pre>
21+
22+
<p><strong class="example">Example 2:</strong></p>
23+
24+
<pre><strong>Input:</strong> nums = [1,2,3,4,5]
25+
<strong>Output:</strong> true
26+
<strong>Explanation:</strong> The array is already sorted, hence we return true.
27+
</pre>
28+
29+
<p><strong class="example">Example 3:</strong></p>
30+
31+
<pre><strong>Input:</strong> nums = [3,16,8,4,2]
32+
<strong>Output:</strong> false
33+
<strong>Explanation:</strong> It can be shown that it is not possible to sort the input array using any number of operations.
34+
</pre>
35+
36+
<p>&nbsp;</p>
37+
<p><strong>Constraints:</strong></p>
38+
39+
<ul>
40+
<li><code>1 &lt;= nums.length &lt;= 100</code></li>
41+
<li><code>1 &lt;= nums[i] &lt;= 2<sup>8</sup></code></li>
42+
</ul>
43+
</div>

0 commit comments

Comments
 (0)