John is going through his family photo album. There is a total of people in the family labeled from 0 to N-1.
Find out the number of members of his family who have more than equal to X children.
Input Specification:
input1: N. denoting the total number of family members.
input2: X denoting the minimum number of children a family member should have in order to be included in John's final set.
input3: An array of N elements where the ith element denotes the parent of the ith index. If the value of the element is -1, then that family member has no parent.
Output Specification:
Your function should return the number of John's family members that have more than or equal to X childrens.
Example 1:
Input1: 1
input2:0
input3:{-1}
output:1
Explanation :Family member 0 has 0 children. As we have to return the family membersthat have more than or equal to 0 children.hence the answer is 1.
#include<stdio.h>
int main(){
int N ;
scanf("%d", &N);
int X ;
scanf("%d", &X);
int Parent[N] ;
int sub[N] ;
int c = 0;
int i =0;
for( i=0; i<N; ++i)
{
scanf("%d", &Parent[i]);
if(Parent[i] != -1)
++sub[Parent[i]];
}
i = 0;
for(i=0; i<N; ++i)
{
if(sub[i] >= X)
++c;
}
printf("%d", c);
}
Comments
Leave a comment