DSA Notes
DSA Notes
LEARN DSA
WITH C++
a b a b
Prefix : a, ab, aba;
Suffix : b, ab, bab; Output = 2;
a a a a
Prefix : a, aa, aaa;
Suffix : a, aa, aaa; Output : 3
#Solve
a b a b
Prefix pointer : a [ Prefix always start from first element of array ]
Suffix pointer : a [Suffix start from second a because Prefix start a ]
Output : 2
#Example :: 02 -
a b c a b d a b c a
Pointer
Prefix Suffix
a a Prefix start first element; and Suffix start second a.
b b increment
c d Not same [ wrong ]
Agine
a a Now Suffix start Third a
b b
c c
d d loop break Output: 4 Time Complexity : O(N^2) N=size of array
Optimization Code :: -
a b c d e a b
Prefix =a So 0 0 0 0 0 1 1 0 = Not same
Suffix=a OutPut = 2
Exp: 2.1
a b c d a b c e h a b c d a b
Prefix = a 0 0 00 1 1 1 0 0 1 1 1 1 1 1
Suffix = a OutPut = 6
Exp : 2.2
ind 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18
str a b c d a b c e a b c d a b c d a b
LPA 0 0 0 0 1 2 3 0 1 2 3 4 5 6 7 4 5 6
Index start = 1; LPA = write index, when first and second pointer same str.
Pointer
First Second
1 5
2 6
3 7
Not same
1 9
2 10
3 11
4 12
5 13
6 14
7 15
Not Same
4 16
5 17
6 18
#Longest Prefix Suffix >> GeeksforGeek <<
class Solution{
public:
int lps(string s) {
// Your code goes here
int index[s.size()+1];
char str[s.size()+1];
index[s.size()]=0;
str1 : a m a z o n
str2 : o n a m a z
ind 1 2 3 4 5 6 7 8 9 10 11 12 13
str a m a z o n $ o n a m a z
LPA 0 0 1 0 0 0 0 0 0 1 2 3 4
LPA = 4; Rotation = size of str (6) - LPA (4) = 2
#Convert palindrome ::
#Exp : 2 : abc
Str : a b c
First second
a c Not match
Str: c a b c add first position from last elem
a b Not match
Str : c b a b c add second position from second last elm
:: a b c biggest Palindrome=a ; add other elm in front b c = c b a b c = 3 - 1 = 2
:: r o o r t b biggest Palindrome = r o o r ; add other elm in front t b = b t r o o r b t = 6 - 4 = 2
#Minimum characters to be added at front to make string palindrome >> GeeksforGeek <<
Str : r o o r t b
ind 1 2 3 4 5 6 7 8 9 10 11 12 13
str r o o r t b $ b t r o o r
LPA 0 0 0 1 0 0 0 0 0 1 2 3 4
Biggest palindrome = 4;
Size of String = 6 Minimum character add = 6-4 = 2
LEARN DSA WITH C++
WEEK :: 05 DAY: 02 DATE: 16-05-2023
POINTER IN C++
#Memory Store ::
Int num = 5;
0 0 0 32 bit, 4 Byte 0 0 0 1 0 1
Read only 32 bit because data int type
Char name = ‘ a ‘;
a convert binary = 1 1 0 0 0 0 1 ascii value = 97
0 1 1 0 0 0 0 1
Read only 8 bit because data char type
#Address :
Int *p;
P is a pointer which is pointed int type value.
Every data defines an address in memory.
Change value
#include<iostream>
using namespace std;
int main()
{
int num = 30;
cout<<num<<endl;
int *p;
p = #
*p = 20;
cout<<num;
return 0;
};
#Address define ::
Arr = Base address ( 500)
(Arr + 0) = 500 + 0*4
(Arr + 1) = 500 + 1*4
(Arr + 2) = 500 + 2*4
If i enter the inside of array so use pointer :: *(arr +3) = 500 + 3*4
Array already carry address so, don't need to add & when : p = arr; don’t p = & arr;
#include<iostream> #include<iostream>
using namespace std; using namespace std;
P++ Arr++
#address Change ::
Int num1 = 5, num2 = 10;
Int *p , *q;
P = & num1;
q = & num2;
Pointer p q
Value 5 10
Now p = q;
Pointer p q
Value 10 10
#Call by pointer (Address) ::
#include<iostream>
using namespace std;
return 0;
};
Call By Reference
#include<iostream>
using namespace std;
return 0;
};
int main()
{
int a = 100;
int *p = &a;
cout<<a<<endl; // a value
cout<<&a<<endl; // a address
cout<<p<<endl; // a address
cout<<*p<<endl; // a value
cout<<&p<<endl; // p address
return 0;
};
LEARN DSA WITH C++
WEEK :: 05 DAY: 03 DATE: 17-05-2023
POINTER IN ADVANCE
#Reference Variable ::
Int i = 20;
Int & j = i;
i and j pointed to the same value 20.
#include <iostream>
using namespace std;
a[ ] = * a is same
p[0] = *(p +0)
// a[] and *a both are pointer
void Double_value(int a[], int size)
{
for (int i = 0; i < size; i++)
{
*a = (*a) * 2;
a++;
}
}
int main()
{
int arr[5] = {2, 5, 6, 8, 3};
Double_value(arr, 5);
for (int i = 0; i < 5; i++)
cout << arr[i] << " ";
return 0;
};
cout<<arr; 100
cout<<&arr; 100
cout<<&arr[0]; 100
#cout<<arr :: address of first element in arr.
#cout<<&arr :: arr where it is stored in memory location.
#cout<<&arr[0] :: address of 0 index position element
#include <iostream>
using namespace std;
int main()
{
int arr[5] = {2, 5, 6, 8, 3};
cout << arr<<endl;
cout << &arr<<endl;
cout << &arr[0] <<endl;
return 0;
};
# 2 D ARRAY ::
Arr :: store address of first row
Arr + 1 :: store address of second row
Arr + 2 :: store address of third row
Arr + n :: store address of n row
&arr :: what is the address of array in memory location
*arr :: address of the first element of the first row.
**arr :: value of the first element of the first row
arr[0] = *(arr + 0) :: address of first element in first row
&(*(arr+0)) = *(&(arr+0)) :: Address of first row
HOME ADDRESS
100 104 112
## Arr[i][ j] = *(*(Arr + i) + j)
[ Define i and j ]
# Address & Pointer ::
Int num = 10; There is one num, it’s type int and store value 10.
Int *P = # There is one P, it is a pointer which is pointed to int type value.
Int ***T = &X T is a pointer which is pointed to another pointer, it also pointed to
another pointer and finally it is pointed to int value.
MEMORY
HEAP
Stack
Global/Local variable
code
#Stack :: Stack memory is a memory usage mechanism that allows the system memory to be used as
temporary data storage that behaves as a first-in-last-out buffer.
#HEAP :: A memory heap is a location in memory where memory may be allocated at random
access.
HEAP Memory >> STACK Memory
#include<iostream>
using namespace std;
int main()
{
/*int *p = new int;
*p = 10;
cout<<*p<<endl;
cout<<"address: "<<p;*/
// for array
int *p = new int[10];
for(int i=0; i<10; i++)
p[i]= i*2;
return 0;
return 0;
};
# DELETE Memory ::
Stack are automatically deleted or Heap memory delete manually
Int *p = new int; ⇒ allocate memory in heap delete address using delete P.
Int *p = p[ ] ⇒ delete address using delete p[ ].
LEARN DSA WITH C++
WEEK :: 05 DAY: 04 DATE: 18-05-2023
2D Array with POINTER + Recursion
#2D Array :
Define : const int col = 5;
Void Fun (int arr[ ] [col], Row)
{
}
int arr[m]
int arr[row][col] m, row & col = always constant.
#Take Input ::
vector<int>v; store in heap automatic delete.
int *p = new int[m] variable(m) can take
Take input 2D Array
int **p = new int *[10]
for(int i=0; i<10; i++)
P[i] = new int [5];
#include<iostream>
using namespace std;
int main()
{
int m;
cin >> m;
int *p = new int[m];
int *temp = p; // Create a temporary pointer to iterate over the
array
return 0;
}
int main()
{
int m;
cin >> m;
int* p = new int[m];
fun(p, m);
return 0;
}
fun(p, n, m);
RECURSION
Recursion is the technique of making a function call itself.
# Recursion importance part :
1. Breaking Condition
2. Calling itself
Exp: 01 :: Factorial
Calculate Factorial Using Recursion ::
5! 5 * 4! 120
4! 4 * 3! 24
3! 3 * 2! 6
2! 2 * 1! 2
1! 1 1
## Code ::
Int main ( )
{ int ans = factorial(n-1);
Int n; ans = ans* n;
cin>> n; return ans;
cout<<Factorial(n); }
Return 0; int main()
{
int n;
cin>>n;
cout<<factorial(n);
return 0;
};
#Power :: 3^n
int power(int n)
{
if(n==1)
return 3;
return 3*power(n-1);
}
int main()
{
int n;
cin>>n;
cout<<power(n);
return 0;
};
|| FIBONACCI ||
#include<iostream>
using namespace std;
int fib(int n)
{
if(n==1)
return 0;
else if(n==2)
return 1;
return 0;
};
LEARN DSA WITH C++
WEEK :: 05 DAY: 05 DATE: 19-05-2023
RECURSION
#include<iostream>
using namespace std;
cout<<start<<" ";
print_num(start+1, end);
}
int main()
{
int n = 10;
print_num(1, 10);
return 0;
};
3 1 2 5 8
3 4 6 11 19
#include<iostream>
using namespace std;
return 0;
};
if(a[0] == key)
return 1;
return 0;
};
return 0;
};
if(a[mid]== key)
return 1;
else if(a[mid]>key)
return binary_search(a, start, mid-1, key);
else
return binary_search(a, mid+1, end, key);
}
int main()
{
int arr[10] = { 2, 3, 4, 6, 7, 9, 10, 15, 16, 18};
int key = 16;
cout<<binary_search(arr, 0, 9, key);
return 0;
};
void binary_search(int *a, int start, int end, int key, int &index)
{
if(end<start)
return;
if(a[mid]== key)
{
index = mid;
return ;
}
else if(a[mid]>key)
return binary_search(a, start, mid-1, key, index);
else
return binary_search(a, mid+1, end, key, index);
}
int main()
{
int arr[10] = { 2, 3, 4, 6, 7, 9, 10, 15, 16, 18};
int key = 16;
int index = -1;
binary_search(arr, 0, 9, key, index);
cout<<index;
return 0;
};