Algorithms Bootcamp 

NIT Raipur

21-22 Jan

Prateek Narang

Engineer, Instructor, Founder Coding Minutes

Algorithmic Paradigms

 

  • Brute Force
  • Backtracking
  • Divide & Conquer
  • Greedy Algorithms
  • Dynamic Programming

Optimisation Techniques on Arrays

- Prefix Sums 

- Carry Forward 

- Hashing

- Sliding Window

- Two Pointer

 

(easy to implement and yet powerful)

Prefix Sums

 

Given an array with N elements, count the number of equlibirum indexes in the array. Equilbrium Index : an index will be call eq index if sum of all elements to the left of that is same as right of that index.

-7  1  5 2 -4 3 0

ans = 2

2D Prefix Sums

Ramu's father has left a farm organized as an N × N grid. Each square in the grid either has or does not have a mango tree. He has to divide the farm with his three sisters as follows: he will draw one horizontal line and one vertical line to divide the field into four rectangles. His sisters will choose three of the four smaller fields and he gets the last one.

He wants to divide the field so that he gets the maximum number of mangos possible, assuming that his sisters will pick the best three rectangles.

 

For example, suppose the field looks as follows:

      . # # . . .
      # . . # # .
      . # . . . .
      . # # . . #
      # . . # # .
      . # . . . .

Ramu can ensure that he gets at least 3 mango trees by cutting as follows:

        . # | # . . .
        # . | . # # .
        . # | . . . .
      ------+---------
        . # | # . . #
        # . | . # # .
        . # | . . . .
      

 

Given a character array, calc no of pairs of indices (i,j) such that i<j and arr[i] = 'a' and arr[j] = 'g'

Carry Forward Technique

b a a g d c a g

 

output : 5

Sliding Window Techniques

Along one side of a road there is a sequence of vacant plots of land. Each plot has a different area (non-zero). So, the areas form a sequence A[1], A[2], … A[N].

 

You want to buy K acres of land to build a house. You want to find a segment of continguous plots (i.e., a subsection in the sequence) of minimum length whose sum is exactly K.

Brute Force Techniques

  • Pair Sum Problem

  • Triplet Sum

Backtracking based Techniques

Finding solutions to some computational problems, notably constraint satisfaction problems, that incrementally builds candidates to the solutions, and abandons a candidate ("backtracks") as soon as it determines that the candidate cannot possibly be completed to a valid solution.

- NQueen Problem

- Sudoku Problem

- Min Len String

Given two non-empty arrays, write a function that determines whether the second arrays is subsequence of first one.

Sample Input

[5, 1, 22, 25, 6, -1, 8, 10]
[1, 22, 8]

Sample Output
Yes
 

Check Subsequence

Brute Force Techniques

  • Pair Sum Problem

  • Triplet Sum

Indian Coin Change

Suppose you want to make a change of some Indian Rupees () using minimum number of coins / notes. 


 Denominations = [1,2,5,10,20,50,100,500,2000]

 

Input

₹ 273

 

Output

200 + 50 + 20 + 2 + 1

Activity Selection

Code

By Prateek Narang

Code

  • 20