-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path3_array_modification.cpp
More file actions
48 lines (46 loc) · 1.09 KB
/
Copy path3_array_modification.cpp
File metadata and controls
48 lines (46 loc) · 1.09 KB
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
#include <algorithm>
#include <iostream>
#include <vector>
// Function to check if the arr[] can be
// converted to target[] by replacing
// any element in arr[] by the sum of arr[]
bool isPossible(std::vector<int> target)
{
// sort the target array
std::sort(target.begin(), target.end());
// store length of target into n
int n = target.size();
// check if target[0] is equal to n or not?
if (target[0] != n)
{
return false;
}
// Traverse the array further for checking
// whether array follows the pattern or not ?
for (int i = 1; i < n; i++)
{
if (target[i] != 2 * target[i - 1] - 1)
{
// if an element does not follow the pattern
// return False
return false;
}
}
// After checking all the elements at the last,
// return True
return true;
}
int main()
{
std::vector<int> target = {9, 3, 5};
bool res = isPossible(target);
if (res)
{
std::cout << "YES" << std::endl;
}
else
{
std::cout << "NO" << std::endl;
}
return 0;
}