Harrypotter is visiting his uncle’s house ,the house of Black ,and ants to know more about the family through their ancestral tree.He notices that the ancestraltreebegins with the head of the family at the top,having 2 children as his descendants,This pattern is followed throught and each member is represented by unique integer.
Given relationships in the form ofan integer arraywhere the head of the family is at the first position (i=0) and his children areat position (2*i+1) and (2*i+2).
Your task is to help harry find and return all the siblings of any given family member and return them in the form of a sorted array.
Note:if there are no siblings ,return {-1}.
Example:
Input1:5
Input2:{1,2,3,4,5}
Input3:1
Output:{-1}
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <ctype.h>
int main(void) {
int tree[5000]={-1};
int n;
scanf("%i",&n);
for(int i=0;i<n;i++)
scanf("%i",&tree[i]);
int k;
scanf("%i",&k);
int cn=0;
if(2*k+1<n&&2*k+2<n)
printf("Left siblings: %i Right sibl: %i \n",tree[2*k+1],tree[2*k+2]);
else
printf("-1\n");
return 0;
}
Comments
Leave a comment