PROSUM - Editorial

Its my pleasure !! :wink:

getchar_unlocked() may be having some problem, if we read the large input. I am not sure of this !

still getting WA with this :-

int main()
{
int t;
cin>>t;
while(t–)
{
int n;
cin>>n;
VI arr;
REP(i,n)
{
int a;
cin>>a;
arr.pb(a);
}
int twoes=0,others=0;
REP(i,n)
{
if(arr[i] == 2)
twoes++;
if(arr[i] > 2)
others++;
}
ll ans = twoesothers1LL;
ans += (1LLothers(others-1))/2;
cout<<ans<<endl;
}
}

Can you provide a code without defines, or the full code with defines you used? I am not able to get “ll ans = twoesothers1LL; ans += (1LLothers(others-1))/2” Is multiplication going on here? You should store twoes and others as long long int, as overflow will occur. (Try changing data type of twoes and others to long long int)

https://www.codechef.com/viewsolution/13198472

https://www.codechef.com/viewsolution/33104731

please someone help me with this.
why this code is wrong?

Assume A[i]==2 , then if you put this in the given condition , then we will have A[ j]>2.
Therefore , we count the number of Two’s and numbers greater than 2. For every ‘2’ in the array, we need an element greater than 2.

Some one help me find where am I failing …

#include <bits/stdc++.h>

#define max3(a,b,c) max(a,max(b,c))
#define min3(a,b,c) min(a,min(b,c))
#define sp ' '
#define nl '\n'
#define ll long long
#define ull unsigned long long
#define all(v) v.begin(),v.end()
#define vi vector<int>
#define range(a , min , max) a >= min and a <= max

using namespace std;

void solve() {
    ull n;
    cin >> n;
    
    vector<int> v(n);
    for(auto &it : v)
        cin >> it;
        
    ull cnt = 0;
    ull ans = (n * (n - 1)) / 2;
    
    for(int i = 1; i <= n; i ++) {
        if (v[i - 1] == 0 || v[i - 1] == 1) {
            ans -= (n - i);
        }
        
        if (v[i - 1] == 2)
            cnt ++;
    }
    
    if (cnt > 0)
        ans -= (cnt * (cnt - 1)) / 2;
    
    cout << ans << nl;
}

int main() {
    int tc;
    cin >> tc;
    while(tc--) {
        solve();
	}

    return 0;
}