Greedy
Greedy
Activity Selection
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.
Note : The start time and end time of two activities may coincide.
Input:
The first line contains T denoting the number of testcases. Then follows description of
testcases. First line is N number of activities then second line contains N numbers which
are starting time of activies.Third line contains N finishing time of activities.
Output:
For each test case, output a single number denoting maximum activites which can be performed
in new line.
Constraints:
1<=T<=50
1<=N<=1000
1<=A[i]<=100
Example:
Input:
2
6
132585
246799
4
1325
2436
Output:
4
4
CODE:
if(p1.second<p2.second)
return true;
if(p1.second==p2.second)
if(p1.first<p2.first)
return true;
return false;
int main()
int t;
cin>>t;
while(t--)
int n;
cin>>n;
int str[n];
int end[n];
vector<pair<int,int>>v;
for(int i=0;i<n;i++)
cin>>str[i];
for(int i=0;i<n;i++)
cin>>end[i];
for(int i=0;i<n;i++)
v.push_back(make_pair(str[i],end[i]));
sort(v.begin(),v.end(),comparator);
int cnt=1;
int startt=v[0].first;
int endt=v[0].second;
for(int i=0;i<n;i++)
if(endt<=v[i].first)
endt=v[i].second;
cnt++;
}
}
cout<<cnt<<endl;
return 0;
CODE:
if(p1.second<p2.second)
return true;
if(p1.second==p2.second)
if(p1.first<p2.first)
return true;
return false;
int main()
int t;
cin>>t;
while(t--)
int n;
cin>>n;
int s[n];
int e[n];
for(int i=0;i<n;i++)
cin>>s[i];
for(int i=0;i<n;i++)
cin>>e[i];
vector<pair<int,int>> v;
map<pair<int,int>,int> mp;
for(int i=0;i<n;i++)
v.push_back(make_pair(s[i],e[i]));
mp[v[i]]=i;
sort(v.begin(),v.end(),comparator);
int start=v[0].first;
int end=v[0].second;
//debug
// for(int i=0;i<n;i++)
// {
// }
// cout<<"-------------------------------------------------"<<endl;
cout<<mp[v[0]]+1<<" ";
for(int i=1;i<n;i++)
// cout<<v[i].first<<" - "<<v[i].second<<endl;
if(end<=v[i].first)
cout<<mp[v[i]]+1<<" ";
end=v[i].second;
cout<<endl;
return 0;
}
3. Maximize Toys
Given an array arr of length N consisting cost of toys. Given an integer K depicting the amount
with you. The task is to Maximise the number of different toys you can have with K amount.
Example 1:
Input: N = 7, K = 50
arr = {1, 12, 5, 111, 200, 1000, 10}
Output: 4
Explaination: The costs of the toys are
1, 12, 5, 10.
Example 2:
Input: N = 3, K = 100
arr = {20, 30, 50}
Output: 3
Explaination: We can buy all types of
toys.
Your Task:
You do not need to read input or print anything. Your task is to complete the
function toyCount() which takes the value N, K and the array arr and returns the maximum
count of toys.
Expected Time Complexity: O(NlogN)
Expected Auxiliary Space: O(1)
Constraints:
1 ≤ N ≤ 1000
1 ≤ K, arr[i] ≤ 10000
CODE:
#include <bits/stdc++.h>
class Solution{
public:
sort(arr,arr+n);
int cnt=0;
int i=0;
k=k-arr[i];
if(k>=0)
cnt++;
i++;
return cnt;
// code here
};
// { Driver Code Starts.
int main(){
int t;
cin>>t;
while(t--){
int N, K;
cin>>N>>K;
int arr[N];
cin>>arr[i];
Solution ob;
cout<<ob.toyCount(N, K, arr)<<endl;
return 0;
CODE:
int main()
int t;
cin>>t;
while(t--)
int n;
cin>>n;
int arr[n];
for(int i=0;i<n;i++)
cin>>arr[i];
int k;
cin>>k;
map<int,int> tv;
map<int,int> vt;
int t=0;
int cnt=0;
for(int i=0;i<n;i++)
if(vt.find(arr[i])==vt.end())
vt[arr[i]]=t;
tv[t]=arr[i];
t++;
cnt++;
int x=tv.begin()->second;
tv.erase(tv.begin());
vt.erase(x);
}
else
tv.erase(vt[arr[i]]);
vt[arr[i]]=t;
tv[t]=arr[i];
t++;
cout<<cnt<<endl;
return 0;
Input: N = 2, S = 9
Output: 90
Explaination: It is the biggest number
with sum of digits equals to 9.
Example 2:
Input: N = 3, S = 20
Output: 992
Explaination: It is the biggest number
with sum of digits equals to 20.
Your Task:
You do not need to read input or print anything. Your task is to complete the
function findLargest() which takes N and S as input parameters and returns the
largest possible number. Return -1 if no such number is possible.
Expected Time Complexity: O(N)
Exepcted Auxiliary Space: O(N)
Constraints:
1 ≤ N ≤ 104
1 ≤ S ≤ 105
CODE:
int main()
int t;
cin>>t;
while(t--)
int n,s;
cin>>n>>s;
if(s==0)
{
cout<<-1<<endl;
continue;
vector<int> v;
if(s>9)
s=s-9;
v.push_back(9);
else
v.push_back(s);
s=0;
n--;
if(s!=0)
cout<<-1;
else
for(int i=0;i<v.size();i++)
cout<<v[i];
}
cout<<endl;
return 0;
Input:
K = 2, N = 4
Arr[] = {1, 5, 8, 10}
Output: 5
Explanation: The array can be modified as
{3, 3, 6, 8}. The difference between
the largest and the smallest is 8-3 = 5.
Example 2:
Input:
K = 3, N = 5
Arr[] = {3, 9, 12, 16, 20}
Output: 11
Explanation: The array can be modified as
{6 12 9 13 17}. The difference between
the largest and the smallest is 17-6 = 11.
Your Task:
You don't need to read input or print anything. Your task is to complete the
function getMinDiff() which takes the arr[], n and k as input parameters and returns an
integer denoting the minimum difference.
Constraints
1 <= K <= 104
1 <= N <= 105
1 <= Arr[i] <= 105
CODE:
#include <bits/stdc++.h>
class Solution{
public:
sort(arr,arr+n);
int diff=arr[n-1]-arr[0];
int small=arr[0]+k;
int big=arr[n-1]-k;
if(small>big)
swap(small,big);
for(int i=1;i<n-1;i++)
int sub=arr[i]-k;
int add=arr[i]+k;
if(sub>=small || add<=big)
continue;
if(big-sub<=add-small)
small=sub;
else
big=add;
return min(diff,big-small);
// code here
};
int main() {
int t;
cin >> t;
while (t--) {
int n, k;
cin >> k;
cin >> n;
int arr[n];
Solution ob;
return 0;
CODE:
int main()
ll t;
cin>>t;
while(t--)
ll n;
cin>>n;
ll arr1[n];
ll arr2[n];
for(ll i=0;i<n;i++)
cin>>arr1[i];
for(ll i=0;i<n;i++)
cin>>arr2[i];
sort(arr1,arr1+n);
sort(arr2,arr2+n);
ll sum=0;
for(ll i=0;i<n;i++)
sum+=arr1[i]*arr2[n-i-1];
cout<<sum<<endl;
return 0;
8. Huffman Decoding-1
he task is to implement Huffman Encoding and Decoding.
Input:
First line consists of T test cases. Only line of every test case consists of String S.
Output:
Single line output, return the Decoded String.
Constraints:
1<=T<=100
2<=S<=1000
Example:
Input:
2
abc
geeksforgeeks
Output:
abc
geeksforgeeks
CODE:
#include <bits/stdc++.h>
struct MinHeapNode
char data;
int freq;
this->freq = freq;
};
struct compare
};
if (!root)
return;
if (root->data != '$')
cout << root->data << ": " << str << "\n";
if (root==NULL)
return;
if (root->data != '$')
codes[root->data]=str;
while (minHeap.size() != 1)
left = minHeap.top();
minHeap.pop();
right = minHeap.top();
minHeap.pop();
top->left = left;
top->right = right;
minHeap.push(top);
storeCodes(minHeap.top(), "");
freq[str[i]]++;
int t;
cin>>t;
while(t--){
codes.clear();
freq.clear();
string str;
cin>>str;
calcFreq(str, str.length());
HuffmanCodes(str.length());
encodedString+=codes[i];
return 0;
2) Encoded String*/
if (s[i] == '0')
curr = curr->left;
else
curr = curr->right;
ans += curr->data;
curr = root;
// cout<<ans<<endl;
return ans+'\0';
CODE:
#include <bits/stdc++.h>
// Driver code
int main() {
int t;
cin >> t;
while (t--) {
int V, E;
int i=0;
while (i++<E) {
int u, v, w;
u--, v--;
graph[u][v] = w;
graph[v][u] = w;
}
return 0;
for(int i=0;i<V;i++)
for(int j=0;j<V;j++)
if(g[i][j]!=0)
adj[i].push_back({j,g[i][j]});
int value[V];
vector<bool> vis(V,false);
value[0]=0;
int res=0;
for(int i=0;i<V;i++)
{
int u=-1;
int mi=INT_MAX;
for(int i=0;i<V;i++)
if(!vis[i])
if(mi>value[i]||u==-1)
mi=value[i];
u=i;
vis[u]=true;
res+=value[u];
for(auto v:adj[u])
if(!vis[v.first])
value[v.first]=min(value[v.first],v.second);
return res;
Input
The first line of the input contains T the number of test cases. Each test case consists
of two lines. The first line of each test case contains the values of N and K as
described above. Then in the next line N integers follow denoting the price of each of
the N different candies.
Output
For each test case output a single line containing 2 space separated integers , the first
denoting the minimum amount of money required to be spent and the second
denoting the maximum amount of money to be spent.
Remember to output the answer of each test case in a new line.
Constraints
1 <= T <= 50
1 <= N <= 1000
0 <= K <= N-1
1 <= Ai <= 100
int main()
int t;
cin>>t;
while(t--)
int n,k;
cin>>n>>k;
int arr[n];
for(int i=0;i<n;i++)
cin>>arr[i];
sort(arr,arr+n);
int r;
if(n%(k+1)==0)
r=n/(k+1);
else
{
r=(n/(k+1))+1;
// cout<<"r="<<r<<endl;
int ans1=0;
int ans2=0;
for(int i=0;i<n;i++)
if(i<r)
ans1+=arr[i];
if(i>=(n-r))
ans2+=arr[i];
cout<<ans1<<" "<<ans2<<endl;
return 0;
CODE:
int main()
int t;
cin>>t;
while(t--)
{
int n,m;
cin>>n>>m;
int arr1[n];
int arr2[m];
for(int i=0;i<n;i++)
cin>>arr1[i];
for(int i=0;i<m;i++)
cin>>arr2[i];
int sum1=0;
int sum2=0;
int ans=0;
int i=0,j=0;
if(arr1[i]==arr2[j])
int p=arr1[i];
sum1+=arr1[i];
i++;
}
sum2+=arr2[j];
j++;
ans+=max(sum1,sum2);
sum1=0;
sum2=0;
else if(arr1[i]<arr2[j])
sum1+=arr1[i];
i++;
else
sum2+=arr2[j];
j++;
}
while(i<n)
sum1+=arr1[i];
i++;
while(j<m)
sum2+=arr2[j];
j++;
ans+=max(sum1,sum2);
cout<<ans<<endl;
return 0;