Doubling the value | Practice | GeeksforGeeks

Given an array and an integer B, traverse the array (from the beginning) and if the element in array is B, double B and continue traversal. Find the value of B after the complete traversal.
Example 1:
Input:
N = 5, 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 at the 3rd index, hence B 
becomes 8. Next, we get B at 4-th index, 
hence B becomes 16.

Example 1:
Input:
N = 5, B = 3
arr[] = {1 2 3 4 8}
Output: 6
Explanation: B is initially 3. We get 3 at
the 2nd index, hence B becomes 6.

Your Task:
You don't need to read input or print anything. Your task is to complete the function solve () which takes the array arr[], its size N and an integer B as inputs and returns the final value of B after the complete traversal of the array.

Expected Time Complexity: O(N).
Expected Auxiliary Space: O(1).

Constraints:
1<=N<=50
1<=B<=1000
1<=arr[i]<=1018
0
tejudupi2 months ago
def solve(self,n : int, a : list, b : int):       # Complete this function       for i in range(n):           if arr[i]== b:               b =2 * b       return b 
0
ramdurgasais3 months ago
for item in a : 
        if item == b : b *= 2
return b 
0
ayazmrz983 months ago
    long solve(int n, long A[], long b)
   {
       for(int i=0;i<n;i++)
       {
           if(A[i]==b)
           {
               b=b*2;
           }
       }
       
       return b;
   }
0
pjodhani33 months ago
long long solve(int n, long long a[], long long b)   {       for(int i=0; i< n; i++)       {           if(a[i] == b)           {               b= 2*b;           }           }              return b;   }  
0
diptendunandi3 months ago
long long solve(int n, long long arr[], long long b)
    {
        //code here.
        for(long long i=0;i<n;i++){
            if(arr[i]==b){
                b=b*2;
            }
        }
        return b;
    }
0
abythankachan3143 months ago
Python code:
def solve(self,n:int,a:list,b:int):
   for t in a:
       if t==b:
           b=b*2
   return b
0
indrathegodofthunder3 months ago
////.   PYTHON  ////
 
 
class Solution:   def solve(self,n : int, a : list, b : int):       sorted(a)       for num in a:           if num == b:               b = b * 2       return b 
0
thanhtai016263 months ago
Majority. It only success when array sorted?
0
mukulvyasg4 months ago
python
 
class Solution:
   def solve(self,n : int, a : list, b : int):
       a.sort()
       while b in a  :
           b = 2*b
       return b
0
nikhillko024 months ago
 def solve(self,n : int, a : list, b : int):       # Complete this function       for i in a:           if i==b:               b=2*b       return b
We strongly recommend solving this problem on your own before viewing its editorial. Do you still
                    want to view the editorial?
 Login to access your submissions. 

Problem


Contest

Reset the IDE using the second button on the top right corner.
Avoid using static/global variables in your code as your code is tested against multiple test cases and these tend to retain their previous values.
Passing the Sample/Custom Test cases does not guarantee the correctness of code. On submission, your code is tested against multiple test cases consisting of all possible corner cases and stress constraints.
You can access the hints to get an idea about what is expected of you as well as the final solution code.
You can view the solutions submitted by other users from the submission tab.