-
Notifications
You must be signed in to change notification settings - Fork 158
Expand file tree
/
Copy pathForm a triangle.cpp
More file actions
44 lines (30 loc) · 793 Bytes
/
Form a triangle.cpp
File metadata and controls
44 lines (30 loc) · 793 Bytes
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
/*
Given an unsorted positive integer array, find three integers to form a triangle.
*/
/*
solution: sort the array at first. Scan the array once to check a1+a2>a3, return.
O(nlogn) time, O(1) space
*/
#include<iostream>
#include<vector>
#include<algorithm>
using namespace std;
bool FindTriangle(vector<int> arr) {
if (arr.size() <3) return false;
sort(arr.begin(), arr.end());
for (int i = 0; i < arr.size()-2; ++i) {
if (arr[i] + arr[i+1] > arr[i+2]) {
return true;
}
}
return false;
}
int main() {
int arr1[] = {1, 4, 6, 3, 8, 2};
int arr2[] = {1, 2, 1, 3};
vector<int> arr1v(arr1, arr1 + sizeof(arr1) / sizeof(int));
vector<int> arr2v(arr2, arr2 + sizeof(arr2) / sizeof(int));
cout<<FindTriangle(arr1v)<<endl;
cout<<FindTriangle(arr2v)<<endl;
return 0;
}