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

05 Apr, 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.
Method – 2: (Sort and the search)
1) Sort the array2) 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. 
C++
Java
Python3
C#
PHP
Javascript
// CPP program to find value if we double// the value after every successful search#include <bits/stdc++.h>using namespace std; // Function to Find the value of kint 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 Codeint 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 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
# 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 = 2n = len(arr) print(findValue(arr, n, k)) # This code is contributed# by Anant Agarwal.
// C# program to find value// if we double the value after// every successful searchusing 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// PHP program to find// value if we double// the value after every// successful search // Function to Find// the value of kfunction findValue($arr, $n, $k){     // Search for k. After every    // successful search, double k.    for ($i = 0; $i < $n; $i++)        if ($arr[$i] == $k)        $k *= 2;         return $k;} // Driver Code$arr = array(2, 3, 4, 10, 8, 1);$k = 2;$n = count($arr);echo findValue($arr, $n, $k); // This code is contributed by anuj_67.?>
<script> // JavaScript program to find value// if we double  the value after// every successful search     // Function to Find the value of k    function findValue(arr, n, k)    {          // Search for k. After every successful        // search, double k.        for (let i = 0; i < n; i++)            if (arr[i] == k)                k *= 2;          return k;    }   // Driver code         let arr = [ 2, 3, 4, 10, 8, 1 ], k = 2;        let n = arr.length;        document.write(findValue(arr, n, k)); </script>
Output:
16
Time Complexity : O(nlogn) 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.
C++
Python3
Javascript
// CPP program for the above approach#include <bits/stdc++.h>using namespace std; // Function to find the valueint 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 Codeint 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;}
# Python program for the above approach # Function to find the valuedef 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 Codearr, k = [ 2, 3, 4, 10, 8, 1 ], 2n = len(arr)print(findValue(arr, n, k)) # This code is contributed by shinjanpatra
<script> // JavaScript program for the above approach // Function to find the valuefunction findValue(a, n, k){         // Unordered Map    let m = new Set();       // Iterate from 0 to n - 1    for (let i = 0; i < n; i++)        m.add(a[i]);     while (m.has(k))        k = k * 2;     return k;} // Driver's Code let arr = [ 2, 3, 4, 10, 8, 1 ], k = 2;let n = arr.length;document.write(findValue(arr, n, k)); // This code is contributed by shinjanpatra </script>
Time Complexity: O(n)
Space Complexity: O(n)
?list=PLqM7alHXFySEQDk2MDfbwEdjd2svVJH9pReference:href=”https://www.geeksforgeeks.org/flipkart-interview-experience-set-35-on-campus-for-sde-1/” 
vt_m
ankurcharan
souravghosh0416
surindertarika1234
surinderdawra388
shinjanpatra
Flipkart
Arrays
Searching
Flipkart
Arrays
Searching
Writing code in comment?
Please use ide.geeksforgeeks.org,
generate link and share the link here.
Introduction to Arrays
Multidimensional Arrays in Java
Linked List vs Array
Given an array A[] and a number x, check for pair in A[] with sum as x (aka Two Sum)
Python | Using 2D arrays/lists the right way
Binary Search
Search an element in a sorted and rotated array
Find the Missing Number
K'th Smallest/Largest Element in Unsorted Array | Set 1
Program to find largest element in an array