0

question

given an array of elements 0, 1, 2 with find the total number of subarrays with the ratio of 0's and 1's equal to x:y.

input


5


1 1


0 1 2 0 1


output 6


\\5 is the size of array 0 1 2 0 1 are elements of the array 1 1 is x and y and now we have to find the subarrays whose counts of 0's and 1's ratio is equal to x and y that is 1 1 \\


here is my approach but it is not giving correct and it gives output 7 rather than 6


#include<bits/stdc++.h>

using namespace std;

int n, x, y;
vector<int> a;
vector<long long> prefix;
map<long long, int> freq;

int main() {
    cin >> n;
    cin >> x >> y;

    a.resize(n);
    for (int i = 0; i < n; i++) {
        cin >> a[i];
        if (a[i]==0) a[i] = y;
        else if( a[i]==0){
            a[i]=0;
        }
        else a[i] = -x;
    }

    prefix.resize(n+1); prefix[0] = 0;
    for (int i = 0; i < n; i++) {
        prefix[i+1] = prefix[i] + a[i];
    }

    for (int i = 0; i < n+1; i++) freq[prefix[i]]++;
    long long ans = 0;
    for (pair<long long, int> p : freq) {
        ans += (long long) p.second * (p.second-1) / 2;
    }
    cout << ans << endl;
}
Abhinav
  • 7
  • 4
  • Please read [ask]. – Richard Critten Sep 10 '22 at 09:19
  • please provide the solution also with explanation please, – Abhinav Sep 10 '22 at 09:21
  • SO is not a homework / write an answer service. You need to show some effort and post your best attempt then we might be able to help. – Richard Critten Sep 10 '22 at 09:22
  • ok sir i have added my own wrong code now sir help me. – Abhinav Sep 10 '22 at 09:30
  • what is `x:y` ? which part of the input is `x` and `y` ? Why is the correct answer for the example 6 ? – 463035818_is_not_an_ai Sep 10 '22 at 09:41
  • 5 is size of array 0 1 2 0 1 are elements of the array 1 1 is x and y and now we have to find the subarrays who's counts of 0's and 1's ratio is equal to x and y that is 1 1; – Abhinav Sep 10 '22 at 10:29
  • 1
    Whichever C++ textbook taught you to use `` -- you need to throw it away and get a different C++ textbook. If you copied that off some web site, without any explanation, don't visit that web site any more. If you saw this in some clown's Youtube video, unsubscribe from that channel, you're not learning proper C++. Most C++ compilers in the world don't have this header file, and will not compile the shown code. – Sam Varshavchik Sep 10 '22 at 11:29
  • bits/stdc++.h is very common in my peer group it includes all header files that we want like vector queue string algorithms like these are automatically included in it thatswhy sir. – Abhinav Sep 14 '22 at 04:49

1 Answers1

0

First, the order you read your inputs is different than what you describe, then your transform of the input values make no sense.

//... 

cin >> n;
cin >> x >> y;  // program expects x then y, then contents of array.

a.resize(n);
for (int i = 0; i < n; i++) {
    cin >> a[i];
    if (a[i]==0) a[i] = y;
    else if( a[i]==0){       ∕/ case a[i] == 0 was already tested for
                             // on line above. 
        a[i]=0;
    }
    else a[i] = -x;
}

//...  

You should sort this out first.

Michaël Roy
  • 6,338
  • 1
  • 15
  • 19