Computer Science, asked by vanshika0206, 2 months ago

you are given a function, int Find Count(int arr[], int length, int num, int diff); The function accepts an integer array 'arr', its length and two integer variables 'num' and 'diff'. Implement this function to find and return the number of elements of 'arr' having absolute difference of less than or equal to 'diff' with 'num'. Note: In case there is no element in 'arr' whose absolute difference with 'num' is less than or equal to 'diff', return -1. Example Input: 'arr': 12 3 1

Answers

Answered by yashmukaty1
5

Answer:

int helper(int arr[], int length, int num, int diff)

{

int count=0;

for(int i=0;i<length;i++)

{

if(abs(arr[i]-num)<=diff)

{count++;}

}

return count>0 ? count : -1;

}

Explanation:

Answered by sahilkiningesk
3

Answer:

def findCount(n, arr, num, diff):

   count=0

   for i in range(n):

       if(abs(arr[i]-num)<=diff):

           count+=1

 

   if count:

       return count

   return 0

 

n=int(input())

arr=list(map(int,input().split()))

num=int(input())

diff=int(input())

print(findCount(n, arr, num, diff))

Explanation: Python

Similar questions