From e07698cbc1b4d5d3c1b4df790550453624f97add Mon Sep 17 00:00:00 2001 From: CruiseDevice Date: Wed, 3 Oct 2018 01:29:34 +0530 Subject: [PATCH 1/2] some of the solutions for algorithms problem from hackerearth --- Algorithms/Graph/edge_existence.cpp | 39 ++++++++++++++++ .../Being_greedy_for_water.cpp | 35 +++++++++++++++ .../Searching/Ternary Search/finding_mino.cpp | 44 +++++++++++++++++++ 3 files changed, 118 insertions(+) create mode 100644 Algorithms/Graph/edge_existence.cpp create mode 100644 Algorithms/Greedy Algorithms/Being_greedy_for_water.cpp create mode 100644 Algorithms/Searching/Ternary Search/finding_mino.cpp diff --git a/Algorithms/Graph/edge_existence.cpp b/Algorithms/Graph/edge_existence.cpp new file mode 100644 index 0000000..33d3651 --- /dev/null +++ b/Algorithms/Graph/edge_existence.cpp @@ -0,0 +1,39 @@ +/* + You have been given an undirected graph consisting of N nodes and M + edges. This graph can consist of self-loops as well as multiple edges. + In addition , you have also been given Q queries. For each query , you + shall be given 2 integers A and B. You just need to find if there exists + an edge between node A and node B. If yes, print "YES" (without quotes) + else , print "NO"(without quotes). + +*/ +#include +using namespace std; +int arr[1000][1000]; +void initialize(){ + for(int i = 0; i < 1000; i++){ + for(int j = 0; j < 1000; j++){ + arr[i][j] = false; + } + } +} +int main(){ + + int x,y,n,m,q; + initialize(); + cin >> n >> m; + for(int i = 0 ; i < m; i++){ + cin >> x >> y; + arr[x][y] = true; + } + cin >> q; + for(int i = 0; i < q; i ++){ + cin >> x >> y; + if(arr[x][y]== true){ + cout << "YES"<<"\n"; + }else{ + cout << "NO"<<"\n"; + } + } + return 0; +} \ No newline at end of file diff --git a/Algorithms/Greedy Algorithms/Being_greedy_for_water.cpp b/Algorithms/Greedy Algorithms/Being_greedy_for_water.cpp new file mode 100644 index 0000000..6e89533 --- /dev/null +++ b/Algorithms/Greedy Algorithms/Being_greedy_for_water.cpp @@ -0,0 +1,35 @@ +/* + You are given container full of water. Container can have limited amount + of water. You also have N bottles to fill. You need to find the maximum + numbers of bottles you can fill. +*/ +#include +#include +using namespace std; +const int MAX = 100000; +int A[MAX]; +int main(){ + int T; + cin >> T; + while(T--){ + + int N,Capacity; + cin >> N >> Capacity; + int numberBottles = 0, numberOfBottles = 0; + for(int i = 0; i < N; i++){ + cin >> A[i]; + } + sort(A,A+N); + for(int i = 0; i < N; i++){ + numberBottles += A[i]; + if(numberBottles > Capacity){ + break; + }else{ + numberOfBottles++; + } + } + cout << numberOfBottles << "\n"; + + } + return 0; +} \ No newline at end of file diff --git a/Algorithms/Searching/Ternary Search/finding_mino.cpp b/Algorithms/Searching/Ternary Search/finding_mino.cpp new file mode 100644 index 0000000..e77a5b8 --- /dev/null +++ b/Algorithms/Searching/Ternary Search/finding_mino.cpp @@ -0,0 +1,44 @@ +#include +#include +#include +using namespace std; +int f(int x){ return 2*x*x -12*x + 7;} +int ternary_search(int l, int r) +{ + while(r-l>=3) + { + int mid1 = l + (r-l)/3; + int mid2 = r - (r-l)/3; + if(f(mid1) < f(mid2)) + r = mid2; + else + l = mid1; + } + if(l==r) + return f(l); + else if (l+1==r) + return min(f(l), f(r)); + else + return min(f(l), min(f(l+1), f(l+2))); +} +int main(int argc, char *argv[]) +{ + //freopen(argv[1], "r", stdin); + //freopen(argv[2], "w", stdout); + int n; + scanf("%d",&n); + assert(n>=1 and n<=1000); + int l,r; + /*for(int i=-3;i<12;i++) + { + cout<=-1000 and l<=1000); + assert(r>=-1000 and l<=1000); + printf("%d\n",ternary_search(l,r)); + } + return 0; +} \ No newline at end of file From ed01e8b7ca7ea4ec04cb4f10b19cb2cca958172f Mon Sep 17 00:00:00 2001 From: CruiseDevice Date: Thu, 4 Oct 2018 00:53:52 +0530 Subject: [PATCH 2/2] Create a new dir for c++ --- Algorithms/{ => C++}/Graph/edge_existence.cpp | 0 .../Being_greedy_for_water.cpp | 0 .../Searching/Ternary Search/finding_mino.cpp | 44 ------------------- 3 files changed, 44 deletions(-) rename Algorithms/{ => C++}/Graph/edge_existence.cpp (100%) rename Algorithms/{ => C++}/Greedy Algorithms/Being_greedy_for_water.cpp (100%) delete mode 100644 Algorithms/Searching/Ternary Search/finding_mino.cpp diff --git a/Algorithms/Graph/edge_existence.cpp b/Algorithms/C++/Graph/edge_existence.cpp similarity index 100% rename from Algorithms/Graph/edge_existence.cpp rename to Algorithms/C++/Graph/edge_existence.cpp diff --git a/Algorithms/Greedy Algorithms/Being_greedy_for_water.cpp b/Algorithms/C++/Greedy Algorithms/Being_greedy_for_water.cpp similarity index 100% rename from Algorithms/Greedy Algorithms/Being_greedy_for_water.cpp rename to Algorithms/C++/Greedy Algorithms/Being_greedy_for_water.cpp diff --git a/Algorithms/Searching/Ternary Search/finding_mino.cpp b/Algorithms/Searching/Ternary Search/finding_mino.cpp deleted file mode 100644 index e77a5b8..0000000 --- a/Algorithms/Searching/Ternary Search/finding_mino.cpp +++ /dev/null @@ -1,44 +0,0 @@ -#include -#include -#include -using namespace std; -int f(int x){ return 2*x*x -12*x + 7;} -int ternary_search(int l, int r) -{ - while(r-l>=3) - { - int mid1 = l + (r-l)/3; - int mid2 = r - (r-l)/3; - if(f(mid1) < f(mid2)) - r = mid2; - else - l = mid1; - } - if(l==r) - return f(l); - else if (l+1==r) - return min(f(l), f(r)); - else - return min(f(l), min(f(l+1), f(l+2))); -} -int main(int argc, char *argv[]) -{ - //freopen(argv[1], "r", stdin); - //freopen(argv[2], "w", stdout); - int n; - scanf("%d",&n); - assert(n>=1 and n<=1000); - int l,r; - /*for(int i=-3;i<12;i++) - { - cout<=-1000 and l<=1000); - assert(r>=-1000 and l<=1000); - printf("%d\n",ternary_search(l,r)); - } - return 0; -} \ No newline at end of file