Repeatedly Search for an Element by Doubling it After Every Successful Search Using C++


In this article, we are given an array of integers and a key. We must find the key repeatedly in the array and double it on each find in the array. We need to return the value not present in the array doing this operation.

Some input scenarios to look at to understand how the method works in different cases

Let's have an array [1,2,6,3,7,4,9], and its key is 1.

Input: {1, 2, 3, 4, 5, 6}, k = 1
Result: 8

If we find 1 we double it to 2.

If we find 2 we double it to 4.

If we find 4 we double it to 8.

We return 8 as there is no element 8 in the array

In another case, let's consider an array {2, 3, 7, 8, 5, 9} and its key is 1.

Input: {2, 3, 7, 8, 5, 9}, k = 1
Result: 1

We return 1 as it is, since there is no element 1 in the input array.

Algorithm

  • Sort the elements of the array as performing a binary search takes lower complexity for small arrays.

  • Whenever the element in the array matches with the key value, double the key value and search the array again to find elements that match with the new key.

  • Repeat this step until there is no element in the array that matches with the doubled key value.

  • The final key value is the obtained output.

Example (Using Vector ADT)

We start implementing this method by sorting the array. After that, we will do exactly as the question says; search and double. We search by the binary search for optimization. Let us look at C++ program by applying the same logic −

#include <iostream> #include <algorithm> #include <vector> using namespace std; int solve(vector<int>& arr, int key) { sort(arr.begin(), arr.end()); bool found = binary_search(arr.begin(), arr.end(), key); while(found) { key*=2; found = binary_search(arr.begin(), arr.end(), key); } return key; } int main() { vector<int> arr = {1,2,6,3,7,4,9}; int key = 1; cout << solve(arr, key) << endl; return 0; }

Output

8

Example (Without Using Vector ADT)

The C++ program also follows the same logic but without using the vector abstract data type.

We start implementing this method by sorting the array. After that, we will do exactly as the question says; search and double. We search by the binary search for optimization.

#include <bits/stdc++.h> using namespace std; int SearchElement(int arr[], int n, int k) { // Sorting is done so binary searching in the element // would be easier sort(arr, arr + n); int max = arr[n - 1]; // Declaring the maximum element in the array while (k < max) { // search for the element k in the array if (binary_search(arr, arr + n, k)) k *= 2; else return k; } return k; } int main() { int arr[] = {1,2,6,3,7,4,9}; int n = sizeof(arr) / sizeof(arr[0]); int k = 3; cout << SearchElement(arr, n, k); return 0; }

Output

12

Conclusion

We have used the STL binary search method, which returns true or false based on whether the element is found or not. We could also have used our custom implementation of binary search. STL provides excellent methods of sorting and search, which helped us code the problem without overthinking the implementation.

Updated on: 10-Aug-2022

306 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements