Computer Science, asked by nupur8947, 9 months ago

A set of points over a straight line is defined as correlative to some K if the absolute difference between any two points ia a multiple of K

Answers

Answered by krishnaofficial15
46

Answer:

def solve(p,k):

   n = len(p)

   hm = dict()

   for t in p:

       if t%k in hm:hm[t%k] += 1

       else:hm[t%k] = 1

   rem = res = 0

   for key in hm:

       res = max(res,hm[key])

       if res == hm[key]:rem = key

   print(res)  # printing set size

   # printing set elements in ascending order

   for t in p:

       if t%k == rem:print(t)

   return

n,k = list(map(int,(input().split())))

p = [-1 for i in range(n)]

for q in range(n):

   p[q] = int(input())

p.sort()

solve(p,k)

Explanation:

All the points in the set must have same remainder when divided with k.

Answered by qwbravo
0

Program implementation:

#include <stdio.h>

#include <stdio.h>#include<stdlib.h>

#include <stdio.h>#include<stdlib.h>#include<malloc.h>

int solve(int N, int K, int *arr){

int cnt=0; // counter variable

int res[100000]={0}; // declares an array and initialises all the elements to 0.

// first for loop runs from 0 to N-1;

for(int i=0; I <N-1;i++){

// second for loop runs from next of ith position to N.

for(int j=i+1;j<N;j++){

// if abs() function finds the absolute difference of integer value passed.

if(abs(arr[i]-arr[j])%K==0 && abs(arr[i]-arr[j])!=K)

res[abs(arr[i]-arr[j])]=1;// marks the occurrence of the value returned from the abs() method.

}

}

// for loop runs to count no-zero value from the resultant array.

for(int i=0; I <1000;i++){

if(res[i]!=0) // if the element of the array is not 0.

cnt++; // increments the counter.

}

return cnt; // returns the counter.

}

int main()

{

int T;

scanf("%d",&T);

for(int t_i=0;t_i<T;t_i++){

int N;

scanf("%d",&N);

int K;

scanf("%d",&K);

int i_arr;

int *arr=(int*)malloc(sizeof(int)*N);

for(i_arr=0;i_arr<N;i_arr++)

scanf("%d",&arr[i_arr]);

int out_=solve(N, K, arr);

print("%d", out_);

print("\n");

}

return 0;

}

A set of points over a straight line is defined as correlative to some K

if the absolute difference between any two points in a multiple of K

The program is compiled

#SPJ3

Similar questions