0% found this document useful (0 votes)
31 views3 pages

Without Using Space

This document discusses algorithms for merging two sorted arrays in-place with O(n+m) time complexity and O(1) auxiliary space complexity. It describes using two pointers to compare elements from each array and merge them into the output array, moving the smaller element and incrementing its pointer, until all elements are merged.

Uploaded by

Prishita Kapoor
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
31 views3 pages

Without Using Space

This document discusses algorithms for merging two sorted arrays in-place with O(n+m) time complexity and O(1) auxiliary space complexity. It describes using two pointers to compare elements from each array and merge them into the output array, moving the smaller element and incrementing its pointer, until all elements are merged.

Uploaded by

Prishita Kapoor
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 3

SC: O(1) – as inplace sort algo is used

If a sorting algorithm with an auxiliary space is used, such as Mergesort or Timsort, the space
complexity would be O(n+m)
2. Use two pointers to compare and merge elements:
Start with the first element of each array and compare them. Move the smaller element to the merged
array and increment the pointer for that array. Repeat this process until all elements are merged. If any
elements are remaining in either array, append them to the merged array.
TC: O(n+m) SC: O(n+m).
WITHOUT USING SPACE

Write a program to cyclically rotate an array by one


Find if there is any subarray with sum equal to 0
Find longest consecutive subsequence
Given an array of size n and a number k, fin all elements that appear more than ” n/k ” times
– Majority Element > n/2 times n/3 times
Best time to buy and Sell stock
Inversion count (how far from being sorted)
1.Traverse through the array, and for every index, find the number of smaller elements on its right side
of the array. This can be done using a nested loop. Sum up the counts for all indices in the array and
print the sum.
TC: O(N^2)
SC: O(1)
2.Merge Sort
Time Complexity: O(N * log N)
Auxiliary Space: O(N), Temporary array.

2-sum
3-sum
4-sum
Chocolate Distribution Problem

Median of 2 sorted arrays


equal size
different size
7. Rearrange the array in alternating positive and negative items / Rearrange Array Elements by
Sign - maintaining the order of appearance. And equal number of positive and negative in input array
1.Using two Extra Arrays for positive and negative numbers (n/2) respectively.And Merge back into
the original array. TC: O(n)
SC: O(n)
2.
Find missing number in array
Maximum consecutive ones
12. Minimum no. of Jumps to reach end of an array / JUMP GAME
1.Using Recursion
minJumps(start, end) = Min ( minJumps(k, end) ) for all k reachable from start.
2.Using Dynamic Programming from left to right
TC: O(N^2) SC: O(N)
3. Using Dynamic Programming from right to left
4. Using greedy
TC: O(N) SC: O(1)
5. Using top down approach (Memoization)
6. Initialize the three variable
7. divides the array into sub-parts and find out the maximum
13. Find duplicate in an array of N+1 Integers / Find the two repeating elements in a given array
/ remove duplicate from array
Sorted array
1. using two nested loops
2. Sorting and check for adjacent neighbours
3. Using extra space - HashSet, HashTable, or ArrayList
Unsorted array
1.two loops
Time Complexity: O(N*K).
Auxiliary Space: O(1).
2.Sort the Array.
Time complexity: O(nlog(n))
Space complexity: O(log(n))
3.using hashing: hashmap or hashset
TC: O(N).
SC: O(N)

You might also like