-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathDifferenceof2.cpp
More file actions
74 lines (65 loc) · 1.96 KB
/
Differenceof2.cpp
File metadata and controls
74 lines (65 loc) · 1.96 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
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
#include<iostream>
#include<algorithm>
#include<cmath>
#include<vector>
#include <string>
#include <sstream>
#include <climits>
#include<iomanip>
using namespace std;
vector<pair<int, int>> twos_difference(const vector<int>& vec) {
vector<pair<int, int>>result;
pair<int, int>resulttemp;
vector<int>temp = vec;
sort(temp.begin(), temp.end());
for (int i = 0; i < vec.size(); i++)
{
for (int j = 0; j < vec.size(); j++)
{
if (temp[i] - temp[j] == 2)
{
resulttemp.first = temp[i];
resulttemp.second = temp[j];
result.push_back(resulttemp);
}
}
}
return result;
}
int main()
{
void print_result(const vector<pair<int, int>>&result) {
for (const auto& p : result) {
cout << "(" << p.first << ", " << p.second << ") ";
}
cout << endl;
vector<pair<int, int>>ss = twos_difference({ 1, 2, 3, 4 });
for (int i = 0; i < ss.size(); i++)
{
cout << ss[i] << " ";
}
return 0;
}
/*Description:
The objective is to return all pairs of integers from a given array of integers that have a difference of 2.
The result array should be sorted in ascending order of values.
Assume there are no duplicate integers in the array.
The order of the integers in the input array should not matter.
Examples
[1, 2, 3, 4] should return [[1, 3], [2, 4]]
[4, 1, 2, 3] should also return [[1, 3], [2, 4]]
[1, 23, 3, 4, 7] should return [[1, 3]]
[4, 3, 1, 5, 6] should return [[1, 3], [3, 5], [4, 6]]
Arrays
Sorting
Algorithms*/
// /> フ
// | n n 彡
// /`ミ_xノ
// / |
// / ヽ ノ
// │ | | |
// / ̄| | | |
// | ( ̄ヽ__ヽ_)__)
// \二つ
// ITS CAT FOR YOU