Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
87 changes: 87 additions & 0 deletions DesignMinHeap.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,87 @@
//
// DesignMinHeap.swift
// DSA-Practice
//
// Created by Paridhi Malviya on 1/15/26.
//

struct MinHeap<T: Comparable> {

var elements: [T] = []

var isEmpty: Bool {
return elements.isEmpty
}

var count: Int {
return elements.count
}

var min: T? {
return elements.first
}

mutating func insert(_ value: T) {
elements.append(value)
heapifyUp(from: elements.count - 1)
}

mutating func heapifyUp(from index: Int) {
var childIndex = index
var parentIndex = (childIndex - 1) / 2
while (childIndex > 0 && elements[childIndex] < elements[parentIndex]) {
elements.swapAt(childIndex, parentIndex)
childIndex = parentIndex
parentIndex = (childIndex - 1) / 2
}
}

mutating func delete(_ value: T) -> T? {
elements.swapAt(0, elements.count - 1)
let r = elements.removeLast()
heapifyDown(from: 0)
return r
}

mutating func heapifyDown(from index: Int) {
var parentIndex = index
var leftChildIndex = (2 * parentIndex) + 1

while (leftChildIndex < count) {

var childIndex = leftChildIndex

var rightChildIndex = leftChildIndex + 1

if (rightChildIndex < count && elements[rightChildIndex] < elements[leftChildIndex]) {
childIndex = rightChildIndex
}

if (elements[parentIndex] < elements[childIndex]) {
break
}

elements.swapAt(parentIndex, childIndex)
parentIndex = childIndex
leftChildIndex = (2 * parentIndex) + 1
}
}
}

class MinHeapImpl {

init() {
var minHeap = MinHeap<Int>()
minHeap.insert(10)
minHeap.insert(15)
minHeap.insert(2)
minHeap.insert(11)
minHeap.insert(7)
minHeap.insert(8)
let removed = minHeap.delete(2)
print("removed \(removed)")

let min = minHeap.min ?? -1
print("minimum *** \(min)")
}
}
52 changes: 52 additions & 0 deletions FindMissingNumberInSortedArray.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
//
// FindMissingNumber.swift
// DSA-Practice
//
// Created by Paridhi Malviya on 1/15/26.
//

/*
If you are given a sorted array, find missing elements in between a range.
[1, 2, 3, 4, 5, 6, 8, 9] ->
take elements 1 and 6
1, index = 0 => 1 - 0 = 1
6, index = 5 => 6 - 5 = 1
since the difference is same, so elements are not missing in between this range.

check if you have an element missing between two elements - e.g. [6, 8]
(6 + 8) / 2 = 7. means 7 is missing.


Apply binary search.
*/
class FindMissingNumbers {

init() {
let r = findMissingElement([1, 2, 3, 4, 5, 8, 9])
print("result is \(r)")
}

func findMissingElement(_ nums: [Int]) -> Int {

var low = 0
var high = nums.count - 1

while ((high - low) >= 2) {
let mid = low + (high - low) / 2
let differenceOfLow = nums[low] - low
let differenceOfMid = nums[mid] - mid
let highIndexDifference = nums[high] - high

if (differenceOfLow != differenceOfMid) {
high = mid
} else if (differenceOfMid != highIndexDifference) {
low = mid
}

print("difference of lwo \(differenceOfLow) **** difference of mid \(differenceOfMid)")
}
print("low *** \(low) *** high \(high)")
return (nums[low] + nums[high])/2 //resultArray
}

}
1 change: 0 additions & 1 deletion Problem1.cpp

This file was deleted.

1 change: 0 additions & 1 deletion Problem1.java

This file was deleted.

1 change: 0 additions & 1 deletion Problem2.cpp

This file was deleted.

1 change: 0 additions & 1 deletion Problem2.java

This file was deleted.