forked from cyberpvn7/Cpp_Lang
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path51code.java
More file actions
43 lines (35 loc) · 803 Bytes
/
51code.java
File metadata and controls
43 lines (35 loc) · 803 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
// C++ program for the above approach
#include <bits/stdc++.h>
using namespace std;
// Function to find the last remaining
// array element after repeatedly removing
// the smallest from pairs having absolute
// difference 2 or 0
void findLastElement(int arr[], int N)
{
// Sort the given array in
// ascending order
sort(arr, arr + N);
int i = 0;
// Traverse the array
for (i = 1; i < N; i++) {
// If difference between
// adjacent elements is
// not equal to 0 or 2
if (arr[i] - arr[i - 1] != 0
&& arr[i] - arr[i - 1] != 2) {
cout << "-1" << endl;
return;
}
}
// If operations can be performed
cout << arr[N - 1] << endl;
}
// Driver Code
int main()
{
int arr[] = { 2, 4, 6, 8, 0, 8 };
int N = sizeof(arr) / sizeof(arr[0]);
findLastElement(arr, N);
return 0;
}