DSA

Data structure & algorithm using swift language.

Minimum Number of Days to Make m Bouquets

[Question] In an integer array bloomDay, an integer m and an integer k.You want to make m bouquets. To make a bouquet, you need to use k adjacent flowers from the garden. The garden consists of n flowers, the ith flower will bloom in the bloomDay[i] and then can be used in exactly one bouquet. Return the minimum number of days you need to wait to be able to make m bouquets from the garden.

Minimum Number of Days to Make m Bouquets Read More »

Koko eating Banana || Return min value using Binary search

[Question]: There are n piles of bananas, the ith pile has piles[i] bananas. The guards have gone and will come back in h hours.Koko can decide her bananas-per-hour eating speed of k. Each hour, she chooses some pile of bananas and eats k bananas from that pile. If the pile has less than k bananas, she eats all of them instead and will not eat any more bananas

Koko eating Banana || Return min value using Binary search Read More »

Find out how many times the array has been rotated || Find Lowest Element Index

[Question] Given an integer array arr of size N, sorted in ascending order (with distinct values). Now the array is rotated between 1 to N times which is unknown. Find how many times the array has been rotated.  Example 1: Input Format: arr = [4,5,6,7,0,1,2,3] Result: 4 Explanation: The original array should be [0,1,2,3,4,5,6,7]. So, we can notice

Find out how many times the array has been rotated || Find Lowest Element Index Read More »

Find Peak Element in an Array

[Question]: Given a 0-indexed integer array nums, find a peak element, and return its index. If the array contains multiple peaks, return the index to any of the peaks. Example 1: Input: nums = [1,2,3,1] Output: 2 Explanation: 3 is a peak element and your function should return the index number 2. Example 2: Input: nums = [1,2,1,3,5,6,4] Output:

Find Peak Element in an Array Read More »

Find Minimum in Rotated Sorted Array

[Question]: Suppose an array of length n sorted in ascending order is rotated between 1 and n times. For example, the array nums = [0,1,2,4,5,6,7] might become: [4,5,6,7,0,1,2] if it was rotated 4 times. [0,1,2,4,5,6,7] if it was rotated 7 times. Notice that rotating an array [a[0], a[1], a[2], …, a[n-1]] 1 time results in the array [a[n-1], a[0], a[1], a[2], …, a[n-2]].Given the sorted rotated array nums of unique elements, return the minimum element of this array.You must write an

Find Minimum in Rotated Sorted Array Read More »

Search in Rotated Sorted Array

[Question] In a given array which is sorted in ascending order (with distinct values). return the index of target if it is in nums, or -1 if it is not in nums. Example 1: Input: nums = [4,5,6,7,0,1,2], target = 0 Output: 4 Example 2: Input: nums = [4,5,6,7,0,1,2], target = 3 Output: -1 #Approach: Using Binary search We can find element using binary search, we

Search in Rotated Sorted Array Read More »