diff --git a/Hasing.bin b/Hasing.bin new file mode 100644 index 0000000..e7245c7 Binary files /dev/null and b/Hasing.bin differ diff --git a/Hasing.cpp b/Hasing.cpp new file mode 100644 index 0000000..61b0a48 --- /dev/null +++ b/Hasing.cpp @@ -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 +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< +using namespace std; +int main() +{ + int n; + cin >> n; + vector 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< +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< +#include +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"< +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; +} \ No newline at end of file diff --git a/Pre_computation.bin b/Pre_computation.bin new file mode 100644 index 0000000..f410d19 Binary files /dev/null and b/Pre_computation.bin differ diff --git a/Pre_computation.cpp b/Pre_computation.cpp new file mode 100644 index 0000000..390522b --- /dev/null +++ b/Pre_computation.cpp @@ -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 +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; +} \ No newline at end of file diff --git a/Pre_computation.exe b/Pre_computation.exe new file mode 100644 index 0000000..b551661 Binary files /dev/null and b/Pre_computation.exe differ diff --git a/Prefix_sum_2dArray.bin b/Prefix_sum_2dArray.bin new file mode 100644 index 0000000..3025eb3 Binary files /dev/null and b/Prefix_sum_2dArray.bin differ diff --git a/Prefix_sum_2dArray.cpp b/Prefix_sum_2dArray.cpp new file mode 100644 index 0000000..b86afff --- /dev/null +++ b/Prefix_sum_2dArray.cpp @@ -0,0 +1,72 @@ +/* + +Given an array of N*N integers. given Q queries and in each queary given a , b , c and d +print sum of square represented by (a ,b) as top left point and (c ,d) as bottom right point. + +Constraints. +1 <= N <= 10^3 +1 <= a[i][j] <= 10^9 +1 <= Q <= 10^5 +1 <= a ,b ,c <= N + +*/ + +#include +using namespace std; +const int N = 10 ^ 3 + 10; +long long int arr[N][N]; +long long int pre_arr[N][N]; + +int main() +{ + // int n; + // cin >> n; + // for (int i = 1; i <= n; i++) + // { + // for (int j = 1; j <= n; j++) + // { + // cin >> arr[i][j]; + // } + // } + // int q; + // cin >> q; + // while (q--) + // { + // int a, b, c, d; + // cin >> a >> b >> c >> d; + // long long int sum = 0; + // for (int i = a; i <= c; i++) + // { + // for (int j = b; j <= d; j++) + // { + // sum = sum + arr[i][j]; + // } + // } + // cout << sum << endl; + // } + /// Time complexity is o(N^2) + o(Q*N^2) = 10^5 * 10^6 = 10^11 , not possible for 1 sec/ input. + + int n; + cin >> n; + for (int i = 1; i <= n; i++) + { + for (int j = 1; j <= n; j++) + { + cin >> arr[i][j]; + pre_arr[i][j] = arr[i][j] + pre_arr[i - 1][j] + pre_arr[i][j - 1] - pre_arr[i - 1][j - 1]; + } + } + + int q; + cin >> q; + while (q--) + { + int a, b, c, d; + cin >> a >> b >> c >> d; + long long int sum = 0; + + cout << pre_arr[c][d] - pre_arr[a - 1][d] - pre_arr[c][b - 1] + pre_arr[a - 1][b - 1] << endl; + } + // Time complexity is o(N) + o(Q) = o(N). + return 0; +} \ No newline at end of file diff --git a/Prefix_sum_2dArray.exe b/Prefix_sum_2dArray.exe new file mode 100644 index 0000000..f1060e0 Binary files /dev/null and b/Prefix_sum_2dArray.exe differ diff --git a/PrimeOrNOt.bin b/PrimeOrNOt.bin new file mode 100644 index 0000000..5644a89 Binary files /dev/null and b/PrimeOrNOt.bin differ diff --git a/PrimeOrNOt.cpp b/PrimeOrNOt.cpp new file mode 100644 index 0000000..55a418d --- /dev/null +++ b/PrimeOrNOt.cpp @@ -0,0 +1,24 @@ +#include +using namespace std; +int main() +{ + int t ; + cin>>t; + while(t--){ + int n; + cin>>n; + int prime = 0; + for(int i = 2 ; i *i <= n-1 ; i++){ + + if(n % i == 0){ + prime++; + } + } + if(prime){ + cout<<"Not Prime"< +using namespace std; + +int main() +{ + int t; + cin >> t; + while (t--) + { + int n; + cin >> n; + + int s[n]; + for (int i = 0; i < n; ++i) + { + cin >> s[i]; + } + + long long mul = 1; + for (int i = 0; i < n; ++i) + { + mul = mul * s[i]; + } + + //cout< +using namespace std; + +char upper(char c) +{ + // return 'A' + (c - 'a'); + char c1 = c-32; + return c1; + +} +char lower(char c) +{ + char c1 = c+32; + return c1; + +} +int main() +{ + + while (true) + { + string s; + cin >> s; + + // getline(cin , s); + + // for(int i = 0 ; i < s.size() ; ++i){ + // if(s[i] == ' '){ + // cout<