Find final value if we double after every successful search in array
Last Updated :
19 Sep, 2023
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++
// C++ 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 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
<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)
{
var 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(let 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
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 muditj148.
</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++
// 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 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
<?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 contributed by anuj_67.
?>
JavaScript
<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>
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++
// CPP program for the above approach
#include <bits/stdc++.h>
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<Integer> m = new HashSet<Integer>();
// 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;
System.out.println(findValue(arr, n, k));
}
}
// This code is contributed by shinjanpatra.
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
<script>
// JavaScript program for the above approach
// Function to find the value
function 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)
Reference: "https://www.geeksforgeeks.org/flipkart-interview-experience-set-35-on-campus-for-sde-1/"
Similar Reads
DSA Tutorial - Learn Data Structures and Algorithms DSA (Data Structures and Algorithms) is the study of organizing data efficiently using data structures like arrays, stacks, and trees, paired with step-by-step procedures (or algorithms) to solve problems effectively. Data structures manage how data is stored and accessed, while algorithms focus on
7 min read
Quick Sort QuickSort is a sorting algorithm based on the Divide and Conquer that picks an element as a pivot and partitions the given array around the picked pivot by placing the pivot in its correct position in the sorted array. It works on the principle of divide and conquer, breaking down the problem into s
12 min read
Merge Sort - Data Structure and Algorithms Tutorials Merge sort is a popular sorting algorithm known for its efficiency and stability. It follows the divide-and-conquer approach. It works by recursively dividing the input array into two halves, recursively sorting the two halves and finally merging them back together to obtain the sorted array. Merge
14 min read
Data Structures Tutorial Data structures are the fundamental building blocks of computer programming. They define how data is organized, stored, and manipulated within a program. Understanding data structures is very important for developing efficient and effective algorithms. What is Data Structure?A data structure is a st
2 min read
Bubble Sort Algorithm Bubble Sort is the simplest sorting algorithm that works by repeatedly swapping the adjacent elements if they are in the wrong order. This algorithm is not suitable for large data sets as its average and worst-case time complexity are quite high.We sort the array using multiple passes. After the fir
8 min read
Breadth First Search or BFS for a Graph Given a undirected graph represented by an adjacency list adj, where each adj[i] represents the list of vertices connected to vertex i. Perform a Breadth First Search (BFS) traversal starting from vertex 0, visiting vertices from left to right according to the adjacency list, and return a list conta
15+ min read
Binary Search Algorithm - Iterative and Recursive Implementation Binary Search Algorithm is a searching algorithm used in a sorted array by repeatedly dividing the search interval in half. The idea of binary search is to use the information that the array is sorted and reduce the time complexity to O(log N). Binary Search AlgorithmConditions to apply Binary Searc
15 min read
Insertion Sort Algorithm Insertion sort is a simple sorting algorithm that works by iteratively inserting each element of an unsorted list into its correct position in a sorted portion of the list. It is like sorting playing cards in your hands. You split the cards into two groups: the sorted cards and the unsorted cards. T
9 min read
Array Data Structure Guide In this article, we introduce array, implementation in different popular languages, its basic operations and commonly seen problems / interview questions. An array stores items (in case of C/C++ and Java Primitive Arrays) or their references (in case of Python, JS, Java Non-Primitive) at contiguous
4 min read
Sorting Algorithms A Sorting Algorithm is used to rearrange a given array or list of elements in an order. For example, a given array [10, 20, 5, 2] becomes [2, 5, 10, 20] after sorting in increasing order and becomes [20, 10, 5, 2] after sorting in decreasing order. There exist different sorting algorithms for differ
3 min read