diff --git a/Coding Ninjas/Greedy Problems/Activity_Selection.cpp b/Coding Ninjas/Greedy Problems/Activity_Selection.cpp new file mode 100644 index 0000000..74e26c6 --- /dev/null +++ b/Coding Ninjas/Greedy Problems/Activity_Selection.cpp @@ -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 +#include +#include +using namespace std; +bool compare(pair i1, pair i2) +{ + return i1.second*arr, int n) +{ + int current_ending_time=arr[0].second; + int count=1; + for(int i=1; i>n; + pair *arr=new pair[n]; + for(int i=0; i>arr[i].first>>arr[i].second; + } + sort(arr, arr+n, compare); + cout< +#include +#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.xiw2.yi; + } + return w1.ti=d) + { + return current_cost; + } + if(arr[current_worker_index].yi>n>>d; + worker*arr=new worker[n]; + for(ll i=0; i>arr[i].ti>>arr[i].xi>>arr[i].yi; + } + sort(arr, arr+n, sorter); + cout< +#include +#include +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 +#include +#include +#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; + ll *arr=new ll [n]; + for(ll i=0; i>arr[i]; + } + sort(arr, arr+n); + cout< +#include +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<>n; + int *arr=new int [n]; + for(int i=0; i>arr[i]; + } + sort(arr, arr+n); + printer(arr, n); +} \ No newline at end of file diff --git a/Coding Ninjas/Greedy Problems/Problem_discussion.cpp b/Coding Ninjas/Greedy Problems/Problem_discussion.cpp new file mode 100644 index 0000000..f10a47d --- /dev/null +++ b/Coding Ninjas/Greedy Problems/Problem_discussion.cpp @@ -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 +#include +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=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>arr[i]; + } + cout< +#include +using namespace std; +// A job has start time, finish time and profit. +struct Job +{ + int start, finish, profit; +}; +// A utility function that is used for sorting events +// according to finish time +bool myfunction(Job s1, Job s2) +{ + return (s1.finish < s2.finish); +} +// A Binary Search based function to find the latest job +// (before current job) that doesn't conflict with current +// job. "index" is index of the current job. This function +// returns -1 if all jobs before index conflict with it. +// The array jobs[] is sorted in increasing order of finish +// time. +int binarySearch(Job jobs[], int index) +{ +// Initialize 'lo' and 'hi' for Binary Search + int lo = 0, hi = index - 1; +// Perform binary Search iteratively + while (lo <= hi) + { + int mid = (lo + hi) / 2; + if (jobs[mid].finish <= jobs[index].start) + { + if (jobs[mid + 1].finish <= jobs[index].start) + lo = mid + 1; + else + return mid; + } + else + hi = mid - 1; + } + return -1; +} +// The main function that returns the maximum possible +// profit from given array of jobs +int findMaxProfit(Job arr[], int n) +{ + // Sort jobs according to finish time + sort(arr, arr+n, myfunction); + // Create an array to store solutions of subproblems. table[i] + // stores the profit for jobs till arr[i] (including arr[i]) + int *table = new int[n]; + table[0] = arr[0].profit; + // Fill entries in table[] using recursive property + for (int i=1; i>n; + Job arr[n]; + for(int i=0;i>arr[i].start>>arr[i].finish>>arr[i].profit; + } + cout << findMaxProfit(arr, n); + return 0; +} \ No newline at end of file diff --git a/Coding Ninjas/Greedy Problems/Winning_Lottery.cpp b/Coding Ninjas/Greedy Problems/Winning_Lottery.cpp new file mode 100644 index 0000000..b0811ca --- /dev/null +++ b/Coding Ninjas/Greedy Problems/Winning_Lottery.cpp @@ -0,0 +1,60 @@ +/* Harshit knows by his resources that this time the winning lottery number is the smallest number whose sum of the digits is S and the number of digits is D. You have to help Harshit and print the winning lottery number. +Input Format +The Input line contains two space-separated integers: S,D +Output Format +The output contains a single integer denoting the winning lottery number +Constraints +1 <= D <= 1000 +1 <= S <= 9*D +Time Limit: 1 second +Sample Input1: +9 2 +Sample Output1: +18 +Explanation +There are many other possible numbers like 45, 54, 90, etc with the sum of digits as 9 and number of digits as 2. The smallest of them is 18. */ + + + +#include +using namespace std; +void lottery(int s, int n) +{ + int* arr = new int[n]; + for (int i = 0; i < n; i++) + { + arr[i] = 0; + } + int sum = s; + arr[0] = 1; + sum -= 1; + int i = n - 1; + while (i >= 0) + { + if (i == 0) + { + arr[i] += sum; + } + if (sum > 9) + { + arr[i] = 9; + sum -= 9; + } + else if (sum <= 9) + { + arr[i] += sum; + sum = 0; + } + i--; + } + for (int i = 0; i < n; i++) + { + cout << arr[i]; + } +} +int main() +{ + int s, d; + cin >> s >> d; + lottery(s, d); +} \ No newline at end of file