Question

C++ Given a sequence of integers, check to see if the sequence constitutes a heap Input:...

C++

Given a sequence of integers, check to see if the sequence constitutes a heap

Input: the first line is an integer n(0 < n < 10000), The second line is n integers

Output: If the sequence is a very small heap, that is, the top of the heap is the smallest element, then output "min heap". If the sequence is a very large heap, the top of the heap is the largest element

The output includes one line with a newline at the end

Example:

## Input

5

3 4 2 1 1

##Output:

no

0 0
Add a comment Improve this question Transcribed image text
Answer #1

Output

#1

#2

#3

Code with comments

#include<bits/stdc++.h>

using namespace std;

void checkMinHeap(vector<int>&v){   //check for min heap

    int n=v.size();

    for(int i=0;i<n;i++){

        int left = 2*i+1;   //left child

        int right = 2*i + 2;    //right child

        if(left<n && v[i]>v[left]){ //check root i greater then left

            cout<<"No";

            return;

        }

        if(right<n && v[i]>v[right]){  //check root i greater then right

            cout<<"No";

            return;

        }

    }

    cout<<"Min heap";

}

void checkMaxHeap(vector<int>&v){  //check for max heap

    int n=v.size();

    for(int i=0;i<n;i++){

        int left = 2*i+1;

        int right = 2*i + 2;

        if(left<n && v[i]<v[left]){  //check root i less then left

            cout<<"No";

            return;

        }

        if(right<n && v[i]<v[right]){  //check root i less then right

            cout<<"No";

            return;

        }

    }

    cout<<"Max heap";

}

int main(){

    int n;

    cin>>n;

    vector<int>v(n);

    for(int i=0;i<n;i++){

        cin>>v[i];

    }

    if(v[0]>v[1]) checkMaxHeap(v);  //check which function to call

    else checkMinHeap(v);

}

For indentation purpose

Add a comment
Know the answer?
Add Answer to:
C++ Given a sequence of integers, check to see if the sequence constitutes a heap Input:...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • Below is a linear time complexity algorithm Max-Min-VER1 to find the biggest and smallest element a...

    Below is a linear time complexity algorithm Max-Min-VER1 to find the biggest and smallest element a given list. Input: A is a given list Output: two integers Example: Given A = {1, 5, 9, -3}. It returns -3 (the smallest element), and 9 (the biggest element) Max-Min-VER1(A, n) Line 1: large ← A[0] Line 2: for I from 1 to n - 1 do Line 3:     large ← Math.max(large, A[I]) Line 4: small ← A[0] Line 5: for I from...

  • Given an array of integers representing heights of consecutive steps, we say that a tumble is...

    Given an array of integers representing heights of consecutive steps, we say that a tumble is a sequence of strictly decreasing numbers and the height of the tumble is the difference between the height of the top step and the bottom step. For example, if there are consecutive steps with heights 16, 9, 4 then they form a tumble of height 12 (= 16-4). With consecutive steps of heights, 0, 4, 12, 4, 5, 7, 2, 1 there is a...

  • Design and implement a Θ(n) algorithm that will simultaneously find the largest and second-largest elements (integers)...

    Design and implement a Θ(n) algorithm that will simultaneously find the largest and second-largest elements (integers) in an array. Note that the second largest element should be distinctly smaller than the largest element. You should also adhere to the following restrictions. (1) You should traverse through the array ONLY ONCE. (2) The array could have both positive and negative elements. So, you should NOT initialize any temporary variables to very small negative values as part of your algorithm. (3) You...

  • Plz i want answer these question using list in python programme. You are given two sequences...

    Plz i want answer these question using list in python programme. You are given two sequences of n integers: 21, 22, ...,an and b1,b2, ..., bn Print a sequence of 2n integers created by alternating elements from the given sequences: a1, 61, 42, 62, a3, 63, ..., an, bn. Input The first line contains a positive integer n (1 <n<1000) - the length of the sequences The second line contains n space-separated integers 01, 02, ..., an (-1000 <a; <...

  • C++ problem Please help me with it. Thank you. the output would be like this: 10-20...

    C++ problem Please help me with it. Thank you. the output would be like this: 10-20 with sequence 10 - 30 no sequence: this is the background: Project Description / Specification Your program takes as input three integer values (same line, space separated) the first two integers, the beginning and ending value in a range of integers you are going to examine, as space separated integers. They are in that order: the first is the lower range and the second...

  • PROGRAM DESCRIPTION Using the given class definitions for either C++, create a minimum heap that stores...

    PROGRAM DESCRIPTION Using the given class definitions for either C++, create a minimum heap that stores integers and and implements a minimum priority queue. (Your program can be "hard coded" for integers - it does not need to use templates, generics, or polymorphism.) Your data structure must always store its internal data as a heap. Your toString function should return a string with the heap values as a comma separated list, also including the size of the heap as well....

  • in C++ HeapSort Implement the heapsort algorithm for sorting an array of integers. Input structure Each...

    in C++ HeapSort Implement the heapsort algorithm for sorting an array of integers. Input structure Each case starts with an integer number which indicates the number of elements to be sorted. Then, the elements follow, one per line. You can assume the input is correctly structured (i.e., no data are missing Output structure Output the sorted sequence separeted by";" (in non-decreasing order). Do not insert spaces or a new line at the beginning or at the end of any element.

  • In Python 3 - Write a program that reads a sequence of integer inputs from the...

    In Python 3 - Write a program that reads a sequence of integer inputs from the user. When the user is finished entering the integers, they will enter a 'q'. There is no need to check if the entry is a valid integer. All integers in the test data (input) will be between -255 and 255. The program should then print: The smallest and largest of the inputs. The number of even and odd inputs (0 should be considered even)...

  • C++ Write a program that asks user to input three positive integers one at a time,...

    C++ Write a program that asks user to input three positive integers one at a time, then compute and output the largest entered number. Use if-else statements for three integer comparison and use for loops for a user validation. Example output: Enter an integer: -7 Invalid! Number must be positive. Enter an integer: -2 Invalid! Number must be positive. Enter an integer: 5 Enter an integer: 7 Enter an integer: 1 Largest number: 7

  • Sequence (Longest Progressive Sequence) in C language Given a sequence, the objective is to find the...

    Sequence (Longest Progressive Sequence) in C language Given a sequence, the objective is to find the longest progressive sequence arranged in ascending order. Detailed descriptions are as: Problem A sequence is said to be progressive if it doesn’t decrease at any point in time. For example 1 1 2 2 is a progressive sequence but 1 2 1 is not a progressive sequence. Let S be the sequence and be represented by L spaced integers Ki, now your task is...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT