VOOZH about

URL: https://www.geeksforgeeks.org/dsa/find-final-value-if-we-double-after-every-successful-search-in-array/

⇱ Final Value if Doubles after Every Successful Search - GeeksforGeeks


  • Courses
  • Tutorials
  • Interview Prep

Final Value if Doubles after Every Successful Search

Last Updated : 9 May, 2026

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.

Using Linear Traversal with Doubling - O(n) Time O(1) Space

The 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:

  • At element 1 -> not equal to b (2) -> no change
  • At element 2 -> equal to b -> double b -> b = 4
  • At element 3 -> not equal to b (4) -> no change
  • At element 4 -> equal to b -> double b -> b = 8
  • At element 8 -> equal to b -> double b -> b = 16

All elements are processed. Final value of B = 16


Output
16
Comment
Article Tags: