leetcode

Leetcode Solutions (0x6A73)
git clone https://git.pngu.org/leetcode
Log | Files | Refs | Submodules | README

commit 8aabdc3dd02e7a09f1d8b29ab73111335356b95b
parent c178e985e197da9b8435fa5982a887301952044f
Author: Jack Sangdahl <jack@pngu.org>
Date:   Tue, 18 Nov 2025 20:46:23 -0700

2154. keep multiplying found values by two

Diffstat:
Akeep_multiplying_val_two.c | 29+++++++++++++++++++++++++++++
Akeep_multiplying_val_two.py | 33+++++++++++++++++++++++++++++++++
2 files changed, 62 insertions(+), 0 deletions(-)

diff --git a/keep_multiplying_val_two.c b/keep_multiplying_val_two.c @@ -0,0 +1,29 @@ +// 2154. Keep Multiplying Found Values by Two +#include "leetcode.h" + +/* + * you are given an array of integers 'nums'. you are also given an integer + * 'original' which is the first number that needs to be searched for in 'nums'. + * you then do the following: if 'original' is found in 'nums', multiply it by + * two, otherwise stop the process. repeat this process with the new number as + * long as you keep finding the number. return the final value of 'original' + */ + +int findFinalValue(int *nums, int numsSize, int original) { + int bits = 0; + for (int i = 0; i < numsSize; i++) { + if (nums[i] % original) + continue; + nums[i] /= original; + if (!(nums[i] & (nums[i] - 1))) + bits |= nums[i]; + } + int ans = bits + 1; + return original * (ans & -ans); +} + +int main() { + int n1[] = {5, 3, 6, 1, 12}, n2[] = {2, 7, 9}; + printf("%d\n", findFinalValue(n1, ARRAY_SIZE(n1), 3)); // expect: 24 + printf("%d\n", findFinalValue(n2, ARRAY_SIZE(n2), 4)); // expect: 4 +} diff --git a/keep_multiplying_val_two.py b/keep_multiplying_val_two.py @@ -0,0 +1,33 @@ +# 2154. Keep Multiplying Found Values by Two + +""" +you are given an array of integers 'nums'. you are also given an integer +'original' which is the first number that needs to be searched for in 'nums'. +you then do the following: if 'original' is found in 'nums', multiply it by +two, otherwise stop the process. repeat this process with the new number as +long as you keep finding the number. return the final value of 'original' +""" + + +class Solution(object): + def findFinalValue(self, nums, original): + """ + :type nums: List[int] + :type original: int + :rtype: int + """ + bits = 0 + for i in nums: + if i % original != 0: + continue + n = i // original + if n & (n - 1) == 0: + bits |= n + ans = bits + 1 + return original * (ans & -ans) + + +if __name__ == "__main__": + obj = Solution() + print(obj.findFinalValue(nums=[5, 3, 6, 1, 12], original=3)) + print(obj.findFinalValue(nums=[2, 7, 9], original=4))