| | | 1 | | // -------------------------------------------------------------------------------- |
| | | 2 | | // Copyright (C) 2026 Eugene Eremeev (also known as Yevhenii Yeriemeieiv). |
| | | 3 | | // All Rights Reserved. |
| | | 4 | | // -------------------------------------------------------------------------------- |
| | | 5 | | // This software is the confidential and proprietary information of Eugene Eremeev |
| | | 6 | | // (also known as Yevhenii Yeriemeieiv) ("Confidential Information"). You shall not |
| | | 7 | | // disclose such Confidential Information and shall use it only in accordance with |
| | | 8 | | // the terms of the license agreement you entered into with Eugene Eremeev (also |
| | | 9 | | // known as Yevhenii Yeriemeieiv). |
| | | 10 | | // -------------------------------------------------------------------------------- |
| | | 11 | | |
| | | 12 | | namespace LeetCode.Algorithms.ConstructTheMinimumBitwiseArray2; |
| | | 13 | | |
| | | 14 | | /// <inheritdoc /> |
| | | 15 | | public sealed class ConstructTheMinimumBitwiseArray2BitManipulation : IConstructTheMinimumBitwiseArray2 |
| | | 16 | | { |
| | | 17 | | /// <summary> |
| | | 18 | | /// Time complexity - O(n log m) |
| | | 19 | | /// Space complexity - O(1) |
| | | 20 | | /// </summary> |
| | | 21 | | /// <param name="nums"></param> |
| | | 22 | | /// <returns></returns> |
| | | 23 | | public int[] MinBitwiseArray(IList<int> nums) |
| | 2 | 24 | | { |
| | 2 | 25 | | var n = nums.Count; |
| | | 26 | | |
| | 2 | 27 | | var result = new int[n]; |
| | | 28 | | |
| | 18 | 29 | | for (var i = 0; i < n; i++) |
| | 7 | 30 | | { |
| | 7 | 31 | | result[i] = FindMinValue(nums[i]); |
| | 7 | 32 | | } |
| | | 33 | | |
| | 2 | 34 | | return result; |
| | 2 | 35 | | } |
| | | 36 | | |
| | | 37 | | private static int FindMinValue(int num) |
| | 7 | 38 | | { |
| | 7 | 39 | | if (num == 2) |
| | 1 | 40 | | { |
| | 1 | 41 | | return -1; |
| | | 42 | | } |
| | | 43 | | |
| | 6 | 44 | | var t = 0; |
| | | 45 | | |
| | 20 | 46 | | while (((num >> t) & 1) == 1) |
| | 14 | 47 | | { |
| | 14 | 48 | | t++; |
| | 14 | 49 | | } |
| | | 50 | | |
| | 6 | 51 | | return num - (1 << (t - 1)); |
| | 7 | 52 | | } |
| | | 53 | | } |