Skip to content

Commit acb2284

Browse files
committed
Day-50 Greedy problem + sort problem2
1 parent f2dce09 commit acb2284

File tree

3 files changed

+101
-3
lines changed

3 files changed

+101
-3
lines changed

README.md

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,9 +4,9 @@
44

55
| Current Status| Stats |
66
| :------------: | :----------: |
7-
| Total Problems | 68 |
8-
| Current Streak | 49 |
9-
| Longest Streak | 49 ( August 17, 2015 - October 4, 2015 ) |
7+
| Total Problems | 70 |
8+
| Current Streak | 50 |
9+
| Longest Streak | 50 ( August 17, 2015 - October 5, 2015 ) |
1010

1111
</center>
1212

@@ -121,10 +121,16 @@ Include contains single header implementation of data structures and some algori
121121
| Problem | Solution |
122122
| :------------ | :----------: |
123123
| Given a sorted vector, return first index of the occurrence of a value in vector, if number does not exist, return -1 | [first_occurrence_binary_search.cpp](sort_search_problems/first_occurrence_binary_search.cpp) |
124+
| Given a list of unsorted integers, A={a<sub>1</sub>,a<sub>2</sub>,…,a<sub>N</sub>}, Find the pair of elements that have the smallest absolute difference between them? If there are multiple pairs, find them all.| [closest_numbers.cpp](sort_search_problems/closest_numbers.cpp)|
124125

125126
### Graph Problems
126127
| Problem | Solution |
127128
| :------------ | :----------: |
128129
| Depth First Traversal of a Graph | [dfsDemo.cpp](graph_problems/dfsDemo.cpp) |
129130
| Breadth First Traversal of a Graph | [bfsDemo.cpp](graph_problems/bfsDemo.cpp) |
130131
| calculate the shortest distance from the start position (Node S) to all of the other nodes in the graph using Dijkstra algorithm. | [dijkstra-shortest-reach.cpp](graph_problems/dijkstra-shortest-reach.cpp)|
132+
133+
###Greedy Problems
134+
| Problem | Solution |
135+
| :------------ | :----------: |
136+
| Given two integer arrays, A and B, each containing N integers. You are free to permute the order of the elements in the arrays.Is there an permutation A', B' possible of A and B, such that, A'<sub>i</sub>+B'<sub>i</sub> ≥ K for all i, where A'<sub>i<sub> denotes the i<sup>th<sup> element in the array A' and B'<sub>i</sub> denotes i<sup>th</sup> element in the array B'.| [two_arrays.cpp](greedy_problems/two_arrays.cpp)|

greedy_problems/two_arrays.cpp

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
/**
2+
* Given two array A and B
3+
* Is there an permutation A', B' possible of A and B, such that, A'i+B'i ≥ K for all i,
4+
* where A'i denotes the ith element in the array A' and B'i denotes ith element in the array B'.
5+
*
6+
* Input: The first line contains two integers, N and K.
7+
* The second line contains N space separated integers, denoting array A.
8+
* The third line describes array B in a same format.
9+
*
10+
* Output: YES or NO
11+
*
12+
* Solution Approach :
13+
* Greedy Algorithm.
14+
* sort one of the arrays in ascending order and the other in descending order and
15+
* then for every i, check if the condition (A[i] + B[i] >= k ) holds true or not
16+
* for each of the array indices i. It can be deduced that if the condition fails on the sorted arrays,
17+
* then there exists no permutation of A and B such that the condition holds good.
18+
*/
19+
#include <vector>
20+
#include <iostream>
21+
#include <algorithm>
22+
using namespace std;
23+
24+
25+
int main() {
26+
int N, K;
27+
cin >> N >> K;
28+
int * A = new int[N];
29+
int * B = new int[N];
30+
for ( int i = 0; i < N; ++i) {
31+
cin >> A[i];
32+
}
33+
for(int i = 0; i < N; ++i) {
34+
cin >> B[i];
35+
}
36+
sort(A, A + N);
37+
sort(B, B + N, std::greater<int>()); //decreasing order sort
38+
int j;
39+
for( j = 0; j < N; ++j ) {
40+
if (A[j] + B[j] < K) {
41+
std::cout << "NO" << std::endl;
42+
break;
43+
}
44+
}
45+
if ( j == N ) {
46+
std::cout << "YES" << std::endl;
47+
}
48+
return 0;
49+
}
50+

sort_search_problems/closest_nums.cpp

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
/*
2+
Given a list of unsorted integers, A={a1,a2,…,aN}, Find the pair of elements that have the smallest absolute difference between them? If there are multiple pairs, find them all.
3+
Input: N= Number of elements, followed by N elements.
4+
Output: Output the pairs of elements with the smallest difference. If there are multiple pairs, output all of them in ascending order, all on the same line (consecutively) with just a single space between each pair of numbers. If there's a number which lies in two pair, print it two times
5+
Example:
6+
Input : 4
7+
5 4 3 2
8+
Output: 2 3 3 4 4 5
9+
*/
10+
11+
#include <iostream>
12+
#include <vector>
13+
#include <algorithm>
14+
#include <climits>
15+
using namespace std;
16+
17+
18+
int main() {
19+
int N;
20+
vector<int> vec;
21+
cin >> N;
22+
int x;
23+
for ( int i = 0; i < N; ++i) {
24+
cin >> x;
25+
vec.push_back(x);
26+
}
27+
sort(vec.begin(), vec.end());
28+
int mindiff = INT_MAX;
29+
for( size_t i = 0; i < vec.size()-1; ++i){
30+
if (vec[i+1]-vec[i] < mindiff) {
31+
mindiff = vec[i+1]-vec[i];
32+
}
33+
}
34+
for( size_t i =0; i < vec.size()-1; ++i ) {
35+
if (vec[i+1]-vec[i] == mindiff) {
36+
std::cout << vec[i] << " " << vec[i+1] << " ";
37+
}
38+
}
39+
std::cout << std::endl;
40+
return 0;
41+
}
42+

0 commit comments

Comments
 (0)