Find final value if we double after every successful search in array | CPP program to find value if we double the value after every successful search ; Function to Find the value of k ; Sort the array ; Search for k . After every successful search , double k . ; Driver 's Code
#include <bits/stdc++.h> NEW_LINE using namespace std ; int findValue ( int a [ ] , int n , int k ) { sort ( a , a + n ) ; for ( int i = 0 ; i < n ; i ++ ) { if ( a [ i ] == k ) k *= 2 ; } return k ; } 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 ; }