I was solving a problem about counting pairs for given sum in an array. I set up an array with length equal to given sum, and using it to count the occurrence of given array elements which are less than the sum. Now in the line 18 of my code even though I have all the conditions true, the loop does not executes. Please help!
I have tried compiling it on ideone and geeksforgeeks ide but same thing is happening.
#include <iostream>
using namespace std;
int main() {
int t, n, x, i, p;
cin >> t;
while(t-->0){
cin >> n >> x;
int a[x] = {0};
while(n-->0){
cin >> i;
if(i>x-1) continue;
a[i]++;
}
p=0;
cout << x/2 << endl;
cout << a[3] << ' ' << a[x-3] << endl;
for(i=1; i<=x/2 && (a[i]>0 && a[x-i]>0); ++i){
n = a[x-i];
cout << a[i] << ' ' << n << endl;
if(x==(i<<1)) p += ((n*(n-1))>>1);
else p += a[i]*n;
}
for(i=0; i<x; ++i){
cout << a[i] << ' ';
}
cout << endl;
cout << p << endl;
}
return 0;
}
The actual output must be some positive number but I am getting 0 instead, due to inactivity of for loop.
Aucun commentaire:
Enregistrer un commentaire