Does array represent Heap
PROBLEM :
Given an array, the task is to check if the given array represents a Binary Max-Heap.
Examples:
Input: A[] = {90, 15, 10, 7, 12, 2}
Output: 1
The given array represents below tree
90
/ \
15 10
/ \ /
7 12 2
The tree follows max-heap property as every
node is greater than all of its descendants.
Input: A[] = {9, 15, 10, 7, 12, 11}
Output: 0
The given array represents below tree
9
/ \
15 10
/ \ /
7 12 11
The tree doesn't follows max-heap property 9 is
smaller than 15 and 10, and 10 is smaller than 11.
Input:
The first line of input contains an integer T denoting the no of test cases. Then T test cases follow. Each test case contains two lines. The first line of input contains an integer N denoting the size of the array. Then in the next line are N space separated values of the array.
Output:
For each test case in a new line print 1 if the array could represent the max heap else print a 0.
Constraints:
1<=T<=100
1<=N<=1000
1<=A[]<=1000
Example:
Input:
2
6
90 15 10 7 12 2
6
9 15 10 7 12 11
Output:
1
0
--------------------------------------------------------------------------------
SIMPLE c++ IMPLEMENTATION :
--------------------------------------------------------------------------------
#include<iostream>
using namespace std;
bool IsMaxHeap(int [],int ) ;
int main()
{
int t ;
cin>>t ;
while(t--)
{
int no ;
cin>>no ;
int arr[no] ;
for(int i=0;i<no;i++)
cin>>arr[i] ;
cout<<IsMaxHeap(arr,no)<<endl ;
}
return 0;
}
bool IsMaxHeap(int arr[],int no)
{
for(int i=0;i<no;i++)
{
if(2*i+1<no&&arr[i]<arr[2*i+1])
return false ;
if(2*i+2<no&&arr[i]<arr[2*i+2])
return false ;
}
return true ;
}
--------------------------------------------------------------------------------
Given an array, the task is to check if the given array represents a Binary Max-Heap.
Examples:
Input: A[] = {90, 15, 10, 7, 12, 2}
Output: 1
The given array represents below tree
90
/ \
15 10
/ \ /
7 12 2
The tree follows max-heap property as every
node is greater than all of its descendants.
Input: A[] = {9, 15, 10, 7, 12, 11}
Output: 0
The given array represents below tree
9
/ \
15 10
/ \ /
7 12 11
The tree doesn't follows max-heap property 9 is
smaller than 15 and 10, and 10 is smaller than 11.
Input:
The first line of input contains an integer T denoting the no of test cases. Then T test cases follow. Each test case contains two lines. The first line of input contains an integer N denoting the size of the array. Then in the next line are N space separated values of the array.
Output:
For each test case in a new line print 1 if the array could represent the max heap else print a 0.
Constraints:
1<=T<=100
1<=N<=1000
1<=A[]<=1000
Example:
Input:
2
6
90 15 10 7 12 2
6
9 15 10 7 12 11
Output:
1
0
--------------------------------------------------------------------------------
SIMPLE c++ IMPLEMENTATION :
--------------------------------------------------------------------------------
#include<iostream>
using namespace std;
bool IsMaxHeap(int [],int ) ;
int main()
{
int t ;
cin>>t ;
while(t--)
{
int no ;
cin>>no ;
int arr[no] ;
for(int i=0;i<no;i++)
cin>>arr[i] ;
cout<<IsMaxHeap(arr,no)<<endl ;
}
return 0;
}
bool IsMaxHeap(int arr[],int no)
{
for(int i=0;i<no;i++)
{
if(2*i+1<no&&arr[i]<arr[2*i+1])
return false ;
if(2*i+2<no&&arr[i]<arr[2*i+2])
return false ;
}
return true ;
}
--------------------------------------------------------------------------------
Comments
Post a Comment