yeasir007

Wednesday, February 14, 2018

Divisible Sum Pairs












































Solution :



#include <iostream>
#include <stdio.h>
#define MAX_ELEMENT 101
using namespace std;

int input[MAX_ELEMENT];
int dataArray[MAX_ELEMENT];
int visited[MAX_ELEMENT];
int output[10000];

int N, K, COUNT;

void permutation(int level, int taken, int totalElement);
void printPermutation(int taken);

int main()
{
    N = K = COUNT = 0;
    //freopen("HackerRankDivisibleSumParis.txt","r",stdin);

    cin >> N >> K;
    for (int i = 0; i < N; i++)
    {
        cin >> dataArray[i];
        input[i] = i;
    }

    permutation(0, 2, N);

    cout << COUNT << endl;
    return 0;
}

void permutation(int level, int taken, int totalElement)
{
    if (level == taken)
    {
        if (output[1] > output[0])
            printPermutation(taken);
        return;
    }

    for (int i = 0; i < totalElement; i++)
    {
        if (!visited[i])
        {
            visited[i] = true;
            output[level] = input[i];
            permutation(level + 1, taken, totalElement);
            visited[i] = false;
        }
    }
}   

void printPermutation(int taken)
{
    /*for (int i = 0; i < taken; i++)
    {
        cout << output[i] << " ";
    }
    cout << endl;*/

    if ((dataArray[output[0]] + dataArray[output[1]]) % K == 0)
        COUNT++;
}







No comments:

Post a Comment

Thanks for your comments