Build Max Heap Problem Statement

Given an integer array with N elements, the task is to transform this array into a max binary heap structure.

Explanation:

A max-heap is a complete binary tree where each internal node has a value greater than or equal to its children. The given integer array needs to be rearranged to satisfy the max-heap property.

Input:
The first line of input contains an integer T, representing the number of test cases. Each test case consists of two lines: the first line contains the integer N (size of the array), and the second line contains N space-separated integers that represent the elements of the array.
Output:
For each test case, output '1' if the transformed array represents a max-heap, otherwise output '0'. Each test case result should be printed on a new line.

Example:

Input:
1
5
3 1 5 2 4
Output:
1

Constraints:

  • 1 <= T <= 10
  • 1 <= N <= 10^4
  • -10^9 <= data <= 10^9
Note:
You do not need to print the heap; simply ensure that the returned array satisfies the max-heap condition using 0-based indexing. In a 0-based index array: 
- The left child of the ith node is at (2 * i + 1)th index.
- The right child of the ith node is at (2 * i + 2)th index.
- The parent of the node at the ith index is at (i - 1) / 2 index.
Be the first one to answer
Add answer anonymously...
Jaguar Land Rover Technical Trainee Interview Questions
Stay ahead in your career. Get AmbitionBox app
qr-code
Helping over 1 Crore job seekers every month in choosing their right fit company
65 L+

Reviews

4 L+

Interviews

4 Cr+

Salaries

1 Cr+

Users/Month

Contribute to help millions

Made with ❤️ in India. Trademarks belong to their respective owners. All rights reserved © 2024 Info Edge (India) Ltd.

Follow us
  • Youtube
  • Instagram
  • LinkedIn
  • Facebook
  • Twitter