Find final value if we double after every successful search in array
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)
- Traverse each element of an array if arr[i] == k then k = 2 * k.
- Repeat the same process for the max value of k.
- At last Return the value of k.
Implementation:
C++
#include <bits/stdc++.h>
using namespace std;
int findValue( int a[], int n, int k)
{
bool exist = true ;
while (exist){
exist = false ;
for ( int i = 0; i < n; i++) {
if (a[i] == k){
k *= 2;
exist = true ;
break ;
}
}
}
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;
}
|
Java
class GFG
{
static int findValue( int arr[], int n, int k)
{
boolean exist = true ;
while (exist){
exist = false ;
for ( int i = 0 ; i < n; i++) {
if (arr[i] == k){
k *= 2 ;
exist = true ;
break ;
}
}
}
return k;
}
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));
}
}
|
C#
using System;
public class GFG {
static int findValue( int [] a, int n, int k)
{
bool exist = true ;
while (exist) {
exist = false ;
for ( int i = 0; i < n; i++) {
if (a[i] == k) {
k *= 2;
exist = true ;
break ;
}
}
}
return k;
}
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));
}
}
|
Python3
def findValue(a, n, k):
exist = True
while exist:
exist = False
for i in range (n):
if a[i] = = k:
k * = 2
exist = True
break
return k
arr = [ 2 , 3 , 4 , 10 , 8 , 1 ]
k = 2
n = len (arr)
print (findValue(arr, n, k))
|
Javascript
<script>
function findValue(arr, n, k)
{
var exist = true ;
while (exist){
exist = false ;
for (let i = 0; i < n; i++){
if (arr[i] == k) {
k *= 2;
exist = true ;
break ;
}
}
}
return k;
}
let arr = [ 2, 3, 4, 10, 8, 1 ], k = 2;
let n = arr.length;
document.write(findValue(arr, n, k));
</script>
|
Time Complexity : O(n^2)
Auxiliary Space: O(1)
Method – 2: (Sort and the search)
- Sort the array
- 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++
#include <bits/stdc++.h>
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;
}
|
Java
class GFG {
static int findValue( int arr[], int n, int k)
{
for ( int i = 0 ; i < n; i++)
if (arr[i] == k)
k *= 2 ;
return k;
}
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));
}
}
|
Python3
def findValue(arr, n, k):
for i in range (n):
if (arr[i] = = k):
k = k * 2
return k
arr = [ 2 , 3 , 4 , 10 , 8 , 1 ]
k = 2
n = len (arr)
print (findValue(arr, n, k))
|
C#
using System;
class GFG {
static int findValue( int [] arr, int n, int k)
{
for ( int i = 0; i < n; i++)
if (arr[i] == k)
k *= 2;
return k;
}
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));
}
}
|
PHP
<?php
function findValue( $arr , $n , $k )
{
for ( $i = 0; $i < $n ; $i ++)
if ( $arr [ $i ] == $k )
$k *= 2;
return $k ;
}
$arr = array (2, 3, 4, 10, 8, 1);
$k = 2;
$n = count ( $arr );
echo findValue( $arr , $n , $k );
?>
|
Javascript
<script>
function findValue(arr, n, k)
{
for (let i = 0; i < n; i++)
if (arr[i] == k)
k *= 2;
return k;
}
let arr = [ 2, 3, 4, 10, 8, 1 ], k = 2;
let n = arr.length;
document.write(findValue(arr, n, k));
</script>
|
Time Complexity : O(nlogn)
Auxiliary Space: O(1)
Method – 3: (Hashing)
- Put all elements in hashmap.
- Search if k is in hashmap, If it is then multiply the value by k or return value of k.
Implementation:
C++
#include <bits/stdc++.h>
using namespace std;
int findValue( int a[], int n, int k)
{
unordered_set< int > m;
for ( int i = 0; i < n; i++)
m.insert(a[i]);
while (m.find(k) != m.end())
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;
}
|
Java
import java.io.*;
import java.util.*;
class GFG {
static int findValue( int [] a, int n, int k){
HashSet<Integer> m = new HashSet<Integer>();
for ( int i= 0 ;i<n;i++){
m.add(a[i]);
}
while (m.contains(k)){
k = k * 2 ;
}
return k;
}
public static void main(String args[]){
int [] arr = { 2 , 3 , 4 , 10 , 8 , 1 };
int k = 2 ;
int n = arr.length;
System.out.println(findValue(arr, n, k));
}
}
|
Python3
def findValue(a, n, k):
m = set ()
for i in range (n):
m.add(a[i])
while (k in m):
k = k * 2
return k
arr, k = [ 2 , 3 , 4 , 10 , 8 , 1 ], 2
n = len (arr)
print (findValue(arr, n, k))
|
C#
using System;
using System.Collections.Generic;
class GFG {
static int findValue( int [] a, int n, int k)
{
HashSet< int > m = new HashSet< int >();
for ( int i = 0; i < n; i++) {
m.Add(a[i]);
}
while (m.Contains(k)) {
k = k * 2;
}
return k;
}
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));
}
}
|
Javascript
<script>
function findValue(a, n, k)
{
let m = new Set();
for (let i = 0; i < n; i++)
m.add(a[i]);
while (m.has(k))
k = k * 2;
return k;
}
let arr = [ 2, 3, 4, 10, 8, 1 ], k = 2;
let n = arr.length;
document.write(findValue(arr, n, k));
</script>
|
Time Complexity: O(n)
Space Complexity: O(n)
Reference: “https://www.geeksforgeeks.org/flipkart-interview-experience-set-35-on-campus-for-sde-1/”
Last Updated :
19 Sep, 2023
Like Article
Save Article
Share your thoughts in the comments
Please Login to comment...