Responsive Ads Here

Tuesday, 23 May 2017

Does array represent Heap(geeksforgeeks)

problem id:-http://practice.geeksforgeeks.org/problems/does-array-represent-heap/0


code:--

#include<iostream>
using namespace std;
int main()
{
int t;
cin>>t;
while(t--)
    {
    int n;
    cin>>n;
    int a[n];
    for(int i=0;i<n;i++)
    cin>>a[i];
    int flag=1;
    for(int i=1;i<n;++i)
        {
        if(i+1<n && a[i+1]>a[i/2] || a[i]>a[(i-1)/2] )
            {
            flag=0;
            break;
            }
        }
    cout<<flag<<'\n';
    }

return 0;
}

No comments:

Post a Comment