ARRAYRET - Editorial

PROBLEM LINK:

Contest Division 1
Contest Division 2
Contest Division 3
Contest Division 4

Setter: Abhinav Gupta
Tester: Takuki Kurokawa
Editorialist: Yash Kulkarni

DIFFICULTY:

1193

PREREQUISITES:

Basic Math

PROBLEM:

Chef has an array A (1 \leq A_i \leq 10^5) of length N.

Let

pref_i = A_1 + A_2 + \dots A_i

suff_i = A_i + A_{i+1} + \dots A_N

Now Chef created another array B of length N such that B_i = pref_i + suff_i.

Chef lost the original array A and wants to recover it with the help of B. Help Chef to recover the array.

It is guaranteed in the input that there exists a valid array A for the given array B. In case of multiple valid arrays A, output any valid array.

EXPLANATION:

Hint

There exists a unique solution.

Solution

Let us have a look at the elements of array B.
B_i = pref_i + suff_i = (A_1 + A_2 + ... A_i) + (A_i + A_{i+1} + ... A_N) = (A_1 + A_2 + ... A_N) + A_i. Hence B_i = SUM_A + A_i, where SUM_A is the sum of the original array (A).

Now, let us have a look at the sum of the elements of the array B and use the above observation.
SUM_B = B_1 + B_2 + ... B_N = (SUM_A + A_1) + (SUM_A + A_2) + ... (SUM_A + A_N).
Hence SUM_B = (N + 1) \times SUM_A.

So, we can find out SUM_A using SUM_B i.e. SUM_A = SUM_B / (N+1). After knowing SUM_A and using the first observation (A_i = B_i - SUM_A), we can find all the elements of the original array (A), which is the required answer.

TIME COMPLEXITY:

O(N) for each test case.

SOLUTION:

Setter's solution
#include <bits/stdc++.h>
#define ll long long int
#define pb push_back
#define mp make_pair
#define mod 1000000007
#define vl vector <ll>
#define all(c) (c).begin(),(c).end()
using namespace std;
ll power(ll a,ll b) {ll res=1;a%=mod; assert(b>=0); for(;b;b>>=1){if(b&1)res=res*a%mod;a=a*a%mod;}return res;}
ll modInverse(ll a){return power(a,mod-2);}
const int N=500023;
bool vis[N];
vector <int> adj[N];
long long readInt(long long l,long long r,char endd){
    long long x=0;
    int cnt=0;
    int fi=-1;
    bool is_neg=false;
    while(true){
        char g=getchar();
        if(g=='-'){
            assert(fi==-1);
            is_neg=true;
            continue;
        }
        if('0'<=g && g<='9'){
            x*=10;
            x+=g-'0';
            if(cnt==0){
                fi=g-'0';
            }
            cnt++;
            assert(fi!=0 || cnt==1);
            assert(fi!=0 || is_neg==false);

            assert(!(cnt>19 || ( cnt==19 && fi>1) ));
        } else if(g==endd){
            if(is_neg){
                x= -x;
            }

            if(!(l <= x && x <= r))
            {
                cerr << l << ' ' << r << ' ' << x << '\n';
                assert(1 == 0);
            }

            return x;
        } else {
            assert(false);
        }
    }
}
string readString(int l,int r,char endd){
    string ret="";
    int cnt=0;
    while(true){
        char g=getchar();
        assert(g!=-1);
        if(g==endd){
            break;
        }
        cnt++;
        ret+=g;
    }
    assert(l<=cnt && cnt<=r);
    return ret;
}
long long readIntSp(long long l,long long r){
    return readInt(l,r,' ');
}
long long readIntLn(long long l,long long r){
    return readInt(l,r,'\n');
}
string readStringLn(int l,int r){
    return readString(l,r,'\n');
}
string readStringSp(int l,int r){
    return readString(l,r,' ');
}
int sumN=0;
void solve()
{
    int n=readInt(1,100000,'\n');
    sumN+=n;
    assert(sumN<=100000);
    ll B[n+1]={0};
    for(int i=1;i<=n;i++)
    {
    	if(i==n)
    		B[i]=readInt(1,(ll)200000 * 1000000,'\n');
    	else
    		B[i]=readInt(1,(ll)200000 * 1000000,' ');
    }
    ll sum=0;
    for(int i=1;i<=n;i++)
    	sum+=B[i];
    assert((sum%(n+1))==0);
    sum/=(n+1);
    ll A[n+1]={0};
    for(int i=1;i<=n;i++)
    {
    	A[i]=B[i]-sum;
    	assert(A[i]>=1 && A[i]<=100000);
    	cout<<A[i]<<' ';
    }
    cout<<'\n';
}
int main()
{
    #ifndef ONLINE_JUDGE
    freopen("input.txt", "r", stdin);
    freopen("output.txt", "w", stdout);
    #endif
    ios_base::sync_with_stdio(false);
    cin.tie(NULL),cout.tie(NULL);
    int T=readInt(1,1000,'\n');
    while(T--)
        solve();
    assert(getchar()==-1);
    cerr << "Time : " << 1000 * ((double)clock()) / (double)CLOCKS_PER_SEC << "ms\n";
}
Editorialist's Solution
using namespace std;

int main() {
	int T;
	cin >> T;
	while(T--){
	    int n;
	    cin >> n;
	    vector<long long>b(n);
	    long long big_sum=0;
	    for(int i=0;i<n;i++){
	        cin >> b[i];
	        big_sum+=b[i];
	    }
	    long long small_sum=big_sum/(n+1);
	    for(int i=0;i<n;i++){
	        cout << b[i]-small_sum << " ";
	    }
	    cout << endl;
	}
	return 0;
}
1 Like

I was doing the same but last two test case got failed, i don’t know why please help !!!
`
#include<bits/stdc++.h>
#define nline “\n”
#define ll long long
#define pb push_back
#define umap unordered_map
#define mod 1000000007

using namespace std;

int main()
{
ios_base::sync_with_stdio(false);
cin.tie(NULL);

int t;cin>>t;

while(t--){

//start

int n;cin>>n;
vector<ll> v;
ll sum = 0;
for (int i = 0; i < n; ++i)
{
	int aa;cin>>aa;
	sum += aa;
	v.pb(aa);
}
sum/=(n+1);
for (int i = 0; i < n; ++i)
{
	cout<<v[i] - sum<<" ";
}
cout<<nline;

//end
}

return 0;

}

`

2 Likes

upper limit of B_i is 2\times10^{10}.

1 Like

So, how to tackle it?

I am doing the same thing . Why am i getting WA for last 2 test case(i have used long long )
Link to code

Could someone let me know, why this code failed?
#include <stdio.h>

int main(void) {
// your code goes here
int t;
scanf("%d", &t);
while(t–)
{
int N;
scanf("%d", &N);
int B[N];
int sum_B=0;
for(int i=0; i< N; i++)
{
scanf("%d",&B[i]);
sum_B+=B[i];
}
int sum_A;
sum_A = sum_B/ (N+1);
for(int i=0; i< N; i++)
{
printf("%d “,B[i]-sum_A);
}
printf(”\n");
}
return 0;
}

Make
int aa ;
as ll aa

ll aa

thank u very much
I was taking b[i] as an int data type which caused me WA

Your code is giving AC .

if array B is given as B={2000000000,2000000000,2000000000}
we are getting A as A={500000000,500000000,500000000}
but range of A is [1,100000]… how is this accepted?

I ran your code in codechef practice and its showing accepted… don’t know why u got WA