vendredi 6 avril 2018

I am getting SIGSEGV for this code.Please provide any possibility.

In this code I have used a set to store the elements.A nested 'for' is used to get all the possible pairs to check if their average is an element of set.Finally number of such unordered pairs are printed.

#include<iostream>
#include<set>
#include<cstring>
using namespace std;
int arr[10000000];//to store number of occurrences of each element.
int main(){
    int T;//Number of testcases
    cin>>T;
    while(T--){
        memset(arr,0,10000000);
        int N,count=0;
        cin>>N;
        set<int> t;//to store the elements
        for(int i=1;i<=N;i++){
            int x;
            cin>>x;
            arr[x]++;
            t.insert(x);    
        }
        for(auto i=t.begin();i!=t.end();i++){
            if(arr[*i]>1){
                count+=1*(arr[*i]*(arr[*i]-1)/2);
            }
            auto it=i;
            for(auto j=++it;j!=t.end();j++){
                if((*i)%2==0&&(*j)%2==0||(*i)%2!=0&&(*j)%2!=0){
                    int el=(*i+*j)/2;
                    if(t.find(el)!=t.end()){
                        count+=1*arr[*i]*arr[*j];
                    }
                }
            }
        }
        cout<<count<<endl;
        count=0;
    }
}

Aucun commentaire:

Enregistrer un commentaire