Lecture 03 Arrays Strings
Lecture 03 Arrays Strings
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} ;
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
ALGO++ 14
Time to try ?
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}};
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)
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