0% found this document useful (0 votes)
78 views

Lecture 03 Arrays Strings

This document provides an overview of algorithms and data structures topics covered in Lecture 3 of the Algo++ course. The key topics covered include: - Binary search - Arrays and strings - Finding the maximum subarray sum in an array - Using binary search to find the square root of a number and solve the minimum pages allocation problem - Finding the pivot element in a sorted and rotated array - Solving miscellaneous problems involving sorting waves in an array and counting set bits - Basics of character arrays and strings in C/C++ - Input and output using cin and getline
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
78 views

Lecture 03 Arrays Strings

This document provides an overview of algorithms and data structures topics covered in Lecture 3 of the Algo++ course. The key topics covered include: - Binary search - Arrays and strings - Finding the maximum subarray sum in an array - Using binary search to find the square root of a number and solve the minimum pages allocation problem - Finding the pivot element in a sorted and rotated array - Solving miscellaneous problems involving sorting waves in an array and counting set bits - Basics of character arrays and strings in C/C++ - Input and output using cin and getline
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 22

Algo++

Lecture-03
ALGO++
• Binary Search
• Arrays & Strings

Prateek Narang
Miscs Problem
 Iterativelyfind subsequences of strings
 Subtrings of a string.
 Find no of set bits in number N.

ALGO++ 2
Misc Problem
 Maximum Subarray Sum

A[] = { 1,-2,3,4,6,-5,8,1,-4, 2} ;

Find a subarray whose sum is maximum.

ALGO++ 3
Binary Search
 Square Root of a number

ALGO++ 4
Binary Search
 Assign Min Pages

Pages[] = {10,20,30,40}
Students = K

ALGO++ 5
Binary Search
 Finding Pivot in sorted rotated array

ALGO++ 6
Time to think ?
 Search an element in sorted & rotated array

ALGO++ 7
Miscellaneous Problem
 Wave Sort
(Popular Interview Question)

ALGO++ 8
Character Array Basics
 char str[100];
 char str[4] = { ‘A’, ‘B’, ‘C’, ‘D’};
 char str[] = {‘A’, ‘B’, ‘C’};
 char str[] = “Welcome”;
 char str[8] = “Welcome”;

ALGO++ 9
Strings
 In C/C++ we use a character array to
simulate strings.
 By convention, a string is a sequence of
characters followed by a null character.
 Null characters is a special character whose
ascii value is 0 and its representation is ‘\0’
 In the previous slide example 4 and 5 are
valid strings

ALGO++ 10
String Class in STL
 Important Functions
String() Constructor
Length()
[ ] Operator

More functions at -
http://www.cplusplus.com/reference/string/string/

ALGO++ 11
Array of strings !
 We simulated a string by a 1-D character
array.
 Similarly we can simulate a list of strings by 2-
D character array.
 char stringlist[10][100];
 Above can store max 10 strings each of
maxlength 100.
 And each string can be accessed by
strlinglist[i].

ALGO++
Input and Output

 cin
 cin.get();
 cin.getline(array,delim);
 getline(cin,string,delim);

ALGO++ 13
Problems

 Read N strings from a user and print the


largest string.
 Write a function to check if two strings are
permutations of each other.
 Write a program to print all substrings of a
given string

ALGO++ 14
Time to try ?

 Sort an array of Strings based on –


- Lexicographically
- Based upon Length

Solve the above questions using STL also.

Bonus : Try STRING SORT on hacker-blocks.

ALGO++ 15
2D Arrays
 int array1[2][3];
 int array2[2][3] = {{1,2,3}, {4,5,6}};
 Int array[][4] = {{1,2,3,4}, {4,5,6,7}, {8,9,10}};

 char array3[3][2] = {{‘A’,’B’}, {‘C’,’D’},


{‘E’,’F’}};

 char array4[][4] = {“abc”, “def”, “efg”,


“hig”};

ALGO++ 16
Problems
 Rotate a n X n Image by 90 degrees CW
 Spiral Print

ALGO++ 17
Know about - strtok() function
 Implementa strtok function
( www.codingblocks.com/resources)

char * strtok ( char * str, char delimiter );

ALGO++ 18
19

Today’s CodeByte-1
 Writea program to create a matrix of
alternate rectangles of O and X
For N = 5;
OOOOO
OXXXO
OXOXO
OXXXO
OOOOO

ALGO++
20

Today’s CodeByte-2
 Code Min Number of Pages problem

ALGO++
21

Today’s CodeByte-3
 Median of two sorted arrays of equal size.
Use binary search.

ALGO++
Thank you
ALGO++

Prateek Narang

You might also like