Given an integer array nums and an initial integer b, traverse nums from left to right.
For each element, if it equals the current value of b, double b (set b = b * 2). Otherwise, leave b unchanged.
Return the final value of b after processing every element of the array.
Function Signature
def final_value(nums: list[int], b: int) -> int:
Rules
-
Each element is compared with the value
b
has when the scan reaches that element. Elements already passed are never checked again after
b
changes.
Constraints
-
1 <= len(nums) <= 10^5
-
-10^9 <= nums[i] <= 10^9
-
-10^9 <= b <= 10^9
Examples
Input: nums = [2,4,8,16], b = 2
Output: 32
Start with b = 2. Seeing 2 makes it 4, seeing 4 makes it 8, seeing 8 makes it 16, and seeing 16 makes it 32.
Input: nums = [1,3,5,7], b = 2
Output: 2
No element ever equals 2, so b stays 2.
Input: nums = [8,2,4,2,8], b = 2
Output: 16
The first 8 is passed while b is still 2, so it does not count. Then 2 makes b 4, 4 makes it 8, the second 2 does not match, and the last 8 makes it 16.