link24
BAN USER
Comments (3)
Reputation 0
Page:
1
Comment hidden because of low score. Click to expand.
Comment hidden because of low score. Click to expand.
Comment hidden because of low score. Click to expand.
0
of 0 vote
I think following should work
#include<iostream>
using namespace std;
int arr[]={2,6,3,6,3,6,-1};
int main(){
int size=7;
int cov[size];
for(int i=0;i<size;i++){
cov[i]=0;
cout<<arr[i]<<" ";
}
int l=0;
for(int i=0;i<size;i++){
if(arr[i]==-1)continue;
else {
cov[arr[i]]=max(cov[arr[i]],cov[i]+1);
l=max(l,cov[arr[i]]);
}
}
cout<<"\nlength = "<<l+1;
}
Page:
1
CareerCup is the world's biggest and best source for software engineering interview preparation. See all our resources.
@G25
- link24 September 09, 2013for every node, cov would store depth of the of that node, the test case which you are referring is below
5
/ / \ \ \
0 1 2 3 4
for which cov array at the end is (after executing this same code)
cov = {0,0,0,0,0,1}
so answer in end is 2 (the height, 1 more than maximum value in array)
kindly check it on your machine, and let me know if still you find some bug in it.