Skip to content

Commit 790e010

Browse files
authored
Merge branch 'master' into master
2 parents 9872c51 + 58de7d1 commit 790e010

File tree

15 files changed

+328
-1
lines changed

15 files changed

+328
-1
lines changed

CONTRIBUTORS.md

+4-1
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,9 @@ Thanks for all your contributions :heart: :octocat:
1515
|[Rahul Arulkumaran](https://github.com/rahulkumaran)| [#94](https://github.com/codeIIEST/Algorithms/pull/94), [#95](https://github.com/codeIIEST/Algorithms/pull/95), [#112](https://github.com/codeIIEST/Algorithms/pull/112), [#151](https://github.com/codeIIEST/Algorithms/pull/151), [#174](https://github.com/codeIIEST/Algorithms/pull/174) | MERGED |
1616
|[Rahul Arulkumaran](https://github.com/rahulkumaran)| [#206](https://github.com/codeIIEST/Algorithms/pull/206), [#207](https://github.com/codeIIEST/Algorithms/pull/207) | OPEN |
1717
|[Abhishek Nalla](https://github.com/abhisheknalla)| [#129](https://github.com/codeIIEST/Algorithms/pull/129) ,[#147](https://github.com/codeIIEST/Algorithms/pull/147) [#204](https://github.com/codeIIEST/Algorithms/pull/204)| MERGED |
18-
| [Zanark ( Debashish Mishra )](https://github.com/Zanark) | [PR #222](https://github.com/codeIIEST/Algorithms/pull/222) , [PR #146](https://github.com/codeIIEST/Algorithms/pull/146) , [PR #135](https://github.com/codeIIEST/Algorithms/pull/135) | OPEN , MERGED , CLOSED|
18+
| [Zanark ( Debashish Mishra )](https://github.com/Zanark) | [PR #222](https://github.com/codeIIEST/Algorithms/pull/222) , [PR #146](https://github.com/codeIIEST/Algorithms/pull/146) , [PR #225](https://github.com/codeIIEST/Algorithms/pull/225) , [PR #135](https://github.com/codeIIEST/Algorithms/pull/135) | CLOSED , MERGED , MERGED , CLOSED|
1919
| [d3v3sh5ingh](https://github.com/D3v3sh5ingh) | [PR #126](https://github.com/codeIIEST/Algorithms/pull/126) , [PR #161](https://github.com/codeIIEST/Algorithms/pull/161) | Closed , MERGED|
2020
|[AdvikEshan( Nitish Kumar Tiwari )](https://github.com/AdvikEshan)| [PR #97](https://github.com/codeIIEST/Algorithms/pull/97),[PR #192](https://github.com/codeIIEST/Algorithms/pull/192), [PR #258](https://github.com/codeIIEST/Algorithms/pull/258)|Merged,Merged,Merged|
21+
|[imVivekGupta](https://github.com/imVivekGupta)|[#173](https://github.com/codeIIEST/Algorithms/pull/173), [#182](https://github.com/codeIIEST/Algorithms/pull/182), [#218](https://github.com/codeIIEST/Algorithms/pull/218), [#230](https://github.com/codeIIEST/Algorithms/pull/230) | MERGED |
22+
|[Priyangshuyogi](https://github.com/Priyangshuyogi)| [#188](https://github.com/codeIIEST/Algorithms/pull/188), [#227](https://github.com/codeIIEST/Algorithms/pull/227), [#251](https://github.com/codeIIEST/Algorithms/pull/251)| MERGED , MERGED , OPEN |
23+
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,70 @@
1+
import java.util.*;
2+
3+
4+
class Knapsack
5+
{
6+
7+
// A function to return maximum of two integers
8+
static int maximum(int a, int b)
9+
{
10+
if(a>b)
11+
return a;
12+
else
13+
return b;
14+
15+
}
16+
17+
// Returns the maximum value that can be put in a knapsack of capacity W
18+
static int knapSack(int W, int wt[], int val[], int n)
19+
{
20+
int i, w;
21+
int K[][] = new int[n+1][W+1];
22+
23+
// Build table K[][] in bottom up manner
24+
for (i = 0; i <= n; i++)
25+
{
26+
for (w = 0; w <= W; w++)
27+
{
28+
if (i==0 || w==0)
29+
K[i][w] = 0;
30+
else if (wt[i-1] <= w)
31+
K[i][w] = maximum(val[i-1] + K[i-1][w-wt[i-1]] , K[i1][w]);
32+
else
33+
K[i][w] = K[i-1][w];
34+
}
35+
}
36+
37+
Return K[n][W];
38+
}
39+
40+
41+
42+
public static void main(String args[])
43+
{
44+
45+
Scanner s=new Scanner(System.in);
46+
System.out.println(“Enter number of objects”);
47+
int n=s.nextInt();
48+
int val[]=new int[n];
49+
int wt[] = new int[n];
50+
System.out.println(“Enter values of objects”);
51+
52+
53+
for(int i=0;i<n;i++)
54+
{
55+
val[i]=s.nextInt();
56+
}
57+
58+
System.out.println(“Enter weights of object”);
59+
for(int i=0;i<n;i++)
60+
{
61+
wt[i]=s.nextInt();
62+
}
63+
64+
65+
System.out.println(“Enter the maximum weight”);
66+
int W = s.nextInt();
67+
System.out.println(knapSack(W, wt, val, n));
68+
}
69+
}
70+
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
# 0-1 Knapsack
2+
The knapsack problem or rucksack problem is a problem in combinatorial optimization: Given a set of items, each with a weight and a value, determine the number of each item to include in a collection so that the total weight is less than or equal to a given limit and the total value is as large as possible. It derives its name from the problem faced by someone who is constrained by a fixed-size knapsack and must fill it with the most valuable items.
3+
4+
**Time Complexity**
5+
Time Complexity: O(nW) where n is the number of items and W is the capacity of knapsack.
6+
### Applications
7+
Knapsack problems appear in real-world decision-making processes in a wide variety of fields, such as finding the least wasteful way to cut raw materials,[4] selection of investments and portfolios selection of assets for asset-backed securitization and generating keys for the Merkle–Hellman and other knapsack cryptosystems
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
Problem :-
2+
===
3+
Given a value N and the values of different denomination,find the number of ways to make changes for N cents,if we have infinite supply of all denominations.
4+
5+
Input:-
6+
---
7+
Size of an array and all the values in the array and the number of cents.
8+
9+
Output :-
10+
---
11+
An integer that denotes the number of ways to make change.
12+
13+
#### Language : `C++`
14+
15+
#### Algorithm Paradigm : `Dynamic Programming`
16+
17+
#### Time Complexity : `O(N*M)`
18+
19+
#### Space Complexity : `O(N*M)`
20+
21+
Working :-
22+
---
23+
An arr array stores the different currency values.
24+
Another array temp stores the best values for sub-problems.For eg: `temp[3][4]` stores the optimal value for the number of ways
25+
to make change if the number of cents(N) were 4 and we only had `arr[0]`,`arr[1]` and `arr[2]` as the different values of currency.
26+
27+
By using dynamic programming we are bringing the time complexity down from exponentional(in case of brute force solution)
28+
to polynomial.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
//Coin change problem using dynamic programming in C++.
2+
#include<bits/stdc++.h>
3+
4+
using namespace std;
5+
6+
int main()
7+
{
8+
int M;//Variable to store the number of different currency values.
9+
cout<<"Enter the number of denominations : ";
10+
cin>>M;//Inputting the number of different currency value from user.
11+
cout<<endl;
12+
13+
int arr[M];//Array to store different currency values.
14+
for(int i=0;i<M;i++)
15+
{
16+
cout<<"Enter the value of denominaion "<<i+1<<" : ";
17+
cin>>arr[i];//Inputting the value of each currency from user.
18+
}
19+
cout<<endl;
20+
21+
int N;//Variable to store the number of cents whose number of ways to make change is to be found.
22+
cout<<"Enter the number of cents : ";
23+
cin>>N;//Inputting the number of cents from user.
24+
cout<<endl;
25+
26+
/*2D array to store the optimal value for sub-cases.*/
27+
int temp[M+1][N+1];
28+
29+
for(int i=0;i<M+1;i++)//Implementing the algorithm.
30+
{
31+
for(int j=0;j<N+1;j++)
32+
{
33+
if(i==0 || j==0)
34+
temp[i][j]=0;
35+
else if(j<arr[i-1])
36+
temp[i][j]=temp[i-1][j];
37+
else if(j==arr[i-1])
38+
temp[i][j]=1+temp[i-1][j];
39+
else
40+
temp[i][j]=temp[i-1][j]+temp[i][j-arr[i-1]];
41+
}
42+
}
43+
44+
cout<<"Number of ways to make change for "<<N<<" cent is "<<temp[M][N]<<endl;//Printing the final answer.
45+
46+
return 0;
47+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
The two different programs in this folder contain the same implementation!
2+
The programs basically are a python implementation of the Catalan Numbers.
3+
They're similar to Fibonacci (very slightly)
4+
5+
The two different implementations vary only in terms of time taken!<br>
6+
Basically we can implement in 3 different ways!<br>
7+
8+
* Recursively - `Exponential time complexity`<br>
9+
* Dynamic Programming - `O(n^2)`<br>
10+
* Binomial Coefficient - `O(n)` <br><br>
11+
12+
Each of the above 3 implementations have a descending order of time complexities from (1) to (3) <br><br>
13+
14+
The Binomial implementation has the best time complexity while the Recursive implementation has the worst time complexity<br>
15+
In this folder we have the Binomial and Recursive implementations of Catalan Numbers.<br><br>
16+
17+
Catalan Numbers have a lot of applications in Combinatorics. Some of them are listed below:
18+
* Cn is the number of standard Young tableaux whose diagram is a 2-by-n rectangle. In other words, it is the number of ways the numbers 1, 2, ..., 2n can be arranged in a 2-by-n rectangle so that each row and each column is increasing. As such, the formula can be derived as a special case of the hook-length formula.
19+
* Cn is the number of ways that the vertices of a convex 2n-gon can be paired so that the line segments joining paired vertices do not intersect. This is precisely the condition that guarantees that the paired edges can be identified (sewn together) to form a closed surface of genus zero (a topological 2-sphere).
20+
* Cn is the number of semiorders on n unlabeled items.
21+
* In chemical engineering Cn-1 is the number of possible separation sequences which can separate a mixture of n components.<br><br>
22+
23+
### Sources :
24+
(1) Click [here](https://www.geeksforgeeks.org/program-nth-catalan-number/) to know more about Catalan number implementations and its theory.<br>
25+
(2) Click [here](https://en.wikipedia.org/wiki/Catalan_number) to know more about the applicatations of Catalan Numbers in Combinatorics
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
def binCoeff(n, k): #Finds the binomial coefficient
2+
if (k > n - k): #As binCoeff(n,k)=binCoeff(n,n-k)
3+
k = n - k
4+
res = 1 #Initialising Result
5+
for i in range(k):
6+
res = res * (n - i)
7+
res = res / (i + 1)
8+
return res #The binomial coefficient is returned
9+
10+
def catalan(n): #Function that finds catalan numbers
11+
c = binCoeff(2*n, n) #Finding value of c by calling the binCoeff function
12+
return c/(n + 1) #This is the final catalan number
13+
14+
if __name__=='__main__':
15+
print "The 10th catalan number is:",catalan(9)
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
def catalan_numbers(n):
2+
if n <=1 : #The result will be 1, if the function takes an argument that's equal to or less than 1
3+
return 1
4+
res = 0 #Result has been initialised to 0
5+
for i in range(n):
6+
res += catalan_numbers(i) * catalan_numbers(n-i-1) #The recursive function call
7+
return res
8+
9+
if __name__=='__main__':
10+
print "The 10th catalan number is:",catalan(9)
Binary file not shown.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
package problem_21;
2+
class P21
3+
{ //Begin class
4+
5+
6+
/***** Function to calculate the sum of proper divisors or factors *****/
7+
public static int sumFactors(int num)
8+
{ //Begin sumFactors()
9+
int sum = 0;
10+
11+
for(int i = 1; i<num; i++)
12+
{ //Begin for
13+
if(num % i == 0)
14+
sum += i;
15+
} //End for
16+
17+
return sum;
18+
} //End sumFactors()
19+
20+
21+
22+
23+
public static void main(String args[])
24+
{ //Begin main()
25+
int sum_amicable = 0;
26+
int x = 0;
27+
int y = 0;
28+
29+
30+
for(int n = 1; n<=10000; n++)
31+
{ //Begin for
32+
33+
x = sumFactors(n); //Sum of factors or divisors of n
34+
y = sumFactors(x); //Sum of factors or divisors of x
35+
36+
/***
37+
1. Checking if the number is not equal to sum of its factors and if the number is equal to y(sum of factors of x) then the number is added to the sum of amicable numbers
38+
39+
2. The other number of the amicable pair is added when it appears in the loop later and checking is done for it ***/
40+
41+
if((n != x) && (n == y))
42+
sum_amicable += n;
43+
44+
} //End for
45+
46+
System.out.println("Sum of amicable numbers less than 10000 is:\t"+sum_amicable);
47+
} //End main()
48+
} //End class
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
# Amicable numbers
2+
3+
----------------------------------------------------------------------------------------------
4+
5+
### Problem 21
6+
7+
Let d(n) be defined as the sum of proper divisors of n (numbers less than n which divide evenly into n).
8+
If d(a) = b and d(b) = a, where a ≠ b, then a and b are an amicable pair and each of a and b are called amicable numbers.
9+
10+
For example, the proper divisors of 220 are 1, 2, 4, 5, 10, 11, 20, 22, 44, 55 and 110; therefore d(220) = 284. The proper divisors of 284 are 1, 2, 4, 71 and 142; so d(284) = 220.
11+
12+
Evaluate the sum of all the amicable numbers under 10000.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
# Project Euler
2+
3+
----------------------------------------------------------------------------------------------
4+
5+
### What is Project Euler?
6+
7+
Project Euler is a series of challenging mathematical/computer programming problems that will require more than just mathematical insights to solve. Although mathematics will help you arrive at elegant and efficient methods, the use of a computer and programming skills will be required to solve most problems.
8+
9+
The motivation for starting Project Euler, and its continuation, is to provide a platform for the inquiring mind to delve into unfamiliar areas and learn new concepts in a fun and recreational context.
10+
11+
12+
Click here to check out the problems: [Project Euler] (https://projecteuler.net/)
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
package Euler
2+
class P21
3+
{ //Begin class
4+
5+
6+
/***** Function to calculate the sum of proper divisors or factors *****/
7+
public static int sumFactors(int num)
8+
{ //Begin sumFactors()
9+
int sum = 0;
10+
11+
for(int i = 1; i<num; i++)
12+
{ //Begin for
13+
if(num % i == 0)
14+
sum += i;
15+
} //End for
16+
17+
return sum;
18+
} //End sumFactors()
19+
20+
21+
22+
23+
public static void main(String args[])
24+
{ //Begin main()
25+
int sum_amicable = 0;
26+
int x = 0;
27+
int y = 0;
28+
29+
30+
for(int n = 1; n<=10000; n++)
31+
{ //Begin for
32+
33+
x = sumFactors(n); //Sum of factors or divisors of n
34+
y = sumFactors(x); //Sum of factors or divisors of x
35+
36+
37+
/***
38+
1. Checking if the number is not equal to sum of its factors and if the number is equal to y(sum of factors of x) then the number is added to the sum of amicable numbers
39+
40+
2. The other number of the amicable pair is added when it appears in the loop later and checking is done for it ***/
41+
42+
if((n != x) && (n == y))
43+
sum_amicable += n;
44+
45+
} //End for
46+
47+
System.out.println("Sum of amicable numbers less than 10000 is:\t"+sum_amicable);
48+
} //End main()
49+
} //End class
1.14 KB
Binary file not shown.

GFG Articles/prateekiiest/article.md

+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
GFG link

0 commit comments

Comments
 (0)