# Find final value if we double after every successful search in array

• Difficulty Level : Basic
• Last Updated : 21 Sep, 2022

Given an array and an integer k, traverse the array and if the element in array is k, double the value of k and continue traversal. In the end return value of k.

Examples:

```Input : arr[] = { 2, 3, 4, 10, 8, 1 }, k = 2
Output: 16
Explanation:
First k = 2 is found, then we search for 4
which is also found, then we search for 8
which is also found, then we search for 16.

Input : arr[] = { 2, 4, 5, 6, 7 }, k = 3
Output: 3```

Method – 1: (Brute-force)

1. Traverse each element of an array if arr[i] == k then k = 2 * k.
2. Repeat the same process for the max value of k.
3. At last Return the value of k.

Implementation:

## C++

 `// C++ program to find value if we double``// the value after every successful search``#include ``using` `namespace` `std;` `// Function to Find the value of k``int` `findValue(``int` `a[], ``int` `n, ``int` `k)``{` `    ``bool` `exist = ``true``;``    ` `    ``// Search for k. After every successful``    ``// search, double k and change exist to true``    ``// and search again for k from the start of array``    ` `    ``while``(exist){``        ` `        ``exist = ``false``;``        ` `        ``for` `(``int` `i = 0; i < n; i++) {``            ` `            ``// Check is a[i] is equal to k``            ``if` `(a[i] == k){``                ``k *= 2;``                ``exist = ``true``;``                ``break``;``            ``}``        ``}``        ` `    ``}` `    ``return` `k;``}` `// Driver's Code``int` `main()``{``    ``int` `arr[] = { 2, 3, 4, 10, 8, 1 }, k = 2;``    ``int` `n = ``sizeof``(arr) / ``sizeof``(arr[0]);``    ` `    ``cout << findValue(arr, n, k);``    ` `    ``return` `0;``}`

## Java

 `// Java program to find value``// if we double  the value after``// every successful search``class` `GFG``{` `  ``// Function to Find the value of k``  ``static` `int` `findValue(``int` `arr[], ``int` `n, ``int` `k)``  ``{` `    ``boolean` `exist = ``true``;` `    ``// Search for k. After every successful``    ``// search, double k and change exist to true``    ``// and search again for k from the start of array` `    ``while``(exist){` `      ``exist = ``false``;` `      ``for` `(``int` `i = ``0``; i < n; i++) {` `        ``// Check is a[i] is equal to k``        ``if` `(arr[i] == k){``          ``k *= ``2``;``          ``exist = ``true``;``          ``break``;``        ``}``      ``}` `    ``}` `    ``return` `k;``  ``}` `  ``// Driver Code``  ``public` `static` `void` `main(String[] args)``  ``{``    ``int` `arr[] = { ``2``, ``3``, ``4``, ``10``, ``8``, ``1` `}, k = ``2``;``    ``int` `n = arr.length;``    ``System.out.print(findValue(arr, n, k));``  ``}``}` `// This code is contributed by Aarti_Rathi`

## C#

 `using` `System;``/*``  ``C# program to find value if we double``  ``the value after every successful search``*/``public` `class` `GFG {``    ``// Function to Find the value of k``    ``static` `int` `findValue(``int``[] a, ``int` `n, ``int` `k)``    ``{` `        ``bool` `exist = ``true``;` `        ``// Search for k. After every successful``        ``// search, double k and change exist to true``        ``// and search again for k from the start of array` `        ``while` `(exist) {` `            ``exist = ``false``;` `            ``for` `(``int` `i = 0; i < n; i++) {` `                ``// Check is a[i] is equal to k``                ``if` `(a[i] == k) {``                    ``k *= 2;``                    ``exist = ``true``;``                    ``break``;``                ``}``            ``}``        ``}` `        ``return` `k;``    ``}` `    ``// Driver Code``    ``public` `static` `void` `Main()``    ``{``        ``int``[] arr = { 2, 3, 4, 10, 8, 1 };``        ``int` `k = 2;``        ``int` `n = arr.Length;` `        ``Console.WriteLine(findValue(arr, n, k));``    ``}``}``// This code is contributed by Aarti_Rathi`

## Python3

 `#  Python program to find value if we double``#  the value after every successful search` `# Function to Find the value of k``def` `findValue(a, n, k):` `    ``exist ``=` `True` `    ``while` `exist:``        ``#  Search for k. After every successful``        ``#  search, double k and change exist to true``        ``# and search again for k from the start of array``        ``exist ``=` `False` `        ``for` `i ``in` `range``(n):``            ``# Check is a[i] is equal to k``            ``if` `a[i] ``=``=` `k:``                ``k ``*``=` `2``                ``exist ``=` `True``                ``break` `    ``return` `k`  `# Driver's Code``arr ``=` `[``2``, ``3``, ``4``, ``10``, ``8``, ``1``]``k ``=` `2``n ``=` `len``(arr)``print``(findValue(arr, n, k))`

## Javascript

 ``

Output

`16`

Time Complexity : O(n^2)

Auxiliary Space: O(1)

Method – 2: (Sort and the search)

1. Sort the array
2. Then you can just search for the element in one loop because we are sure that k*2 would be after k in this array. Therefore,  just multiply the value of k there in the loop only.

Implementation:

## C++

 `// CPP program to find value if we double``// the value after every successful search``#include ``using` `namespace` `std;` `// Function to Find the value of k``int` `findValue(``int` `a[], ``int` `n, ``int` `k)``{` `    ``// Sort the array``    ``sort(a, a + n);` `    ``// Search for k. After every successful``    ``// search, double k.``    ``for` `(``int` `i = 0; i < n; i++) {``        ` `        ``// Check is a[i] is equal to k``        ``if` `(a[i] == k)``            ``k *= 2;``    ``}` `    ``return` `k;``}` `// Driver's Code``int` `main()``{``    ``int` `arr[] = { 2, 3, 4, 10, 8, 1 }, k = 2;``    ``int` `n = ``sizeof``(arr) / ``sizeof``(arr[0]);``    ``cout << findValue(arr, n, k);``    ``return` `0;``}`

## Java

 `// Java program to find value``// if we double  the value after``// every successful search` `class` `GFG {``    ``// Function to Find the value of k``    ``static` `int` `findValue(``int` `arr[], ``int` `n, ``int` `k)``    ``{` `        ``// Search for k. After every successful``        ``// search, double k.``        ``for` `(``int` `i = ``0``; i < n; i++)``            ``if` `(arr[i] == k)``                ``k *= ``2``;` `        ``return` `k;``    ``}` `    ``// Driver Code``    ``public` `static` `void` `main(String[] args)``    ``{``        ``int` `arr[] = { ``2``, ``3``, ``4``, ``10``, ``8``, ``1` `}, k = ``2``;``        ``int` `n = arr.length;``        ``System.out.print(findValue(arr, n, k));``    ``}``}``// This code is contributed by``// Smitha Dinesh Semwal`

## Python3

 `# Python program to find``# value if we double``# the value after every``# successful search` `# Function to Find the value of k`  `def` `findValue(arr, n, k):` `    ``# Search for k.``    ``# After every successful``    ``# search, double k.``    ``for` `i ``in` `range``(n):``        ``if` `(arr[i] ``=``=` `k):``            ``k ``=` `k ``*` `2` `    ``return` `k` `# Driver's Code`  `arr ``=` `[``2``, ``3``, ``4``, ``10``, ``8``, ``1``]``k ``=` `2``n ``=` `len``(arr)` `print``(findValue(arr, n, k))` `# This code is contributed``# by Anant Agarwal.`

## C#

 `// C# program to find value``// if we double the value after``// every successful search``using` `System;` `class` `GFG {` `    ``// Function to Find the value of k``    ``static` `int` `findValue(``int``[] arr, ``int` `n, ``int` `k)``    ``{` `        ``// Search for k. After every successful``        ``// search, double k.``        ``for` `(``int` `i = 0; i < n; i++)``            ``if` `(arr[i] == k)``                ``k *= 2;` `        ``return` `k;``    ``}` `    ``// Driver Code``    ``public` `static` `void` `Main()``    ``{``        ``int``[] arr = { 2, 3, 4, 10, 8, 1 };``        ``int` `k = 2;``        ``int` `n = arr.Length;` `        ``Console.WriteLine(findValue(arr, n, k));``    ``}``}` `// This code is contributed by vt_m.`

## PHP

 ``

## Javascript

 ``

Output

`16`

Time Complexity : O(nlogn)

Auxiliary Space: O(1)

Method – 3: (Hashing)

1. Put all elements in hashmap.
2. Search if k is in hashmap, If it is then multiply the value by k or return value of k.

Implementation:

## C++

 `// CPP program for the above approach``#include ``using` `namespace` `std;` `// Function to find the value``int` `findValue(``int` `a[], ``int` `n, ``int` `k)``{``    ` `    ``// Unordered Map``    ``unordered_set<``int``> m;``  ` `    ``// Iterate from 0 to n - 1``    ``for` `(``int` `i = 0; i < n; i++)``        ``m.insert(a[i]);` `    ``while` `(m.find(k) != m.end())``        ``k = k * 2;` `    ``return` `k;``}` `// Driver's Code``int` `main()``{``    ``int` `arr[] = { 2, 3, 4, 10, 8, 1 }, k = 2;``    ``int` `n = ``sizeof``(arr) / ``sizeof``(arr[0]);``    ``cout << findValue(arr, n, k);``    ``return` `0;``}`

## Java

 `/*package whatever //do not write package name here */``import` `java.io.*;``import` `java.util.*;``class` `GFG {``  ``static` `int` `findValue(``int``[] a,``int` `n,``int` `k){` `    ``// Unordered set``    ``HashSet m = ``new` `HashSet();` `    ``// Iterate from 0 to n - 1``    ``for``(``int` `i=``0``;i

## Python3

 `# Python program for the above approach` `# Function to find the value``def` `findValue(a, n, k):``    ` `    ``# Unordered Map``    ``m ``=` `set``()``  ` `    ``# Iterate from 0 to n - 1``    ``for` `i ``in` `range``(n):``        ``m.add(a[i])` `    ``while` `(k ``in` `m):``        ``k ``=` `k ``*` `2` `    ``return` `k` `# Driver's Code``arr, k ``=` `[ ``2``, ``3``, ``4``, ``10``, ``8``, ``1` `], ``2``n ``=` `len``(arr)``print``(findValue(arr, n, k))` `# This code is contributed by shinjanpatra`

## C#

 `// C# program for the above approach``using` `System;``using` `System.Collections.Generic;` `class` `GFG {``    ``static` `int` `findValue(``int``[] a, ``int` `n, ``int` `k)``    ``{``        ``// Unordered set``        ``HashSet<``int``> m = ``new` `HashSet<``int``>();` `        ``// Iterate from 0 to n - 1``        ``for` `(``int` `i = 0; i < n; i++) {``            ``m.Add(a[i]);``        ``}``        ``while` `(m.Contains(k)) {``            ``k = k * 2;``        ``}``        ``return` `k;``    ``}``    ``// Drivers code``    ``public` `static` `void` `Main(``string``[] args)``    ``{``        ``int``[] arr = { 2, 3, 4, 10, 8, 1 };``        ``int` `k = 2;``        ``int` `n = arr.Length;``        ``Console.WriteLine(findValue(arr, n, k));``    ``}``}` `// This code is contributed by Tapesh(tapeshdua420)`

## Javascript

 ``

Output

`16`

Time Complexity: O(n)
Space Complexity: O(n)

My Personal Notes arrow_drop_up