Competitive Resources WEEK @2

You might also like

Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 3

INTRODUCTION TO

COMPETITIVE PROGRAMING
WEEK @2

➔ Two pointers

◆ Problem 1(Easy): Squares of sorted Array


Solution
◆ Problem 2(Medium): Longest Mountain in a Array
● Solution Approach : Find out if a point is a peak and maximise around
that point using 2 pointers.
● Do this for all possible peaks and calculate maximum.
● Complexity Analysis : O(n) time . O(1) space.
◆ Problem 3(Easy): Two Sum
◆ Problem 4(Medium): Three Sum
● Do it only after attempting ‘two sum’ problem
● See hint1, hint2, hint3 in leetcode if unable to solve.
◆ Problem 5,6(Medium, interesting): Container with most water, Fruits into
Basket
● Solution for both problems 5,6

➔ Sorting

◆ Make use of inbuilt functions for competitive programming.


● STL C++ Sort read
● Time complexity better than O(nlg(n)) is not achievable in comparison
based sorting algorithms. (Proof not required right now)
● Problem1 (Very easy): Priyank and toys
● Problem2 (Easy): Twins
● Problem 3(Medium) : K closest points to Origin
○ Solution
● Problem 4(Medium): Meeting Rooms II
○ See 4 hints one by one, given on website if unable to solve

◆ [[Optional]] Read about all these sorting algos from gfg to have a basic
deeper understanding of sorting.
Sorting Time Time Time Space Stable Comments
Algo Best Worst Average
case case case
Bubble O(n^2) O(n^2) O(n^2) O(1) Yes For each pair of indices, swap the
sort elements if they are out of order.

Modified O(n) O(n^2) O(n^2) O(1) Yes At each pass check if the array is
bubble already sorted. Best case-array
sort already sorted

Selection O(n^2) O(n^2) O(n^2) O(1) Yes Swap happens only once in a single
Sort pass.

Insertion O(n) O(n^2) O(n^2) O(1) Yes Very small constant factor even if the
Sort complexity is O(n^2).
Best case: Array already sorted
Worst case: Sorted in reverse order

Quick O(n.lg(n)) O(n^2) O(n.lg(n)) O(1) Yes Best case: When pivot divide in 2
sort equal halves
Worst case: Array already sorted
-1/n-1 partitions

Randomis O(n.lg(n)) O(n^2) O(n.lg(n)) O(1) Yes Pivot chosen randomly


ed Quick
sort

Merge O(n.lg(n)) O(n.lg(n)) O(n.lg(n)) O(n) Yes Best to sort linked-lists(constant


sort(Imp) extra space.)
Best for very large number of
elements which cannot fit in memory
(External sorting)

Counting O(n+k) O(n+k) O(n+k) O(n+2^k) Yes k= Range of Numbers in list


sort

➔ Additional important topics to ponder


◆ GCD
● Euclidean Algorithm(Recursive)
● STL function
◆ Binary search STL function
● Detailed algorithm of binary search and problems related to it will be
discussed in the coming weeks.
◆ Upper/Lower bound STL functions (used for sorted arrays)

You might also like