Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Some Basic Concept #19

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Binary file added Hasing.bin
Binary file not shown.
69 changes: 69 additions & 0 deletions Hasing.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
/*
Given an array of size N integer , Give Q querues and in each query given a number x .
print count of that number in arraay.

consrains :
1 <= N <= 10^5
1 <= a[i] <= 10^7
1 <= Q <= 10^5

*/

#include <bits/stdc++.h>
using namespace std;
const long long int N = 1e7+ 10;
int has_arr[N];

int main()
{
/*
int n;
cin >> n;
int arr[n];
for (int i = 0; i < n; i++)
{
cin >> arr[i];
}

int q;
cin >> q;
while (q--)
{
int x, count = 0;
cin >> x;
for (int i = 0; i < n; i++)
{
if (arr[i] == x)
{
count++;
}
}
cout << count << endl;
}
*/
// Time complexity.
// o(N) + o(Q*N) = o(N) cause Q and N are equal . so it will take time for input 10^10 .
// it won't work for 1 sec , only 10^7 can execute on online plateforms.
// To reduce this we will use precompution , Hasing a form of pre-compution.


int n;
cin >> n;
int arr[n];
for (int i = 0; i < n; i++)
{
cin >> arr[i];
has_arr[arr[i]]++; // For hasing . we are already counting value of count. at which index value is present
}
int q;
cin >> q;
while (q--)
{
int x;
cin >> x;
cout<<has_arr[x]<<endl;
}
// Time complexity = o(N) + o(Q) = 2* o(N) = 2 * 10^5

return 0;
}
Binary file added InbuildAlgo.bin
Binary file not shown.
70 changes: 70 additions & 0 deletions InbuildAlgo.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
// In STL there are lots of inbuild algorithm like max element , min element etc .

#include <bits/stdc++.h>
using namespace std;
int main()
{
int n;
cin >> n;
vector<int> v(n);
for (int i = 0; i < n; i++)
{
cin >> v[i];
}
// Min or max_element algos always retruen adress.
auto p = min_element(v.begin(), v.end()); // min_element takse 2 argument , starting and end.
cout << *(p) << endl;
int p1 = *max_element(v.begin(), v.end());
cout << p1 << endl;

// accumulate algo gives us sum of array or vector.
int init_sum = 0;
int sum = accumulate(v.begin(), v.end(), init_sum); // its take 3 argus , start , end , initial sum which we need to provide.
cout << sum << endl;

// Count function return number of count in vector/array.
int ct = count(v.begin(), v.end(), 4); // its take 3 argus , start , end , and that element which count is required.
cout << ct << endl;

// Find Function used to find an element and its return pointer or iterator.
auto it = find(v.begin(), v.end(), 10); // its take 3 argus , start , end and that value which is to be find.
if (it != v.end())
{
cout << *it;
}
else
{
cout << "Element not Found" << endl;
}

// Reverse function used to reverse an string , array or vector.
for (auto t : v)
{
cout << t << " ";
}
cout << endl;
reverse(v.begin(), v.end()); // Reverse function take two argus , start and end. And it reverse that vector.
for (auto t : v)
{
cout << t << " ";
}
cout << endl;

// In String Case.
string s = "abcdefghi";
cout<<s.size()<<endl;
for (auto t : s)
{
cout << t << " ";
}
cout << endl;
reverse(s.begin(), s.end()); // It will Also reverse the string
for (auto t : s)
{
cout << t << " ";
}
cout << endl;

return 0;

}
Binary file added Modulo.bin
Binary file not shown.
42 changes: 42 additions & 0 deletions Modulo.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
//Many times result for the given problem can not be stored even in long long int.
// to finding value of these type problem we use modulo , basicallu we tell
// that give the modulo value of exect result and we can easily store this in int data type.
// Basically it it given in 10^9+7 which can be stored in int and this is a prime number.

// let's understand it with question.
// Q - find the factorial of a given number N;
// Constrain 1<= N <=100.
//Sol :- Now factorail of 100 will is very big num , even we can not store factorail of 21 in long lng int.
// so here we have given to print modulo value.

// like print modulo of 47. so then factorail modulo will be less than 47 .

#include<iostream>
using namespace std;
int main()
{
int n ;
cin>>n;
long long int fact = 1;
int factmod = 1;
int mod = 47;
for (int i = 2; i <=n; i++)
{
fact = fact*i; // here the largest value we can store is factorial of 20. 21 factorail will be negative.
factmod = (factmod*i) % mod;
}
cout<<fact<<endl;
cout<<factmod<<endl; // it will evaluate for every value of i for 47 modulo. value will be less than modulo


return 0;
}

/*
Some Basic rule for evaluting Modulo.
(a + b) % M = ((a % M) + (b % M)) % M
(a * b) % M = ((a % M) * (b % M)) % M
(a - b) % M = (((a % M) - (b % M))+M) % M
(a / b) % M = ((a % M) * (b^-1)%M) % M

*/
Binary file added Modulo.exe
Binary file not shown.
20 changes: 20 additions & 0 deletions Pelendrom.cpp.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
#include <iostream>
#include <string.h>
using namespace std;

int main()
{
string s , str_rev;
cin>>s;
for (int i = s.size()-1; i>=0; --i)
{
str_rev.push_back(s[i]);
}
if(s==str_rev){
cout<<"Pelendrom"<<endl;
}
else{
cout<<"Not Pelendrom"<<endl;
}

}
Binary file added Pelendrom.cpp.exe
Binary file not shown.
Binary file added Pre_com_using_1D_2DArray.bin
Binary file not shown.
Binary file added Pre_com_using_1D_2DArray.exe
Binary file not shown.
Binary file added Pre_com_using_prefixSum_1d2dArray.bin
Binary file not shown.
64 changes: 64 additions & 0 deletions Pre_com_using_prefixSum_1d2dArray.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
/*
Given array a of N integers , Given Q queries and in each query given L and r.
Print sum of array element from index L to R ( L , R included).

Constraints.
1 <= N <= 10^5
1 <= a[i] <= 10^9
1 <= Q <= 10^5
1 <= L , R <= N


*/
#include <bits/stdc++.h>
using namespace std;
const int N = 1e5 + 10;
int arr[N];
int prefix_sum[N]; // array for

int main()
{
// int n;
// cin>>n;
// for (int i = 1; i <=n; i++)
// {
// cin >> arr[i];
// }
// int q;
// cin>>q;
// while (q--)
// {
// int l, r;
// cin >> l >> r;
// long long sum = 0;

// for (int i = l; i <= r; i++) // Worst case time complexity o(N) + o(Q) = o(N^2)
// {
// sum = sum + arr[i];
// }
// cout << sum << endl;
// }
// time complexity is o(N^2) and code will also not run for 10^5 input/ second. so we will sum before computation.
// In pre_computation prefix sum technique we use to store the sum of first i element.
// suppose we are at index 3 then we will also have store the sum of index 1 , 2 and 3 .
int n;
cin >> n;

for (int i = 1; i <= n; i++)
{
cin >> arr[i];

prefix_sum[i] = prefix_sum[ i - 1] + arr[i];
}
int q;
cin >> q;
while (q--)
{
int l, r;
cin >> l >> r;
cout << prefix_sum[r] - prefix_sum[l-1] << endl;
}
// Now the time complexity is o(N).
// Here we use a array in which we store sum of ith element . and for finding sum l to r we just minus r - l-1.
return 0;
}
Binary file added Pre_computation.bin
Binary file not shown.
60 changes: 60 additions & 0 deletions Pre_computation.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
// Precomputation technique used for reduce time complexit.
// In this we calculate result for every possible input brfore test cases and store then.
// then simpley pass thoses value which wil be call in Test Cases.

/*
Q- Given T test cases in each case
tese case a number N . Print its factorial for each test cases % M , where M = 10^9+7.

constraints.
1<= T <= 10^5
1 <= N <= 10^5
*/

#include <bits/stdc++.h>
using namespace std;
const int M = 1e9 + 7;

const long long int N = 1e5 + 10; // For pre-computation.
long long int fact[N];

int main()
{
// int t;
// cin >> t;
// while (t--)
// {
// int n;
// cin >> n;

// long long int fact = 1;
// for (int i = 2; i <= n; i++)
// {
// fact = (fact * i) % M ;
// }
// cout << fact << endl;
// }
// Time complexity of above code is o(T*N) , T and N are equal (10^5) , so complexity is o(N^2).
// The problem of this code is it will not run for big digit . total time taken is 10^10,
// this will not run in per second . on online coading we have to complie input max 10^7 input per second.
//
// there for we will calculate value for each input 1 to 10^5 in array of size [10^5] and store them earlier the program begain.

fact[0] = fact[1] = 1;
for (int i = 2; i <= N; i++)
{
fact[i] = fact[i-1] * i;
}
int t;
cin >> t;
while (t--)
{
int n;
cin >> n;
cout << fact[n] % M<< endl;
}
// Old time complwsity = o(N^2);
// This time complexity = for storing every factorial o(N) + for every testcases o(T) = o(N).

return 0;
}
Binary file added Pre_computation.exe
Binary file not shown.
Binary file added Prefix_sum_2dArray.bin
Binary file not shown.
Loading