-
Notifications
You must be signed in to change notification settings - Fork 1k
/
Copy pathDoubling_the_value.cpp
79 lines (71 loc) · 1.64 KB
/
Doubling_the_value.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
/*
Description :
Given an array and an integer target, traverse the array (from the beginning)
and if the element in array is target, double the tagret and continue traversal.
Find the value of B after the complete traversal.
*/
#include <bits/stdc++.h>
using namespace std;
class Solution
{
public:
//function for finding the value after complete traversal
long long sol(int n, long long arr[], long long target)
{
sort(arr, arr + n);
long long maxi = arr[n - 1];
while (target <= maxi)
{
//in-built method
if (binary_search(arr, arr + n, target))
{
target = target * 2;
}
else
{
return target;
}
}
return target;
}
};
int main()
{
// size of an array
int size;
cout << "Enter the size of an array : " << endl;
cin >> size;
//element to be searched and then traversed
int target;
cout << "Enter the target element : " << endl;
cin >> target;
long long arr[size];
cout << "Enter the data in the array : " << endl;
for (int i = 0; i < size; i++)
{
cin >> arr[i];
}
Solution obj;
long long result = obj.sol(size, arr, target);
cout << "Value of target after complete traversal : " << endl;
//output
cout << result << endl;
return 0;
}
/*
Time complexity : O(n)
Space complexity : O(n)
*/
/*
Test Case:
Input :
"Enter the size of an array :
5
Enter the target element :
2
Enter the data in the array :
1 2 3 4 8
Output :
Value of target after complete traversal :
16
*/