Tuesday, October 11, 2016

Divisible pairs sum

The link for the question is : 
Divisible pairs sum
Code :
#include<iostream>
using namespace std;
int main(){
    int n, k;
    cin>>n>>k;
    int a[n];
    int i, j;
    i = 0;
    for(i=0;i<n;i++){
        cin>>a[i];
    }
    int c;
    int count = 0;
    for(i=0;i<n;i++){
        for(j=i+1;j<n;j++){  // these two loops check all the elements such as i<j
            c = a[i]+a[j];
            if(c%k==0){
                count++;
            }
        }
    }
    cout<<count;
    return 0;
}

No comments:

Post a Comment

Search This Blog