Skip to content

Commit a7cdc8f

Browse files
committed
c++ code solution for array recovery question
1 parent e98b8ae commit a7cdc8f

File tree

3 files changed

+67
-0
lines changed

3 files changed

+67
-0
lines changed
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
{"name":"Local: arrayrecovery","url":"c:\\Users\\CC\\Desktop\\git-anurag\\git-akpro\\leetcode-solutions-1\\arrayrecovery.cpp","tests":[{"id":1666365548858,"input":"","output":""}],"interactive":false,"memoryLimit":1024,"timeLimit":3000,"srcPath":"c:\\Users\\CC\\Desktop\\git-anurag\\git-akpro\\leetcode-solutions-1\\arrayrecovery.cpp","group":"local","local":true}

arrayrecovery.cpp

Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
1+
// shree ganeshay namah
2+
#include <bits/stdc++.h>
3+
using namespace std;
4+
#define ll long long
5+
#define pi (3.141592653589)
6+
#define mod 1000000007
7+
#define ll long long
8+
#define float double
9+
#define pb push_back
10+
#define mp make_pair
11+
#define ff first
12+
#define ss second
13+
#define all(c) c.begin(), c.end()
14+
#define min3(a, b, c) min(c, min(a, b))
15+
#define min4(a, b, c, d) min(d, min(c, min(a, b)))
16+
#define rrep(i, n) for (int i = n - 1; i >= 0; i--)
17+
#define rep(i, n) for (int i = 0; i < n; i++)
18+
#define fast ios_base::sync_with_stdio(false), cin.tie(nullptr), cout.tie(nullptr);
19+
20+
int main()
21+
{
22+
int t;
23+
cin >> t;
24+
while (t--)
25+
{
26+
int n;
27+
cin >> n;
28+
int c = 0;
29+
vector<int> a(n);
30+
vector<int> b(n);
31+
int flag = 0;
32+
for (int i = 0; i < n; i++)
33+
{
34+
cin >> a[i];
35+
}
36+
b[0] = a[0];
37+
for (int i = 1; i < n; i++)
38+
{
39+
if(a[i]==0){
40+
b[i] = b[i-1] + a[i];
41+
continue;
42+
}
43+
if (b[i-1] - a[i] >= 0)
44+
{
45+
flag = 1;
46+
break;
47+
}
48+
else{
49+
b[i] = a[i] + b[i - 1];
50+
}
51+
}
52+
for (int i = 0; i < n; i++)
53+
{
54+
if (flag == 0)
55+
{
56+
cout << b[i] << " ";
57+
}
58+
else{
59+
cout << "-1";
60+
break;
61+
}
62+
}
63+
cout<<endl;
64+
}
65+
return 0;
66+
}

arrayrecovery.exe

58.9 KB
Binary file not shown.

0 commit comments

Comments
 (0)