Implement Power Function 1

Problem Implement pow(x, n), which calculates x raised to the power n (i.e., x^n). Examples Example 1: Input: x = 2.00000, n = 10 Output: 1024.00000 Example 2: Input: x = 2.10000, n = 3 Output: 9.26100 ...

Find first position of target element in sorted array

Problem Given a sorted array and an element, find the first occurrence of key in array. As array can contain duplicate values, there can be multiple occurrences of same element, problem is to find first index. Examples Example 1: Input: a = [1, 4, 4, 10, 10, 15, 20], target = 10 Output: 3 ...

Maximum Subarray Sum

Problem Given an integer array nums, find the contiguous subarray (containing at least one number) which has the largest sum and return its sum. Examples Example 1: Input: nums = [-2,1,-3,4,-1,2,1,-5,4] Output: 6 Explanation: [4,-1,2,1] has the largest sum = 6. ...

Merge Sort Algorithm

Problem Array Sorting Problem Similar Sorts 3-way Merge Sort Solution Mergesort is one of the older algorithms known since 1945. It is very good example of divide and conquer paradigm and is better than other simpler sort algorithms like selection, insertion or bubble sort. Method 1 - Recursive Top Down Merge Sort Merge sort is a recursive algorithm that repeatedly calls itself on progressively smaller subsets of the problem. ...

Binary Search Algo 13 - on a linked list

Problem Given the head of a sorted singly linked list and a target value, implement a function to find the target value using a binary search approach. If the target is found in the linked list, return the node; otherwise, return None. Examples Example 1: Input: head = [1, 3, 5, 7, 9, 11], target = 7 Output: Node with value 7 Explanation: The target 7 is found in the linked list. ...

Median of Two Sorted Arrays Problem

Problem Given two sorted arrays nums1 and nums2 of size m and n respectively, return the median of the two sorted arrays. The overall run time complexity should be O(log (m+n)). Examples Example 1: Input: nums1 = [1,3], nums2 = [2] Output: 2.00000 Explanation: merged array = [1,2,3] and median is 2. ...

Binary Search on Sorted Array

Problem Given a sorted array arr[] of n elements, write a function to search a given element x in arr[]. Input: A sorted array, arrA[] and an key Output : Return index if element is found, else -1. Examples Example 1: Input: nums = [-1,0,3,5,9,12], target = 9 Output: 4 Explanation: 9 exists in nums and its index is 4 ...

Find first and last Position of Target Element in Sorted Array

Problem Given an array of integers nums sorted in non-decreasing order, find the starting and ending position of a given target value. If target is not found in the array, return [-1, -1]. You must write an algorithm with O(log n) runtime complexity. Examples Example 1: Input: nums = [5,7,7,8,8,10], target = 8 Output: [3,4] ...

Search in Rotated Sorted Array

Problem Suppose a sorted array is rotated at some pivot unknown to you beforehand. (i.e., [0, 1, 2, 4, 5, 6, 7] might become [4, 5, 6, 7, 0, 1, 2]). You are given a target value to search. If found in the array return its index, otherwise return -1. You may assume no duplicate exists in the array. Follow up: what if duplicates are allowed? ...

Best Time To Buy And Sell Stock 1 - only one transaction

Problem You are given an array prices where prices[i] is the price of a given stock on the ith day. You want to maximize your profit by choosing a single day to buy one stock and choosing a different day in the future to sell that stock. Return the maximum profit you can achieve from this transaction. If you cannot achieve any profit, return 0. OR ...

This site uses cookies to improve your experience on our website. By using and continuing to navigate this website, you accept this. Privacy Policy