Week - 04-Learn Dsa With C++
Week - 04-Learn Dsa With C++
LEARN DSA
WITH C++
2 3 10 15 19 28
Find sum = 29;
2 3 10 15 19 28
Find Deff = 5;
Take Pointer
First = 0;
Second = 1;
Second move right side = different increase
First move right side = different increase
Container With Most Water << GeeksforGeeks >>
while(first<last)
{
length = last - first;
if(A[first]> A[last])
breadth = A[last--];
else
breadth = A[first++];
if(sum<length*breadth)
sum = length*breadth;
}
return sum;
}
LEARN DSA WITH C++
WEEK :: 04 DAY: 02 DATE: 09-05-2023
2 D ARRAY IN DETAIL
The two-dimensional array can be defined as an array of arrays.
2 6 8 7 5
1 - D array
2 6 8 7 5
5 5 22 3 7
9 7 7 5 8
2 - D Array
Define int arr[row][col] ;
Array A[3][4];
00 01 02 03
10 11 12 13
20 21 22 23
Store in Memory :: {Row major Order}
00 01 02 03 10 11 12 13 20 21 22 23
Index = 0 1 2 3 4 5 6 7 8 9 10 11
Solve :- 13 =>
row = 7/4=1 col = 7%4=3
00 10 20 01 11 21 02 12 22 03 13 23
Index = 0 1 2 3 4 5 6 7 8 9 10 11
int main()
{
int arr[3][4];
return 0;
};
#include<iostream>
using namespace std;
int main()
{
int arr[3][4];
return 0;
};
#include<iostream>
using namespace std;
int main()
{
int arr[3][4];
return 0;
};
Print sum ::
#include<iostream>
using namespace std;
int main()
{
int arr[3][4];
int sum=0;
for(int i=0; i<3; i++)
for(int j=0; j<4; j++)
{
sum = sum + arr[i][j];
}
cout<<sum;
return 0;
};
# Print Array Row Sum ::
#include <iostream>
using namespace std;
int main()
{
int arr[3][4];
int sum = 0;
for (int i = 0; i < 3; i++)
{
sum = 0;
for (int j = 0; j < 4; j++)
{
sum = sum + arr[i][j];
}
cout << sum<<" ";
}
return 0;
};
#include<iostream>
using namespace std;
int main()
{
int arr[3][3];
return 0;
};
#include<iostream>
using namespace std;
int main()
{
int arr[3][3];
class Solution {
public:
bool searchMatrix(vector<vector<int>>& matrix, int target) {
#include<iostream>
using namespace std;
int main()
{
int arr[5][4];
col++;
}
row= 1;
cout<<endl;
while(row<5);
{
i=row, j=3;
while(i<5)
{
cout<<arr[i][j]<<" ";
i++, j--;
};
row++;
};
return 0;
};
#Find 0 in matrix array
class Solution{
public:
int countZeros(vector<vector<int>>A)
{
//code here
int count =0, row = A.size(), col = A[0].size();
int i=0, j= col -1;
count+=j+1;
i++;
}
return count;
}
};
#include<iostream>
#include<vector> // add header file
using namespace std;
int main()
{
vector<int>v;
for(int i=1; i<=10; i++)
v.push_back(i);
for(auto x:v)
cout<<x<<" ";
return 0;
};
#include<iostream>
#include<vector> // add header file
#include<algorithm>
using namespace std;
int main()
{
vector<int> v;
for(int i = 1; i <= 10; i++)
v.push_back(i * 13 % 10);
sort(v.begin(), v.end());
for(auto x : v)
cout << x << " ";
return 0;
}
#Descending order:
#include<iostream>
#include<vector>
#include<algorithm> // add header file
using namespace std;
int main()
{
vector<int> v;
for(int i = 1; i <= 10; i++)
v.push_back(i * 13 % 10);
sort(v.rbegin(), v.rend());
for(auto x : v)
cout << x << " ";
return 0;
}
#include<iostream>
#include<vector>
using namespace std;
int main()
{
vector<int>arr(5,3);
cout<<arr.capacity()<<endl;
arr.push_back(7);
cout<<arr.capacity()<<endl;
for(auto x:arr)
cout<<x<<" ";
return 0;
};
#include<iostream>
#include<vector>
using namespace std;
int main()
{
vector<int>arr(5,3);
cout<<arr.capacity()<<" ";
for(auto x:arr)
cout<<x<<" ";
return 0;
};
#2D Vector :
No of rows and col:-
Rows = arr.size( );
Cols = arr[0].size( );
Total element = rows * col
#include<iostream>
#include<vector>
#include<algorithm>
using namespace std;
int main()
{
vector<vector<int>>arr(3, vector<int>(3));
for(int i=0; i<3; i++)
for(int j=0; j<3; j++)
cin>>arr[i][j];
return 0;
};
#Character Array :
#include<iostream>
using namespace std;
int main()
{
char arr[10];
for(int i=0; i<10; i++)
cin>>arr[i];
return 0;
};
LEARN DSA WITH C++
WEEK :: 04 DAY: 04 DATE: 11-05-2023
STRING
#Define::
String string-name; input = “pradum”;
cin>>string-name;
Store in memory:
p r a d u m
Index = 0 1 2 3 4 5
Excise[0] = “p”;
#Operation :
Add:
S = “Pradum’’, T = “Singha”;
S = S + T = “Pradum” + “Singha”; = “PradumSingh”;
#include<iostream>
#include<algorithm> // for sort
using namespace std;
int main()
{
// take input from user
string str;
getline (cin,str); // input for one line
cout<<str;
cout<<endl;
// Add operation
string s = "Pradum", t = "Singha";
s = s + t;
cout<<s;
cout<<endl;
char c = 'd'+2;
cout<<c;
cout<<endl;
// Add char behind
string a = "CoderArmy";
a.push_back('s'); // a = a + 's';
cout<<a;
cout<<endl;
// size of string
cout<<a.size();
cout<<endl;
// sort string
string k = "cdagef";
sort(k.begin(), k.end());
cout<<k;
cout<<endl;
// reverse string
reverse(k.begin(), k.end());
cout<<k;
cout<<endl;
return 0;
};
#include<iostream>
using namespace std;
int main()
{
string s;
cin >> s;
int n = s.size();
int freq[26];
for (int i = 0; i < 26; i++)
freq[i] = 0;
for (int i = 0; i < n; i++)
{
int index = s[i] - 'a';
freq[index]++;
}
for (int i = 0; i < 26; i++)
{
for (int j = 0; j < freq[i]; j++)
{
char c = 'a' + i;
cout << c;
}
}
return 0;
}
#Palindrome String :-
int minFlips(string S) {
int count1 = 0, count2 = 0;
bool flag = 0;
for (int i = 0; i < S.size(); i++) {
if (flag != S[i] - '0') {
count1++;
}
flag = !flag;
}
flag = 1;
for (int i = 0; i < S.size(); i++) {
if (flag != S[i] - '0') {
count2++;
}
flag = !flag;
}
return min(count1, count2);
}
LEARN DSA WITH C++
WEEK :: 04 DAY: 05 DATE: 12-05-2023
STRING IN HARD
#Length of the longest substring:
class Solution{
public:
int longestUniqueSubsttr(string S){
//code
bool count[26];
for(int i=0; i<26; i++)
count[i]=0;
}
};
class Solution{
public:
else
return "-1";
}
}
count++;
}
if(count)
return arr[0].substr(0, count);
else
return "-1";
}
};
#include <bits/stdc++.h>
using namespace std;
class Solution {
public:
string findSum(string X, string Y) {
int Xend = X.size()-1, Yend = Y.size()-1;
string ans = "";
int num, rem, carry=0;
char c;
while(Xend>=0 && Yend>=0)
{
num = X[Xend] - '0' + Y[Yend]-'0'+ carry;
rem = num%10;
carry = num/10;
c = rem + '0';
ans +=c;
Xend--, Yend--;
}
while(Xend>=0)
{
num = X[Xend] - '0' + carry;
rem = num%10;
c = rem + '0';
ans +=c;
carry = num/10;
Xend--;
}
while(Yend>=0)
{
num = Y[Yend] - '0' + carry;
rem = num%10;
c = rem + '0';
ans +=c;
carry = num/10;
Yend--;
}
if(carry)
ans+="1";
int i=ans.size()-1;
while(i>0 && ans[i] == '0')
{
ans.pop_back();
i--;
}
reverse(ans.begin(),ans.end());
return ans;
}
};