![]() |
VOOZH | about |
Given an array arr[] and an integer b, traverse the array from left to right. If any element equals b, double b. Return the final value of b. after traversal.
Examples:
Input: b = 2, arr[] = [1, 2, 3, 4, 8]
Output: 16
Explanation: b is initially 2. We get 2 at the 1st index, hence b becomes 4.
Next, we get b 3rd time, hence b becomes 8.
Next, we get b 4th time, hence b becomes 16.Input: b = 3, arr[] = [1, 2, 3, 4, 8]
Output: 6
Explanation: b is initially 3. We get 3 2nd times, hence b becomes 6.
O(n) Time O(1) SpaceThe idea is to traverse the array from left to right and check each element. If an element equals the current value of b, double b. Continue this for all elements and return the final value of b.
Let us understand with an example:
Input: arr[] = {1, 2, 3, 4, 8}, b = 2
Start traversal from left:
All elements are processed. Final value of B = 16
16