Wednesday, July 5, 2023

Maximum profit from the stock sale.

Maximum profit from a single buy and sale of stock data

Merge two sorted arrays

Problem Merge One Sorted Array Into Another First array has n positive numbers, and they are sorted in the non-descending order. Second array has 2n numbers: first n are also positive and sorted in the same way but the last n are all zeroes. Merge the first array into the second and return the latter. You should get 2n positive integers sorted in the non-descending order. Example { "first": [1, 3, 5], "second": [2, 4, 6, 0, 0, 0] } Output: [1, 2, 3, 4, 5, 6]

Thursday, June 29, 2023

Mergesort algorithm in python

Mergesort algorithm is a divide and conquer algorithm. Its not efficient as Quicksort algorithm but in some cases Mergesort is better than Quicksort. For example if the data to be sorted is in a linked list then Quicksort is costly since in a linkedlist accessing random elements is costly and in Quicksort we have to access data randomly.



Asymptotic Complexity
SomethingTime ComplexitySpace Complexity
Worst Case PerformanceO (n log n)
Best Case PerformanceO (n log n )
Average Case PerformanceO(n log n)
Worst Case space Complexity
O (n)




Monday, February 28, 2022

Friday, April 30, 2021

Dutch Flag problem

Dutch flag is a notable problem wherein you have a array representing three colors . Red(R), Green(G) and Blue(B) which needs to be sorted with Reds coming first followed by Green and then Blue. This is achieved by lumoto partitioning scheme.

Tuesday, April 20, 2021

Printing out all combinations of string

Printing out all combinations of a string

Printing out all the permutations of string

How to generate permutation of a given string where digits are kept as it is, but alphabets are replaced by lower and upper case characters The above solution uses immutable strings so its not efficient time complexity-wise below solution replaces string with list

Monday, April 19, 2021

Palindrome Linked List

Given the head of a singly linked list, return true if it is a palindrome. 

 Example 1: Input: head = [1,2,2,1] 
Output: true 

Example 2: Input: head = [1,2]
 Output: false 

 Constraints: The number of nodes in the list is in the range [1, 105]. 0 <= Node.val <= 9 
 Follow up: Could you do it in O(n) time and O(1) space?

Saturday, March 27, 2021

Selection sort implementation in python

Here is a simple python implementation of selection sort. The asymptotic complexity of selection sort is n^2

Wednesday, February 19, 2020

Balanced Binary Tree from sorted array

Create a balanced binary tree from sorted array


Array character sequence replacement with another array sequence

In this program a character sequence in an an array is replaced with another.

 Example

 Input string : "mary had a little lamp"

 "little" needs to be replaced with "big"

 Output string : " "mary had a big lamp"




Sunday, March 3, 2019

Variable arguments in C

Variable arguments can be passed to a function in C. The best example for this is the inbuilt printf function. For printf the first argument is a character array and variable number of arguments after that. This is implemented using macros within the stdarg header. When parameters are passed to a function , its copied to a stack created for that function. va_list and va_arg and va_end are macros which help traverse thru this stack. This is partial implementation of your own printf function.

Sunday, April 22, 2018

Replace where ever question mark is there in a string with 0 and 1 with all the patterns created.

Example : input string "a??b?" outputs are a00b0,a00b1,a01b0, a01b1... like that all possiblities Here is the sample code for same

Monday, April 9, 2018

String replacement with characters from Map to generate all possible combinations

String replacement with characters from Map to generate all possible combinations Given a hashmap M which is a mapping of characters to arrays of replacement characters, and an input string S, return an array of all possible combinations of S (where any character in S can be substituted with one of its substitutes in M, if it exists). What is the time complexity? What is the space complexity? Can you optimize either? Example input: M = { f: [F, 4], b: [B, 8] } S = fab Expected output: [FaB, Fa8, 4aB, 4a8]

Sunday, April 8, 2018

String pattern generation based on Dictionary entries

Given a string as input, return the list of all the possible patterns: ''' { "1" : ['A', 'B', 'C'], "2" : ['D', 'E'], "12" : ['X'], "3" : ['P', 'Q'] } ''' Example if input is "123", then output is ["ADP","ADQ","AEP","AEQ","BDP","BDQ","BEP","BEQ","CDP","CDQ","CEP","CEQ","XP","XQ"]

Saturday, April 7, 2018

Merging overlapping lists

You are given two lists of intervals, A and B.
In A, the intervals are sorted by their starting points. None of the intervals within A overlap.
Likewise, in B, the intervals are sorted by their starting points. None of the intervals within B overlap.
Return the intervals that overlap between the two lists.
Example:
A: {[0,4], [7,12]}
B: {[1,3], [5,8], [9,11]}
Return:
{[1,3], [7,8], [9,11]}
How would you do this?

Here is a solution