Notes![what is notes.io? What is notes.io?](/theme/images/whatisnotesio.png)
![]() ![]() Notes - notes.io |
#include <iostream>
#include <cstring>
using namespace std;
int knapsack(int W, int wt[], int val[], int n)
{
int dp[n+1][W+1];
memset(dp, 0, sizeof(dp));
for(int i=1; i<=n; i++)
{
for(int j=1; j<=W; j++)
{
if(wt[i-1] <= j)
dp[i][j] = max(val[i-1] + dp[i-1][j-wt[i-1]], dp[i-1][j]);
else
dp[i][j] = dp[i-1][j];
}
}
return dp[n][W];
}
int main()
{
// int val[] = {60, 100, 120};
// int wt[] = {10, 20, 30};
int val[] = {2,3,4,1};
int wt[] = {3,4,5,6};
int W = 8;
int n = sizeof(val)/sizeof(val[0]);
cout << "Maximum value that can be obtained is: " << knapsack(W, wt, val, n);
return 0;
}
//Fractional Knapsack
#include<bits/stdc++.h>
using namespace std;
struct Item{
int value;
int weight;
};
bool cmp(Item a,Item b){
double ratio1 = (double) (a.value/a.weight);
double ratio2 = (double) (b.value/b.weight);
return ratio1 > ratio2;
}
int main()
{
Item items[] = {{60,10},{100,20},{120,30}};
int i=0;
int allowedWeight = 50;
int currentWeight = 0;
double currentValue = 0.0;
int size = sizeof(items)/ sizeof(items[0]);
sort(items,items+size,cmp);
for(i=0;i<size;i++)
{
if(currentWeight + items[i].weight <= allowedWeight)
{
currentValue += items[i].value;
currentWeight += items[i].weight;
}
else{
int remainingWeight = allowedWeight - currentWeight;
int valueOfRemainingWeight = items[i].value * ((double) remainingWeight/items[i].weight);
currentWeight += remainingWeight;
currentValue += valueOfRemainingWeight;
}
}
printf("%d %.2fn",currentWeight,currentValue);
}
///Number of ways of making money using coins
#include<bits/stdc++.h>
using namespace std;
int numOfWay(int coins[],int sz,int money)
{
int values[money+1];
memset(values,0,sizeof(values));
values[0] = 1;
for(int i=0;i<sz;i++){
for(int j=coins[i];j<=money;j++)
values[j] += values[j - coins[i]];
}
return values[money];
}
int main()
{
int coins[] = {1,2};
int money = 6;
int sz = sizeof(coins) / sizeof(coins[0]);
int ans = numOfWay(coins,sz,money);
printf("Money %d can be made in %d ways.n",money,ans);
}
#include <iostream>
#include <climits>
using namespace std;
int minCoins(int coins[], int n, int amount) {
int dp[n + 1][amount + 1];
for (int i = 0; i <= n; ++i)
dp[i][0] = 0;
for (int j = 1; j <= amount; ++j)
dp[0][j] = INT_MAX;
for (int i = 1; i <= n; ++i) {
for (int j = 1; j <= amount; ++j) {
if (coins[i - 1] <= j && dp[i][j - coins[i - 1]] != INT_MAX)
dp[i][j] = min(dp[i - 1][j], dp[i][j - coins[i - 1]] + 1);
else
dp[i][j] = dp[i - 1][j];
}
}
return dp[n][amount] == INT_MAX ? -1 : dp[n][amount];
}
int main() {
int coins[] = {1,2,4};
int n = sizeof(coins) / sizeof(coins[0]);
int amount = 8;
int minCoinsRequired = minCoins(coins, n, amount);
if (minCoinsRequired != -1) {
cout << "Minimum coins required to make " << amount << ": " << minCoinsRequired << endl;
} else {
cout << "It's not possible to make " << amount << " with the given coins." << endl;
}
return 0;
}
//ActivitySelection
#include<bits/stdc++.h>
using namespace std;
struct Activity{
int start, en;
};
bool cmp(Activity act1, Activity act2){
return (act1.en < act2.en);
}
void maxActivity(Activity act[], int n){
sort(act, act+n, cmp);
int i=0;
cout<<i<<" "<<act[i].start<<" "<<act[i].en<<endl;
for(int j=0;j<n;j++){
if(act[j].start >= act[i].en){
cout<<j<<" "<<act[j].start<<" "<<act[j].en<<endl;
i=j;
}
}
}
int main(){
Activity actArr[] = {{5,9},{1,2},{3,4},{0,6},{5,7},{8,9},{4,5}};
int n=sizeof(actArr)/sizeof(actArr[0]);
maxActivity(actArr, n);
}
//LIS
#include<bits/stdc++.h>
using namespace std;
vector<int> v;
int lis(int arr[], int n)
{
int lis[n],i,j,maxValue = 0;
for(i=0; i<n; i++)
lis[i] = 1;
for(i=1; i<n; i++)
for(j=0; j<i; j++)
if(arr[i] > arr[j] && lis[i] <lis[j] +1)
lis[i] = lis[j]+1;
for(i=0; i<n; i++)
if(maxValue < lis[i])
maxValue = lis[i];
int ans = maxValue;
for(int k=n-1;k>=0;k--)
{
if(lis[k] == maxValue)
{
v.push_back(arr[k]);
maxValue--;
}
}
cout<<"--"<<endl;
for(int i=0;i<n;i++)cout<<lis[i]<<" ";
cout<<endl;
return ans;
}
int main()
{
// int arr[] = {4,5,1,2,2,3};
int arr[] = {10,22,9,33,21,50,41,60,80};
int n=sizeof(arr)/sizeof(arr[0]);
printf("Length of lis is %dn",lis(arr, n));
for(int i=v.size()-1;i>=0;i--)
cout<<v[i]<<" ";
cout<<endl;
return 0;
}
//QuickSort
#include<bits/stdc++.h>
using namespace std;
int partition_array(int a[], int start, int en)
{
int pivot = a[en];
int partitionIndex = start;
for(int i=start; i<en; i++)
{
if(a[i] <= pivot)
{
swap(a[i], a[partitionIndex]);
partitionIndex++;
}
}
swap(a[partitionIndex],a[en]);
return partitionIndex;
}
void quicksort(int a[],int start, int en)
{
if(start<en)
{
int partitionIndex = partition_array(a,start,en);
quicksort(a,start, partitionIndex-1);
quicksort(a, partitionIndex+1,en);
}
}
int main()
{
int a[] = {5,6,4,2,7,1,3,2,60,3,3,3,7};
int sz = sizeof(a)/sizeof(a[0]);
quicksort(a,0,sz-1);
for(int i=0; i<sz; i++) cout<<a[i]<<" ";
cout<<endl;
}
/* Merge sort in C */
#include<bitsstdc++.h>
using namespace std;
void Merge(int A[],int L[],int leftCount,int R[],int rightCount)
{
int i,j,k;
i = 0;
j = 0;
k =0;
while(i<leftCount && j< rightCount)
{
if(L[i] < R[j]) A[k++] = L[i++];
else A[k++] = R[j++];
}
while(i < leftCount) A[k++] = L[i++];
while(j < rightCount) A[k++] = R[j++];
}
void MergeSort(int A[],int n)
{
int mid,i;
if(n < 2) return;
mid = n/2;
int L[mid], R[n-mid];
for(i = 0; i<mid; i++) L[i] = A[i];
for(i = mid; i<n; i++) R[i-mid] = A[i];
MergeSort(L,mid);
MergeSort(R,n-mid);
Merge(A,L,mid,R,n-mid);
}
int main()
{
int A[] = {6,2,3,1,9,10,15,13,12,17};
int i,numberOfElements;
numberOfElements = sizeof(A)/sizeof(A[0]);
MergeSort(A,numberOfElements);
for(i = 0; i < numberOfElements; i++) cout<<A[i]<<" ";
cout<<"n";
return 0;
}
![]() |
Notes.io is a web-based application for taking notes. You can take your notes and share with others people. If you like taking long notes, notes.io is designed for you. To date, over 8,000,000,000 notes created and continuing...
With notes.io;
- * You can take a note from anywhere and any device with internet connection.
- * You can share the notes in social platforms (YouTube, Facebook, Twitter, instagram etc.).
- * You can quickly share your contents without website, blog and e-mail.
- * You don't need to create any Account to share a note. As you wish you can use quick, easy and best shortened notes with sms, websites, e-mail, or messaging services (WhatsApp, iMessage, Telegram, Signal).
- * Notes.io has fabulous infrastructure design for a short link and allows you to share the note as an easy and understandable link.
Fast: Notes.io is built for speed and performance. You can take a notes quickly and browse your archive.
Easy: Notes.io doesn’t require installation. Just write and share note!
Short: Notes.io’s url just 8 character. You’ll get shorten link of your note when you want to share. (Ex: notes.io/q )
Free: Notes.io works for 12 years and has been free since the day it was started.
You immediately create your first note and start sharing with the ones you wish. If you want to contact us, you can use the following communication channels;
Email: [email protected]
Twitter: http://twitter.com/notesio
Instagram: http://instagram.com/notes.io
Facebook: http://facebook.com/notesio
Regards;
Notes.io Team