Skip to content
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
58 changes: 58 additions & 0 deletions Coding Ninjas/Greedy Problems/Activity_Selection.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
/* You are given n activities with their start and finish times. Select the maximum number of activities that can be performed by a single person, assuming that a person can only work on a single activity at a time.
Input
The first line of input contains one integer denoting N.
Next N lines contains two space separated integers denoting the start time and finish time for the ith activity.

Output
Output one integer, the maximum number of activities that can be performed
Constraints
1 ≤ N ≤ 10^6
1 ≤ ai, di ≤ 10^9
Sample Input
6
1 2
3 4
0 6
5 7
8 9
5 9
Sample Output
4 */




#include<iostream>
#include<utility>
#include<algorithm>
using namespace std;
bool compare(pair<int, int> i1, pair<int, int> i2)
{
return i1.second<i2.second;
}
int activities(pair<int, int>*arr, int n)
{
int current_ending_time=arr[0].second;
int count=1;
for(int i=1; i<n; i++)
{
if(current_ending_time<=arr[i].first)
{
count+=1;
current_ending_time=arr[i].second;
}
}
return count;
}
int main()
{
int n;
cin>>n;
pair<int, int> *arr=new pair<int, int>[n];
for(int i=0; i<n; i++)
{
cin>>arr[i].first>>arr[i].second;
}
sort(arr, arr+n, compare);
cout<<activities(arr, n)<<endl;
}
74 changes: 74 additions & 0 deletions Coding Ninjas/Greedy Problems/Fractional_Knapsack.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
/* You want to paint your house. The total area of your house is D units. There are a total of N workers. The ith worker is available after time Ti, has hiring cost Xi and speed Yi. This means he becomes available for hiring from time Ti and remains available after that. Once available, you can hire him with cost Xi, after which he will start painting the house immediately, covering exactly Yi units of house with paint per time unit. You may or may not hire a worker and can also hire or fire him at any later point of time. However, no more than 1 worker can be painting the house at a given time.
Since you want the work to be done as fast as possible, figure out a way to hire the workers, such that your house gets painted at the earliest possible time, with minimum cost to spend for hiring workers.
Note: You can hire a previously hired worker without paying him again.
Input
The first line of input contains two integers "N D", the number of workers and the area of your house respectively. The ith of the next N lines denotes the ith worker, and contains three integers "Ti Xi Yi", described in the statement.
Output
Output one integer, the minimum cost that you can spend in order to get your house painted at the earliest.
Constraints
1 ≤ N, T, X, Y ≤ 10^5
1 ≤ D ≤ 10^11
Sample Input
3 3
1 1 1
2 2 2
3 1 5
Sample Output
3 */



#include<iostream>
#include<algorithm>
#define ll long long int
using namespace std;
struct worker
{
ll ti;
ll xi;
ll yi;
};
bool sorter(worker w1, worker w2)
{
if(w1.ti==w2.ti)
{
if(w1.yi==w2.yi)
{
return w1.xi<w2.xi;
}
return w1.yi>w2.yi;
}
return w1.ti<w2.ti;
}
ll minimum_cost(worker *arr, ll n, ll d)
{
ll current_cost=arr[0].xi;
ll current_area_covered=0;
ll current_worker_index=0;
for(ll i=1; i<n&& current_area_covered<d; i++)
{
current_area_covered+=(arr[i].ti-arr[i-1].ti)*arr[current_worker_index].yi;
if(current_area_covered>=d)
{
return current_cost;
}
if(arr[current_worker_index].yi<arr[i].yi)
{
current_worker_index=i;
current_cost+=arr[current_worker_index].xi;
}
}
return current_cost;
}
int main()
{
ll n, d;// d is the area to be painted;
cin>>n>>d;
worker*arr=new worker[n];
for(ll i=0; i<n; i++)
{
cin>>arr[i].ti>>arr[i].xi>>arr[i].yi;
}
sort(arr, arr+n, sorter);
cout<<minimum_cost(arr, n, d)<<endl;
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
/* Given an integer array A of size N, find and return the minimum absolute difference between any two elements in the array.
We define the absolute difference between two elements ai, and aj (where i != j ) is |ai - aj|.
Input format :
Line 1 : Integer N, Array Size
Line 2 : Array elements (separated by space)
Output Format :
Minimum difference
Constraints :
1 <= N <= 10^6
Sample Input :
5
2 9 0 4 5
Sample Input :
1 */




// arr - input array
// n - size of array
#include<iostream>
#include<algorithm>
#include<climits>
using namespace std;
int mod(int a)
{
if(a<0)
{
return -a;
}
return a;
}
int minAbsoluteDiff(int arr[], int n) {
/* Don't write main().
* Don't read input, it is passed as function argument.
* Return output and don't print it.
* Taking input and printing output is handled automatically.
*/
sort(arr, arr+n);
int i=0;
int j=1;
int minimum=INT_MAX;
while(j<n)
{
if(mod(arr[i]-arr[j])<minimum)
{
minimum=mod(arr[i]-arr[j]);
}
i++;
j++;
}
return minimum;
}
44 changes: 44 additions & 0 deletions Coding Ninjas/Greedy Problems/Nikunj_And_Donuts.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
/* Nikunj loves donuts, but he also likes to stay fit. He eats n donuts in one sitting, and each donut has a calorie count, ci. After eating a donut with k calories, he must walk at least 2^j x k(where j is the number donuts he has already eaten) miles to maintain his weight.
Given the individual calorie counts for each of the n donuts, find and print a long integer denoting the minimum number of miles Nikunj must walk to maintain his weight. Note that he can eat the donuts in any order.
Input
The first line contains an integer, n, denoting the number of donuts.
The second line contains n space-separated integers describing the respective calorie counts of each donut I, i.e ci.
Output
Print a long integer denoting the minimum number of miles Nikunj must walk to maintain his weight.
Constraints
1 ≤ n ≤ 40
1 ≤ ci ≤ 1000
Sample Input
3
1 3 2
Sample Output
11 */



#include<iostream>
#include<math.h>
#include<algorithm>
#define ll unsigned long long int
using namespace std;
ll minimum_miles(ll *arr, ll n)
{
ll total_miles=0;
for(ll i=0; i<n; i++)
{
total_miles+=pow(2, i)*arr[n-1-i];
}
return total_miles;
}
int main()
{
ll n;
cin>>n;
ll *arr=new ll [n];
for(ll i=0; i<n; i++)
{
cin>>arr[i];
}
sort(arr, arr+n);
cout<<minimum_miles(arr, n)<<endl;
}
63 changes: 63 additions & 0 deletions Coding Ninjas/Greedy Problems/Perimeter_With_Conditions.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
/* Aahad gives an array of integers and asks Harshit to find which three elements form a triangle (non-degenerate). The task seems easy to Harshit.
So, Aahad adds some conditions to this task -
1. Find the triangle with maximum perimeter
2. If there are two or more combinations with same value of maximum perimeter, then find the one with the longest side.
3.If there are more than one combinations which satisfy all the above conditions the find with maximum longest minimum side.
Input Format
The First line contains no of elements of array: N
Each T lines contains N space-separated integers: A [i]
Output Format
The output contains three space-separated elements that denote the length of the sides of triangle. If no such triangle is possible, then print -1.
Constraints
1 =< N <= 10^5
1 <= A[i] <= 10^9
Time Limit: 1 second
Sample Input1:
5
1 1 1 3 3
Sample Output1:
1 3 3
Sample Input2:
3
2 2 4
Sample Output3:
-1
Explaination
In the First Sample case, the elements that form a triangle with maximum perimeter is 1,3,3.
In the Second Sample case, the elements that can form a triangle are degenerate, so, we printed -1. */



#include<iostream>
#include<algorithm>
using namespace std;
void printer(int *arr, int n)
{
int start=n-3;
int mid=n-2;
int end=n-1;
while(start+mid<=end)
{
start--;
mid--;
end--;
if(start<0)
{
cout<<-1<<endl;
return;
}
}
cout<<arr[start]<<" "<<arr[mid]<<" "<<arr[end];
}
int main()
{
int n;
cin>>n;
int *arr=new int [n];
for(int i=0; i<n; i++)
{
cin>>arr[i];
}
sort(arr, arr+n);
printer(arr, n);
}
70 changes: 70 additions & 0 deletions Coding Ninjas/Greedy Problems/Problem_discussion.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
/* Harshit gave Aahad an array of size N and asked to minimize the difference between the maximum value and minimum value by modifying the array under the condition that each array element either increase or decrease by k(only once).
It seems difficult for Aahad so he asked for your help
Input Format
The First line contains two space-separated integers: N,K
Next lines contain N space-separated integers denoting elements of the array
Output Format
The output contains a single integer denoting the minimum difference between maximum value and the minimum value in the array
Constraints
1 =< N <= 10^5
1 <= Ai,K <= 10^9
Sample Input1:
3 6
1 15 10
Sample Output1:
5
Explaination
We change from 1 to 6, 15 to 9 and 10 to 4. Maximum difference is 5 (between 4 and 9). We can't get a lower difference. */




#include<iostream>
#include<algorithm>
using namespace std;
int get_min_difference(int *arr, int n, int k)
{
if(n==1)
{
return 0;
}
sort(arr, arr+n);
int ans=arr[n-1]-arr[0];
int small=arr[0]+k;
int big=arr[n-1]-k;
if(small>big)
{
int temp=big;
big=small;
small=temp;
}
for(int i=1; i<n-1; i++)
{
int subtract=arr[i]-k;
int add=arr[i]+k;
if(subtract>=small || add<=big)
{
continue;
}
if(big-subtract<=add-small)
{
small=subtract;
}
else
{
big=add;
}
}
return min(ans, big-small);
}
int main()
{
int n, k;
cin>>n>>k;
int *arr=new int [n];
for(int i=0; i<n; i++)
{
cin>>arr[i];
}
cout<<get_min_difference(arr, n, k)<<endl;
}
Loading