Skip to content

Commit 250942a

Browse files
authored
Merge pull request #399 from amritansh22/revert-392-master
Revert "Added Unbounded Knapsack in C++"
2 parents 1e6cbfc + f7e36cb commit 250942a

File tree

164 files changed

+10246
-31
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

164 files changed

+10246
-31
lines changed

.DS_Store

12 KB
Binary file not shown.

.github/config.yml

+19
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
# Configuration for new-issue-welcome - https://github.com/behaviorbot/new-issue-welcome
2+
3+
# Comment to be posted to on first time issues
4+
newIssueWelcomeComment: >
5+
Thanks for opening your first issue here!
6+
7+
# Configuration for new-pr-welcome - https://github.com/behaviorbot/new-pr-welcome
8+
9+
# Comment to be posted to on PRs from first time contributors in your repository
10+
newPRWelcomeComment: >
11+
Thanks for opening this pull request! Please be sure that you have checked out our contributing guidelines.
12+
13+
# Configuration for first-pr-merge - https://github.com/behaviorbot/first-pr-merge
14+
15+
# Comment to be posted to on pull requests merged by a first time user
16+
firstPRMergeComment: >
17+
Congrats on merging your first pull request! We are proud of you!
18+
19+
# It is recommend to include as many gifs and emojis as possible

0-1 Knapsack

+44
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
//Question: - You are given weights and values of N items,
2+
//put these items in a knapsack of capacity W to get the maximum total value in the knapsack.
3+
//Note that we have only one quantity of each item.
4+
5+
//Dynamic Programming - 0/1 Knapsack
6+
7+
#include <iostream>
8+
#include<bits/stdc++.h>
9+
using namespace std;
10+
11+
12+
int main() {
13+
14+
int t;
15+
cin>>t;
16+
while(t--)
17+
{
18+
int n;
19+
cin>>n;
20+
int w;
21+
cin>>w;
22+
int val[n],wt[n],i,j;
23+
for(i=0;i<n;i++)
24+
cin>>val[i];
25+
for(i=0;i<n;i++)
26+
cin>>wt[i];
27+
int dp[n+1][w+1];
28+
29+
for(i=0;i<=n;i++)
30+
for(j=0;j<=w;j++)
31+
{ if(i==0||j==0)
32+
dp[i][j]=0;
33+
else
34+
{if(wt[i-1]>j)
35+
dp[i][j]=dp[i-1][j];
36+
else
37+
dp[i][j]=max(val[i-1]+dp[i-1][j-wt[i-1]],dp[i-1][j]);
38+
}
39+
}
40+
41+
cout<<dp[n][w]<<endl;
42+
}
43+
return 0;
44+
}

494-Target_sum-Leetcode-DP.cpp

+43
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
#include<bits/stdc++.h>
2+
#define M 1001
3+
using namespace std;
4+
5+
int t[M][M];
6+
7+
int Count_Subset_Sum(vector<int> &arr, int sum, int n)
8+
{
9+
//Initialization
10+
for(int i=0; i<M; i++)
11+
{
12+
t[0][i] = 0;
13+
t[i][0] = 1;
14+
}
15+
//Choice Diagram
16+
for(int i=1; i<n+1; i++)
17+
{
18+
for(int j=1; j<sum+1; j++)
19+
{
20+
if(arr[i-1]<=j)
21+
t[i][j] = t[i-1][j] + t[i-1][j-arr[i-1]];
22+
else
23+
t[i][j] = t[i-1][j];
24+
}
25+
}
26+
return t[n][sum];
27+
}
28+
29+
int main()
30+
{
31+
int n,diff;
32+
cin>>n>>diff;
33+
vector<int> arr(n);
34+
for(int i=0; i<n; i++)
35+
cin>>arr[i];
36+
int arr_sum = 0;
37+
for(int i=0; i<n; i++)
38+
arr_sum += arr[i];
39+
int sum = (diff+arr_sum)/2;
40+
memset(t,0,sizeof(t));
41+
cout<<Count_Subset_Sum(arr,sum,n);
42+
return 0;
43+
}

Abhinav_LongestAP.cpp

+60
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
2+
#include <iostream>
3+
using namespace std;
4+
5+
int lenghtOfLongestAP(int set[], int n)
6+
{
7+
if (n <= 2) return n;
8+
9+
int L[n][n];
10+
int llap = 2; // Initialize the result
11+
12+
for (int i = 0; i < n; i++)
13+
L[i][n-1] = 2;
14+
15+
for (int j=n-2; j>=1; j--)
16+
{
17+
int i = j-1, k = j+1;
18+
while (i >= 0 && k <= n-1)
19+
{
20+
if (set[i] + set[k] < 2*set[j])
21+
k++;
22+
23+
else if (set[i] + set[k] > 2*set[j])
24+
{ L[i][j] = 2, i--; }
25+
26+
else
27+
{
28+
L[i][j] = L[j][k] + 1;
29+
30+
llap = max(llap, L[i][j]);
31+
32+
i--; k++;
33+
}
34+
}
35+
36+
while (i >= 0)
37+
{
38+
L[i][j] = 2;
39+
i--;
40+
}
41+
}
42+
return llap;
43+
}
44+
45+
int main()
46+
{
47+
int set1[] = {1, 7, 10, 13, 14, 19};
48+
int n1 = sizeof(set1)/sizeof(set1[0]);
49+
cout << lenghtOfLongestAP(set1, n1) << endl;
50+
51+
int set2[] = {1, 7, 10, 15, 27, 29};
52+
int n2 = sizeof(set2)/sizeof(set2[0]);
53+
cout << lenghtOfLongestAP(set2, n2) << endl;
54+
55+
int set3[] = {2, 4, 6, 8, 10};
56+
int n3 = sizeof(set3)/sizeof(set3[0]);
57+
cout << lenghtOfLongestAP(set3, n3) << endl;
58+
59+
return 0;
60+
}

Array_Stack.cpp

+219
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,219 @@
1+
#include <iostream>
2+
#include <iomanip>
3+
using namespace std;
4+
5+
const int size = 10;
6+
7+
template <class T>
8+
class stacktype{
9+
T s[size];
10+
int top;
11+
public :
12+
void push(T);
13+
T pop();
14+
int isempty();
15+
int isfull();
16+
void display();
17+
T topmost();
18+
void clear();
19+
20+
stacktype()
21+
{
22+
top = -1;
23+
}
24+
};
25+
26+
template <class T>
27+
void stacktype<T>::push(T p)
28+
{
29+
top++;
30+
s[top] = p;
31+
}
32+
33+
template <class T>
34+
T stacktype<T>::pop()
35+
{
36+
int d;
37+
d = s[top];
38+
top--;
39+
return d;
40+
}
41+
42+
template <class T>
43+
int stacktype<T>::isempty()
44+
{
45+
int e;
46+
if(top == -1)
47+
{
48+
cout<<"\nStack is empty"<<endl;
49+
return 1;
50+
}
51+
else
52+
{
53+
cout<<"\nStack is not empty"<<endl;
54+
return 0;
55+
}
56+
}
57+
58+
template <class T>
59+
int stacktype<T>::isfull()
60+
{
61+
int f;
62+
if(top == size-1)
63+
{
64+
cout<<"\nStack is full"<<endl;
65+
return 1;
66+
}
67+
else
68+
{
69+
cout<<"\nStack is not full"<<endl;
70+
return 0;
71+
}
72+
}
73+
74+
template <class T>
75+
T stacktype<T>::topmost()
76+
{
77+
return s[top];
78+
}
79+
80+
template <class T>
81+
void stacktype<T>::clear()
82+
{
83+
top = -1;
84+
display();
85+
}
86+
87+
template <class T>
88+
void stacktype<T>::display()
89+
{
90+
if(top == -1)
91+
{
92+
cout<<"\nStack is empty"<<endl;
93+
}
94+
else
95+
{
96+
cout<<"\nStack elements starting from top are - ";
97+
for(int i=top;i>=0;i--)
98+
{
99+
cout<<setw(3)<<s[i];
100+
}
101+
cout<<endl;
102+
}
103+
104+
}
105+
106+
int main()
107+
{
108+
stacktype<int> st;
109+
char c = 'y';
110+
int choice,f,p,d,e;
111+
112+
while(c=='y'||c=='Y')
113+
{
114+
cout<<"MAIN MENU :"<<endl;
115+
cout<<"From the following options which operation you want to perform on stack :"<<endl;
116+
cout<<"1. PUSH onto the stack "<<endl;
117+
cout<<"2. POP from the stack "<<endl;
118+
cout<<"3. Check if stack is EMPTY "<<endl;
119+
cout<<"4. Check if stack is FULL "<<endl;
120+
cout<<"5. CLEAR the stack "<<endl;
121+
cout<<"6. TOPMOST element of stack "<<endl;
122+
cout<<"7. DISPLAY the stack "<<endl;
123+
cout<<"Enter your Choice: ";
124+
cin>>choice;
125+
126+
switch(choice)
127+
{
128+
case 1:
129+
f = st.isfull();
130+
if(f==1) //f==1 implies stack is full
131+
{
132+
cout<<endl;
133+
cout<<"Insertion of a new element is not possible"<<endl; //overflow condition
134+
}
135+
else
136+
{
137+
cout<<"Enter the element you want to push- ";
138+
cin>>p;
139+
st.push(p);
140+
st.display();
141+
}
142+
break;
143+
144+
145+
case 2:
146+
e = st.isempty();
147+
if(e==1) //e==1 implies stack is empty
148+
{
149+
cout<<endl;
150+
cout<<"Deletion not possible"<<endl; //Underflow condition
151+
}
152+
else
153+
{
154+
d = st.pop();
155+
cout<<"\n"<<d<<" has been deleted";
156+
st.display();
157+
}
158+
break;
159+
160+
case 3:
161+
e = st.isempty();
162+
if(e==1)
163+
{
164+
cout<<endl;
165+
}
166+
else
167+
{
168+
cout<<endl;
169+
st.display();
170+
}
171+
break;
172+
173+
case 4:
174+
f = st.isfull();
175+
if(f==1)
176+
{
177+
cout<<endl;
178+
st.display();
179+
}
180+
else
181+
{
182+
cout<<endl;
183+
}
184+
break;
185+
186+
case 5:
187+
st.clear();
188+
cout<<"\nStack is cleared!!"<<endl;
189+
break;
190+
191+
case 6:
192+
e = st.isempty();
193+
if(e == 1)
194+
{
195+
cout<<endl;
196+
}
197+
else
198+
{
199+
cout<<"The topmost element of the stack is - ";
200+
cout<<st.topmost()<<endl;
201+
}
202+
/*cout<<"The topmost element of the stack is - ";
203+
cout<<st.topmost()<<endl;*/
204+
break;
205+
206+
case 7:
207+
st.display();
208+
break;
209+
210+
default:
211+
cout<<"Invalid Choice!!"<<endl;
212+
break;
213+
}
214+
215+
cout<<"\nDo you want to continue ??(Y/N): ";
216+
cin>>c;
217+
}
218+
return 0;
219+
}

0 commit comments

Comments
 (0)