Skip to content

Commit

Permalink
initial commit
Browse files Browse the repository at this point in the history
  • Loading branch information
khushal87 committed Jan 12, 2021
0 parents commit 34f5420
Show file tree
Hide file tree
Showing 122 changed files with 3,599 additions and 0 deletions.
16 changes: 16 additions & 0 deletions .vscode/c_cpp_properties.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
{
"configurations": [
{
"name": "Linux",
"includePath": [
"${workspaceFolder}/**"
],
"defines": [],
"compilerPath": "/usr/bin/clang",
"cStandard": "c11",
"cppStandard": "c++14",
"intelliSenseMode": "clang-x64"
}
],
"version": 4
}
7 changes: 7 additions & 0 deletions .vscode/settings.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
{
"files.associations": {
"iostream": "cpp",
"vector": "cpp",
"ostream": "cpp"
}
}
5 changes: 5 additions & 0 deletions Array/._GLOBAL__sub_I_reverse_an_array.cpp.dot
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
digraph "CFG for '_GLOBAL__sub_I_reverse_an_array.cpp' function" {
label="CFG for '_GLOBAL__sub_I_reverse_an_array.cpp' function";

Node0x1313310 [shape=record,label="{%0:\l call void @__cxx_global_var_init()\l ret void\l}"];
}
12 changes: 12 additions & 0 deletions Array/._Z5solvePiii.dot
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
digraph "CFG for '_Z5solvePiii' function" {
label="CFG for '_Z5solvePiii' function";

Node0x130ee30 [shape=record,label="{%3:\l %4 = alloca i32*, align 8\l %5 = alloca i32, align 4\l %6 = alloca i32, align 4\l %7 = alloca i32, align 4\l store i32* %0, i32** %4, align 8\l store i32 %1, i32* %5, align 4\l store i32 %2, i32* %6, align 4\l br label %8\l}"];
Node0x130ee30 -> Node0x130f120;
Node0x130f120 [shape=record,label="{%8:\l8: \l %9 = load i32, i32* %5, align 4\l %10 = load i32, i32* %6, align 4\l %11 = icmp slt i32 %9, %10\l br i1 %11, label %12, label %36\l|{<s0>T|<s1>F}}"];
Node0x130f120:s0 -> Node0x130eef0;
Node0x130f120:s1 -> Node0x130f300;
Node0x130eef0 [shape=record,label="{%12:\l12: \l %13 = load i32*, i32** %4, align 8\l %14 = load i32, i32* %5, align 4\l %15 = sext i32 %14 to i64\l %16 = getelementptr inbounds i32, i32* %13, i64 %15\l %17 = load i32, i32* %16, align 4\l store i32 %17, i32* %7, align 4\l %18 = load i32*, i32** %4, align 8\l %19 = load i32, i32* %6, align 4\l %20 = sext i32 %19 to i64\l %21 = getelementptr inbounds i32, i32* %18, i64 %20\l %22 = load i32, i32* %21, align 4\l %23 = load i32*, i32** %4, align 8\l %24 = load i32, i32* %5, align 4\l %25 = sext i32 %24 to i64\l %26 = getelementptr inbounds i32, i32* %23, i64 %25\l store i32 %22, i32* %26, align 4\l %27 = load i32, i32* %7, align 4\l %28 = load i32*, i32** %4, align 8\l %29 = load i32, i32* %6, align 4\l %30 = sext i32 %29 to i64\l %31 = getelementptr inbounds i32, i32* %28, i64 %30\l store i32 %27, i32* %31, align 4\l %32 = load i32, i32* %5, align 4\l %33 = add nsw i32 %32, 1\l store i32 %33, i32* %5, align 4\l %34 = load i32, i32* %6, align 4\l %35 = add nsw i32 %34, -1\l store i32 %35, i32* %6, align 4\l br label %8\l}"];
Node0x130eef0 -> Node0x130f120;
Node0x130f300 [shape=record,label="{%36:\l36: \l ret void\l}"];
}
5 changes: 5 additions & 0 deletions Array/.__cxx_global_var_init.dot
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
digraph "CFG for '__cxx_global_var_init' function" {
label="CFG for '__cxx_global_var_init' function";

Node0x130e2d0 [shape=record,label="{%0:\l call void @_ZNSt8ios_base4InitC1Ev(%\"class.std::ios_base::Init\"*\l... @_ZStL8__ioinit)\l %1 = call i32 @__cxa_atexit(void (i8*)* bitcast (void\l... (%\"class.std::ios_base::Init\"*)* @_ZNSt8ios_base4InitD1Ev to void (i8*)*),\l... i8* getelementptr inbounds (%\"class.std::ios_base::Init\",\l... %\"class.std::ios_base::Init\"* @_ZStL8__ioinit, i32 0, i32 0), i8*\l... @__dso_handle) #3\l ret void\l}"];
}
23 changes: 23 additions & 0 deletions Array/.main.dot
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
digraph "CFG for 'main' function" {
label="CFG for 'main' function";

Node0x1310490 [shape=record,label="{%0:\l %1 = alloca i32, align 4\l %2 = alloca i32, align 4\l %3 = alloca i8*, align 8\l %4 = alloca i64, align 8\l %5 = alloca i32, align 4\l %6 = alloca i32, align 4\l store i32 0, i32* %1, align 4\l %7 = call dereferenceable(280) %\"class.std::basic_istream\"*\l... @_ZNSirsERi(%\"class.std::basic_istream\"* @_ZSt3cin, i32* dereferenceable(4)\l... %2)\l %8 = load i32, i32* %2, align 4\l %9 = zext i32 %8 to i64\l %10 = call i8* @llvm.stacksave()\l store i8* %10, i8** %3, align 8\l %11 = alloca i32, i64 %9, align 16\l store i64 %9, i64* %4, align 8\l store i32 0, i32* %5, align 4\l br label %12\l}"];
Node0x1310490 -> Node0x13110b0;
Node0x13110b0 [shape=record,label="{%12:\l12: \l %13 = load i32, i32* %5, align 4\l %14 = load i32, i32* %2, align 4\l %15 = icmp slt i32 %13, %14\l br i1 %15, label %16, label %24\l|{<s0>T|<s1>F}}"];
Node0x13110b0:s0 -> Node0x1311290;
Node0x13110b0:s1 -> Node0x13112e0;
Node0x1311290 [shape=record,label="{%16:\l16: \l %17 = load i32, i32* %5, align 4\l %18 = sext i32 %17 to i64\l %19 = getelementptr inbounds i32, i32* %11, i64 %18\l %20 = call dereferenceable(280) %\"class.std::basic_istream\"*\l... @_ZNSirsERi(%\"class.std::basic_istream\"* @_ZSt3cin, i32* dereferenceable(4)\l... %19)\l br label %21\l}"];
Node0x1311290 -> Node0x1311650;
Node0x1311650 [shape=record,label="{%21:\l21: \l %22 = load i32, i32* %5, align 4\l %23 = add nsw i32 %22, 1\l store i32 %23, i32* %5, align 4\l br label %12\l}"];
Node0x1311650 -> Node0x13110b0;
Node0x13112e0 [shape=record,label="{%24:\l24: \l %25 = load i32, i32* %2, align 4\l %26 = sub nsw i32 %25, 1\l call void @_Z5solvePiii(i32* %11, i32 0, i32 %26)\l store i32 0, i32* %6, align 4\l br label %27\l}"];
Node0x13112e0 -> Node0x1311b30;
Node0x1311b30 [shape=record,label="{%27:\l27: \l %28 = load i32, i32* %6, align 4\l %29 = load i32, i32* %2, align 4\l %30 = icmp slt i32 %28, %29\l br i1 %30, label %31, label %41\l|{<s0>T|<s1>F}}"];
Node0x1311b30:s0 -> Node0x1311d10;
Node0x1311b30:s1 -> Node0x1311d60;
Node0x1311d10 [shape=record,label="{%31:\l31: \l %32 = load i32, i32* %6, align 4\l %33 = sext i32 %32 to i64\l %34 = getelementptr inbounds i32, i32* %11, i64 %33\l %35 = load i32, i32* %34, align 4\l %36 = call dereferenceable(272) %\"class.std::basic_ostream\"*\l... @_ZNSolsEi(%\"class.std::basic_ostream\"* @_ZSt4cout, i32 %35)\l %37 = call dereferenceable(272) %\"class.std::basic_ostream\"*\l... @_ZStlsISt11char_traitsIcEERSt13basic_ostreamIcT_ES5_PKc(%\"class.std::basic_os\l...tream\"* dereferenceable(272) %36, i8* getelementptr inbounds ([2 x i8], [2 x\l... i8]* @.str, i64 0, i64 0))\l br label %38\l}"];
Node0x1311d10 -> Node0x1312590;
Node0x1312590 [shape=record,label="{%38:\l38: \l %39 = load i32, i32* %6, align 4\l %40 = add nsw i32 %39, 1\l store i32 %40, i32* %6, align 4\l br label %27\l}"];
Node0x1312590 -> Node0x1311b30;
Node0x1311d60 [shape=record,label="{%41:\l41: \l %42 = call dereferenceable(272) %\"class.std::basic_ostream\"*\l... @_ZNSolsEPFRSoS_E(%\"class.std::basic_ostream\"* @_ZSt4cout,\l... %\"class.std::basic_ostream\"* (%\"class.std::basic_ostream\"*)*\l... @_ZSt4endlIcSt11char_traitsIcEERSt13basic_ostreamIT_T0_ES6_)\l %43 = load i8*, i8** %3, align 8\l call void @llvm.stackrestore(i8* %43)\l %44 = load i32, i32* %1, align 4\l ret i32 %44\l}"];
}
Binary file added Array/best_time_to_buy_and_sell_stock
Binary file not shown.
29 changes: 29 additions & 0 deletions Array/best_time_to_buy_and_sell_stock.c++
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
#include <bits/stdc++.h>
using namespace std;

int maxProfit(vector<int>& prices) {
int max_so_far=0;
int max_ending_here=0;
for(int i=1;i<prices.size();i++){
max_ending_here+=prices[i]-prices[i-1];
if(max_ending_here>max_so_far){
max_so_far=max_ending_here;
}
if(max_ending_here<0){
max_ending_here=0;
}
}
return max_so_far;
}

int main(){
int n;
cin>>n;
vector <int> v;
for(int i=0;i<n;i++){
int x;
cin>>x;
v.push_back(x);
}
cout<<maxProfit(v)<<endl;
}
Binary file added Array/check_if_array_is_subset_of_another_array
Binary file not shown.
55 changes: 55 additions & 0 deletions Array/check_if_array_is_subset_of_another_array.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
#include <bits/stdc++.h>
using namespace std;
int main()
{
//code
int t;
cin >> t;
while (t--)
{
int m, n;
cin >> n >> m;
int arr[n], arr1[m];
map<int, int> mp, mp1;
for (int i = 0; i < n; i++)
{
cin >> arr[i];
mp[arr[i]]++;
}
for (int i = 0; i < m; i++)
{
cin >> arr1[i];
mp1[arr1[i]]++;
}
int flag = 0;
for (auto j : mp1)
{
if (mp.count(j.first) > 0)
{
if ((mp[j.first] - j.second) < 0)
{
flag = 1;
break;
}
else
{
flag = 0;
}
}
else
{
flag = 1;
break;
}
}
if (flag == 1)
{
cout << "No" << endl;
}
else
{
cout << "Yes" << endl;
}
}
return 0;
}
Binary file added Array/count_pairs_with_given_sum
Binary file not shown.
35 changes: 35 additions & 0 deletions Array/count_pairs_with_given_sum.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
#include <bits/stdc++.h>
using namespace std;

int solve(int arr[], int n, int sum)
{
unordered_map<int, int> mp;
for (int i = 0; i < n; i++)
{
mp[arr[i]]++;
}
int count = 0;
for (int i = 0; i < n; i++)
{
count += mp[sum - arr[i]];
if (sum - arr[i] == arr[i])
{
count--;
}
}
return count / 2;
}

int main()
{
int n;
cin >> n;
int arr[n];
int sum;
for (int i = 0; i < n; i++)
{
cin >> arr[i];
}
cin >> sum;
cout << solve(arr, n, sum) << endl;
}
Binary file added Array/kadane_algorithm
Binary file not shown.
28 changes: 28 additions & 0 deletions Array/kadane_algorithm.c++
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
#include <bits/stdc++.h>
using namespace std;

int solve(int arr[],int n){
int max_so_far=INT_MIN;
int max_ending_here = 0;
for(int i=0;i<n;i++){
max_ending_here+=arr[i];
if(max_ending_here>max_so_far){
max_so_far=max_ending_here;
}
if(max_ending_here<0){
max_ending_here=0;
}
}
return max_so_far;
}

int main(){
int n;
cin>>n;
int arr[n];
for(int i=0;i<n;i++){
cin>>arr[i];
}
cout<<solve(arr,n);
cout<<endl;
}
Binary file added Array/kth_min_element
Binary file not shown.
23 changes: 23 additions & 0 deletions Array/kth_min_element.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
#include<bits/stdc++.h>
using namespace std;

int kThMin(int arr[],int n,int k){
sort(arr,arr+n);
return arr[k-1];
}

int main()
{
//code

int n;
cin>>n;
int arr[n];
for(int i=0;i<n;i++){
cin>>arr[i];
}
int k;
cin>>k;
cout<<kThMin(arr,n,k)<<endl;
return 0;
}
18 changes: 18 additions & 0 deletions Array/max_min_in_arrays/1.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
#include <bits/stdc++.h>
using namespace std;

int main(){
int n;
cin>>n;
set <int> st;
for(int i=0;i<n;i++){
int x;
cin>>x;
st.insert(x);
}
int ans;
for(auto j:st){
ans=j;
}
cout<<*st.begin()<<" "<<ans<<endl;
}
Binary file added Array/max_min_in_arrays/2
Binary file not shown.
57 changes: 57 additions & 0 deletions Array/max_min_in_arrays/2.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
#include "bits/stdc++.h"
using namespace std;

struct Pair
{
/* data */
int min;
int max;
};

Pair solve(int arr[], int n)
{
Pair ans;
if (n == 1)
{
ans.max = arr[0];
ans.min = arr[0];
return ans;
}

if (arr[0] > arr[1])
{
ans.max = arr[0];
ans.min = arr[1];
}
else
{
ans.max = arr[1];
ans.min = arr[0];
}

for (int i = 2; i < n; i++)
{
if (arr[i] > ans.max)
{
ans.max = arr[i];
}
else if (arr[i] < ans.min)
{
ans.min = arr[i];
}
}
return ans;
}

int main()
{
int n;
cin >> n;
int arr[n];
for (int i = 0; i < n; i++)
{
cin >> arr[i];
}
Pair ans = solve(arr, n);
cout << ans.min << " " << ans.max << endl;
}
Binary file added Array/maximum_product_subarray
Binary file not shown.
36 changes: 36 additions & 0 deletions Array/maximum_product_subarray.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
#include <bits/stdc++.h>
#define ll long long

using namespace std;

ll maxProduct(int arr[], int n)
{
ll max_so_far = LONG_LONG_MIN;
ll max_ending_here = 1;
for (int i = 0; i < n; i++)
{
max_ending_here *= arr[i];
if (max_ending_here > max_so_far)
{
max_so_far = max_ending_here;
}
if (max_ending_here < 0)
{
max_ending_here = abs(max_ending_here);
}
cout<<max_ending_here<<" ";
}
return max_so_far;
}

int main()
{
int n;
cin >> n;
int arr[n];
for (int i = 0; i < n; i++)
{
cin >> arr[i];
}
cout << maxProduct(arr, n) << endl;
}
Binary file added Array/negative_elements_to_one_side
Binary file not shown.
Loading

0 comments on commit 34f5420

Please sign in to comment.