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
#include <bits/stdc++.h>
using namespace std;
int solve(int b, vector<int> &arr)
{
// Traverse each element in the array
for (int x : arr)
{
// Check if current element equals b
if (x == b)
{
// Double the value of b
b = b * 2;
}
}
// Return final value of b
return b;
}
int main()
{
// Initial value of b
int b = 2;
// Input array
vector<int> arr = {1, 2, 3, 4, 8};
cout << solve(b, arr);
return 0;
}
#include <stdio.h>
// Function to compute final value of b
int solve(int b, int arr[], int n) {
// Traverse the array
for (int i = 0; i < n; i++) {
// Check if element equals b
if (arr[i] == b) {
// Double b
b = b * 2;
}
}
// Return final b
return b;
}
int main() {
int b = 2;
int arr[] = {1, 2, 3, 4, 8};
int n = sizeof(arr) / sizeof(arr[0]);
printf("%d", solve(b, arr, n));
return 0;
}
import java.util.Arrays;
import java.util.List;
public class GfG {
// Method to solve the problem
static int solve(int b, List<Integer> arr) {
// Traverse each element in the array
for (int x : arr) {
// Check if current element equals b
if (x == b) {
// Double the value of b
b = b * 2;
}
}
// Return final value of b
return b;
}
public static void main(String[] args) {
int b = 2;
List<Integer> arr = Arrays.asList(1, 2, 3, 4, 8);
System.out.println(solve(b, arr));
}
}
class Solution:
def solve(self, b, arr):
# Traverse each element in array
for x in arr:
# Check if element equals b
if x == b:
# Double b
b = b * 2
# Return final value
return b
# Driver code
if __name__ == "__main__":
b = 2
arr = [1, 2, 3, 4, 8]
obj = Solution()
print(obj.solve(b, arr))
using System;
class GfG {
public int solve(int b, int[] arr) {
// Traverse each element
foreach (int x in arr) {
// Check if element equals b
if (x == b) {
// Double b
b = b * 2;
}
}
return b;
}
static void Main() {
int b = 2;
int[] arr = {1, 2, 3, 4, 8};
GfG obj = new GfG();
Console.WriteLine(obj.solve(b, arr));
}
}
class Solution {
solve(b, arr) {
// Traverse each element
for (let x of arr) {
// Check if element equals b
if (x === b) {
// Double b
b = b * 2;
}
}
return b;
}
}
// Driver code
function main() {
let b = 2;
let arr = [1, 2, 3, 4, 8];
let obj = new Solution();
console.log(obj.solve(b, arr));
}
main();
Output
16