forked from DHEERAJHARODE/Hacktoberfest2024-Open-source-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdecode_array.cpp
51 lines (42 loc) · 1.07 KB
/
decode_array.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
#include <iostream>
#include <cmath>
using namespace std;
// Function to decode given array to get back the original array elements
void decode(int inp[], int m)
{
// base case
if (m == 0 || m == 2) {
return;
}
// calculate the size of the original array
int n = (sqrt(8*m + 1) + 1) / 2;
// create an auxiliary array of size `n` to store elements
// of the original array
int A[n];
// calculate the first element of the original array
if (n == 1 || m == 1) {
A[0] = inp[0];
}
else if (n == 2) {
A[0] = inp[0] - inp[1];
}
else {
A[0] = (inp[0] + inp[1] - inp[n - 1]) / 2;
}
// calculate the remaining elements of the original array using
// the first element
for (int i = 1; i < n; i++) {
A[i] = inp[i - 1] - A[0];
}
// print the original array
for (int i = 0; i < n; i++) {
cout << A[i] << " ";
}
}
int main()
{
int inp[] = { 3, 4, 5, 6, 5, 6, 7, 7, 8, 9 };
int m = sizeof(inp)/sizeof(inp[0]);
decode(inp, m);
return 0;
}