Answer to Question #331313 in C for shiva

Question #331313

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}


1
Expert's answer
2022-04-24T07:03:28-0400



#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;
}

Need a fast expert's response?

Submit order

and get a quick answer at the best price

for any assignment or question with DETAILED EXPLANATIONS!

Comments

No comments. Be the first!

Leave a comment

LATEST TUTORIALS
APPROVED BY CLIENTS