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
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
1- Traverse each element of an array if arr[i] == k then k = 2 * k
2- Repeat the same process till last element of an array
3- At last Return the value of k
C++
// 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 k 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'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 contriubted 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 = 2n = 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 contriubted by vt_m. |
PHP
<?php // PHP 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 ($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 contriubted by anuj_67. ?> |
Output: 16
Time Complexity : O(n)
Reference:href=”https://www.geeksforgeeks.org/flipkart-interview-experience-set-35-on-campus-for-sde-1/”
Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the DSA Self Paced Course at a student-friendly price and become industry ready.
Recommended Posts:
- Repeatedly search an element by doubling it after every successful search
- Find the final sequence of the array after performing given operations
- Find the final radiations of each Radiated Stations
- Find the final number obtained after performing the given operation
- Search in an almost sorted array
- Saddleback Search Algorithm in a 2D array
- Front and Back Search in unsorted array
- Efficient search in an array where difference between adjacent is 1
- Create a Sorted Array Using Binary Search
- Search an element in a sorted and rotated array
- Search, insert and delete in an unsorted array
- Search, insert and delete in a sorted array
- Check if an array is sorted and rotated using Binary Search
- Recursive program to linearly search an element in a given array
- Search an element in a sorted and rotated array with duplicates
- Search an element in an array where difference between adjacent elements is 1
- Search an element in an unsorted array using minimum number of comparisons
- Queries to search an element in an array and move it to front after every query
- Search equal, bigger or smaller in a sorted array in Java
- Maximum height of the binary search tree created from the given array
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.
Improved By : vt_m

