Monday 1 November 2021

Divisible Sum Pairs || HackerRank C Programming Solution

Given an array of integers and a positive integer , determine the number of  pairs where  and  +  is divisible by .

Example


Three pairs meet the criteria:  and .

Function Description

Complete the divisibleSumPairs function in the editor below.

divisibleSumPairs has the following parameter(s):

  • int n: the length of array 
  • int ar[n]: an array of integers
  • int k: the integer divisor

Returns
int: the number of pairs

Input Format

The first line contains  space-separated integers,  and .
The second line contains  space-separated integers, each a value of .

Constraints

Sample Input

STDIN           Function
-----           --------
6 3             n = 6, k = 3
1 3 2 6 1 2     ar = [1, 3, 2, 6, 1, 2]

Sample Output

 5

Solution:

#include<stdio.h>
int main()
{
    int n,i,j,k,m,c=0;
    scanf("%d",&n);
    scanf("%d",&k);
    int a[n];
    for(i=0;i<n;i++)
    scanf("%d",&a[i]);
    for(i=0;i<n;i++)
    {
        m=a[i];
        for(j=i+1;j<n;j++)
        {
            m+=a[j];
            if(m%k==0)
            c++;
            m=a[i];
        }
    }
    printf("%d",c);
    return 0;
}

No comments:

Post a Comment

If you have any doubts, Please let me know....

Latest

Question 3: Wire {HDLBits-Verilog-Solutions}

  Welcome to  HDLBits -Verilog-Solutions ! --------------------------------------------------------------------------- Question 3:   Create ...

Popular Posts