Size of Binary Tree (Function Program)

Size of Binary Tree (Function Program)

Given an array of size n, find all elements in array that appear more than n/k times.

Input:

The first line of input contains an integer T denoting the number of test cases. Then T test cases follow. Each test case contains an integer n denoting the size of the array. Then the next line contains n space separated integers forming the array. The last line of input contains an integer k.

Output:

Print the count of elements in array that appear more than n/k times.

Constraints:

1<=T<=10^5
1<=N<=10^5
1<=a[i]<=10^5
1<=k<=N


Example:Input:

2
8
3 1 2 2 1 2 3 3
4
4
2 3 3 2
3

Output:

2
2


**For More Examples Use Expected Output**


Solution:

  We can solve this by using recursion and non-recursion both way. 
Benefit of Recursion :  Everyone understand the code and also we see the flow of the code that how its work. In this we take help of inorder traversal because here we only need to traverse all the node and count how many it is.
By using Inorder traversal we first left node than right node and in the last when we reched the last node (Null) then we return with 1.

Post a Comment

0 Comments