Build heap from a given Array

Heaps Theory and Implementation Medium

Given an array of integers nums, convert it in-place into a min-heap.


A binary min-heap is a complete binary tree where the key at the root is the minimum among all keys present in a binary min-heap and the same property is recursively true for all nodes in a Binary Tree.

Constraints

  • 1 <= nums.length <= 105
  • -104 <= nums[i] <= 104

Hints

  • For each node at index i. Find the left child at 2*i + 1. Find the right child at 2*i + 2.
  • "Swap with the smallest child if nums[i] > nums[child]. Recursively heapify down until the heap property is restored."

Company Tags

Etsy Electronic Arts PwC Databricks Zynga Zomato Cloudflare Philips Healthcare Morgan Stanley Ubisoft Snowflake Roche Zoho Docker Robinhood Dropbox Optum Uber PayPal Walmart HashiCorp Ernst & Young Red Hat Swiggy Boston Consulting Group Google Microsoft Amazon Meta Apple Netflix Adobe