Skip to content

Commit 93ddad0

Browse files
authored
Merge branch 'smv1999:master' into master
2 parents c54b11f + 6e01d61 commit 93ddad0

File tree

8 files changed

+426
-28
lines changed

8 files changed

+426
-28
lines changed

Arrays/Average_Calculator.py

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
'''
2+
Aim: To calculate average of all the distinct integers.
3+
4+
'''
5+
6+
def average(array):
7+
# getting all unique numbers
8+
p = set(array)
9+
# calculating the total number of elements
10+
s = len(p)
11+
# computing the average
12+
ans = sum(p)/s
13+
# printing the result
14+
print('Average is:',ans)
15+
16+
# getting the input
17+
arr = input('Enter numbers: ').strip().split()
18+
# converting elements into integer type for calculating average
19+
arr_int = []
20+
for i in arr:
21+
arr_int.append(int(i))
22+
# calling function to compute the average
23+
average(arr_int)
24+
25+
'''
26+
27+
COMPLEXITY:
28+
29+
Time Complexity -> O(1)
30+
Space Complexity -> O(N)
31+
32+
Sample Input:
33+
Enter numbers: 5 3 1 2 3 4 5
34+
35+
Sample Output:
36+
Average is: 3.0
37+
38+
Explanation:
39+
All unique integers --> 1 2 3 4 5
40+
Total count --> 5
41+
Average --> (1+2+3+4+5)/5 = 3.0
42+
43+
'''
Lines changed: 80 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,80 @@
1+
/*Problem statement => https://www.spoj.com/problems/SQRBR/
2+
You are given:
3+
4+
-> a positive integer n,
5+
-> an integer k, 1<=k<=n,
6+
-> an increasing sequence of k integers 0 < s1 < s2 < ... < sk <= 2n.
7+
What is the number of proper bracket expressions of length 2n with opening brackets appearing in positions s1, s2,...,sk?
8+
*/
9+
10+
11+
#include<bits/stdc++.h>
12+
using namespace std;
13+
14+
15+
typedef string STR;
16+
typedef long long LL;
17+
typedef vector<LL> VLL;
18+
19+
#define all(a) (a).begin(),(a).end()
20+
#define dec(n) cout<<fixed<<setprecision(n);
21+
#define f(i,a,b) for (i = a; i < b; i++)
22+
#define fr(i,a,b) for (i = a; i > b; i--)
23+
#define rep(i,n) for (i = 0 ; i < n; i++)
24+
#define repr(i,n) for (i = n - 1; i >= 0; i--)
25+
#define fsort(a) sort(a.begin(),a.end())
26+
#define rsort(a) sort(a.rbegin(),a.rend())
27+
28+
const LL MOD = 1000000007;
29+
30+
LL sq_brackets(LL n, LL k, VLL pos)
31+
{
32+
vector<bool>h(2*n+1, 0);
33+
LL i;
34+
rep(i, k) h[pos[i]]=1; // hash array ti store the postions where opening bracket is must
35+
// dp array
36+
vector<vector<LL>>dp(2*n+1, vector<LL>(2*n+1, 0));
37+
// first position only one possible combination
38+
dp[0][0] = 1%MOD;
39+
// dp[i][j] represents i positions such that there are j more '[' brackets than ']'
40+
for(LL i=1;i<=2*n;i++)
41+
{
42+
for(LL j=0;j<=2*n;j++)
43+
{
44+
//if that position has an opening bracket
45+
if(h[i])
46+
{
47+
// if diff(j) is not 0 then ith position has '[' hence dp[i][j]=dp[i-1][j-1]
48+
if(j!=0)
49+
dp[i][j] = dp[i-1][j-1]%MOD;
50+
else
51+
dp[i][j]=0; // no possibility if j==0
52+
}
53+
else
54+
{
55+
// both opening and closing brackets possible if h[i]!=1
56+
if(j!=0)
57+
dp[i][j] = (dp[i-1][j-1]%MOD + dp[i-1][j+1]%MOD)%MOD;
58+
else
59+
dp[i][j] = dp[i-1][j+1]%MOD;
60+
}
61+
}
62+
}
63+
return dp[2*n][0];
64+
}
65+
int main()
66+
{
67+
int t=1;
68+
cin>>t;
69+
while(t--)
70+
{
71+
LL n,k,i;
72+
cin>>n>>k;
73+
VLL pos(k);
74+
rep(i,k)
75+
cin>>pos[i];
76+
LL ans = sq_brackets(n, k, pos);
77+
cout<<ans<<"\n";
78+
}
79+
return 0;
80+
}
Lines changed: 122 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,122 @@
1+
/*Write a program to implement the job sequencing with deadline problem using greedy method. Consider the
2+
following data for the problem. N=4
3+
4+
JOB: J1 J2 J3 J4
5+
PROFIT: 100 10 15 27
6+
DEADLINE: 2 1 2 1
7+
*/
8+
9+
#include <stdio.h>
10+
11+
// Structure of each job
12+
struct job
13+
{
14+
int id;
15+
int profit;
16+
int deadline;
17+
};
18+
19+
/* Auxiliary Functions */
20+
21+
int scanJobs(struct job Jobs[], int n)
22+
{
23+
// Scan n number of jobs
24+
int i, max_deadline = 0;
25+
for (i = 0; i < n; i++)
26+
{
27+
printf("Enter the profit and deadline of job (J%d): ", i + 1);
28+
scanf("%d %d", &Jobs[i].profit, &Jobs[i].deadline);
29+
Jobs[i].id = i + 1;
30+
if (Jobs[i].deadline > max_deadline)
31+
{
32+
max_deadline = Jobs[i].deadline;
33+
}
34+
}
35+
return max_deadline;
36+
}
37+
38+
void sort(struct job Jobs[], int n)
39+
{
40+
// Sort the job in descending order of profit
41+
int i, j;
42+
struct job key;
43+
for (i = 1; i < n; i++)
44+
{
45+
key = Jobs[i];
46+
j = i - 1;
47+
while ((j >= 0) && (Jobs[j].profit < key.profit))
48+
{
49+
Jobs[j + 1] = Jobs[j];
50+
j--;
51+
}
52+
Jobs[j + 1] = key;
53+
}
54+
}
55+
56+
int isEmpty(int arr[], int i)
57+
{
58+
// Returns True if i'th slot of an array is empty
59+
return arr[i] == 0 ? 1 : 0;
60+
}
61+
62+
void initSlots(int arr[], int m)
63+
{
64+
// Initialise job slots to 0
65+
int i;
66+
for (i = 0; i < m; i++)
67+
{
68+
arr[i] = 0;
69+
}
70+
}
71+
72+
int job_sequencing(struct job Jobs[], int m)
73+
{
74+
// Job sequencing function
75+
int slots[m], profit = 0;
76+
initSlots(slots, m);
77+
int i, j, deadlineIdx;
78+
for (i = 0; i < m; i++)
79+
{
80+
deadlineIdx = Jobs[i].deadline - 1;
81+
if (isEmpty(slots, deadlineIdx))
82+
{
83+
slots[deadlineIdx] = Jobs[i].id;
84+
profit += Jobs[i].profit;
85+
}
86+
else
87+
{
88+
for (j = deadlineIdx - 1; j >= 0; j--)
89+
{
90+
if (isEmpty(slots, j))
91+
{
92+
slots[j] = Jobs[i].id;
93+
profit += Jobs[i].profit;
94+
}
95+
}
96+
}
97+
}
98+
printf("\nJobs sequence: { ");
99+
for (i = 0; i < m; i++)
100+
{
101+
printf("J%d ", slots[i]);
102+
}
103+
printf("}");
104+
return profit;
105+
}
106+
107+
int main()
108+
{
109+
int n, m, max_profit;
110+
/*
111+
n: number of jobs
112+
m: number of job slots
113+
*/
114+
printf("Enter the number of jobs: ");
115+
scanf("%d", &n);
116+
struct job Jobs[n];
117+
m = scanJobs(Jobs, n);
118+
sort(Jobs, n);
119+
max_profit = job_sequencing(Jobs, m);
120+
printf("\nOptimal maximum profit: %d\n", max_profit);
121+
return 0;
122+
}
Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
// C++ program to demonstrate Hierarchical Inheritance
2+
3+
// Hierarchical Inheritance - It is defined as the inheritance in which more that one derived classes
4+
// are inherited from a single base class.
5+
6+
#include<bits/stdc++.h>
7+
using namespace std;
8+
9+
// base class
10+
class Animal{
11+
public:
12+
void eat(){
13+
cout<<"I can eat!"<<endl;
14+
}
15+
void sleep(){
16+
cout<<"I can sleep!"<<endl;
17+
}
18+
};
19+
20+
// derived class 1
21+
class Cat:public Animal{
22+
public:
23+
void meow(){
24+
cout<<"I can meow! meow! meow!"<<endl;
25+
}
26+
};
27+
28+
// derived class 2
29+
class Cow:public Animal{
30+
public:
31+
void moo(){
32+
cout<<"I can moo! moo! moo!"<<endl;
33+
}
34+
};
35+
36+
int main(){
37+
// Create object of Cat class
38+
Cat cat1;
39+
cout<<"Cat Class:"<<endl;
40+
//Calling members of the base class
41+
cat1.eat();
42+
cat1.sleep();
43+
//Calling members of the derived class
44+
cat1.meow();
45+
46+
// Create object of Cow class
47+
Cow cow1;
48+
cout<<"\nCow Class:"<<endl;
49+
//Calling members of the base class
50+
cow1.eat();
51+
cow1.sleep();
52+
//Calling members of the derived class
53+
cow1.moo();
54+
return 0;
55+
}
Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
// C++ program to demonstrate Multilevel Inheritance
2+
3+
// Multilevel Inheritance - It is defined as the inheritance in which the derived class is inherited
4+
// from a base class which itself is inherited from another base class.
5+
6+
#include<bits/stdc++.h>
7+
using namespace std;
8+
9+
// base class for Cat
10+
class Animal{
11+
public:
12+
void eat(){
13+
cout<<"I can eat!"<<endl;
14+
}
15+
void sleep(){
16+
cout<<"I can sleep!"<<endl;
17+
}
18+
};
19+
20+
// base class for Kitten, and derived from class Animal
21+
class Cat:public Animal{
22+
public:
23+
void meow(){
24+
cout<<"I can meow! meow! meow!"<<endl;
25+
}
26+
};
27+
28+
// derived from class Cat
29+
class Kitten:public Cat{
30+
public:
31+
void info(){
32+
cout<<"I am a Kitten."<<endl;
33+
}
34+
};
35+
36+
int main(){
37+
// Create object of Kitten class
38+
Kitten k1;
39+
//Calling members of the Kitten class
40+
k1.info();
41+
//Calling members of the Cat class
42+
k1.meow();
43+
//Calling members of the Animal class
44+
k1.eat();
45+
k1.sleep();
46+
return 0;
47+
}

README.md

Lines changed: 3 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -62,34 +62,9 @@ Head over to [Contributing Guidelines](https://github.com/smv1999/CompetitivePro
6262

6363
Thanks goes to these **Wonderful People** 👨🏻‍💻:
6464

65-
<!-- ALL-CONTRIBUTORS-LIST:START - Do not remove or modify this section -->
66-
<!-- prettier-ignore-start -->
67-
<!-- markdownlint-disable -->
68-
<table>
69-
<tr>
70-
<td align="center"><a href="https://github.com/smv1999"><img src="https://avatars.githubusercontent.com/u/42896577?s=400&u=9530610016fa2171d559af8bcdb3e9178bb7d308&v=4" width="100px;" alt=""/><br /><sub><b>smv1999</b></sub></a><br /><a href="https://github.com/smv1999/CompetitiveProgrammingQuestionBank/commits?author=smv1999" title="Code">💻</a></td>
71-
<td align="center"><a href="https://github.com/TawfikYasser"><img src="https://avatars.githubusercontent.com/u/54971231?s=400&u=0666d4ced1599a86cdb8d5bb817080ab2cbe22a0&v=4" width="100px;" alt=""/><br /><sub><b>TawfikYasser</b></sub></a><br /><a href="https://github.com/smv1999/CompetitiveProgrammingQuestionBank/commits?author=TawfikYasser" title="Code">💻</a></td>
72-
<td align="center"><a href="https://github.com/Ayush7614"><img src="https://avatars.githubusercontent.com/u/67006255?s=400&u=c0e16c3bba31328a028cfcca4b1fa7599509f905&v=4" width="100px;" alt=""/><br /><sub><b>Ayush7614</b></sub></a><br /><a href="https://github.com/smv1999/CompetitiveProgrammingQuestionBank/commits?author=Ayush7614" title="Code">💻</a></td>
73-
<td align="center"><a href="https://github.com/kiruba-r11"><img src="https://avatars.githubusercontent.com/u/76843281?s=400&u=e505d92cafc37670d23a8b51eb7d99777c46a84e&v=4" width="100px;" alt=""/><br /><sub><b>kiruba-r11</b></sub></a><br /><a href="https://github.com/smv1999/CompetitiveProgrammingQuestionBank/commits?author=kiruba-r11" title="Code">💻</a></td>
74-
<td align="center"><a href="https://github.com/nikita-jain-01"><img src="https://avatars.githubusercontent.com/u/72670446?s=400&u=608b2cb6bb50668db257a6d2a0c9138b53f5eb92&v=4" width="100px;" alt=""/><br /><sub><b>nikita-jain-01</b></sub></a><br /><a href="https://github.com/smv1999/CompetitiveProgrammingQuestionBank/commits?author=nikita-jain-01" title="Code">💻</a></td>
75-
<td align="center"><a href="https://github.com/Rishabh062"><img src="https://avatars.githubusercontent.com/u/57454462?s=400&u=00d039afe29ffad87e32135bc704a6c19aba9784&v=4" width="100px;" alt=""/><br /><sub><b>Rishabh062</b></sub></a><br /><a href="https://github.com/smv1999/CompetitiveProgrammingQuestionBank/commits?author=Rishabh062" title="Code">💻</a></td>
76-
<td align="center"><a href="https://github.com/SarthakKeshari"><img src="https://avatars.githubusercontent.com/u/55992140?v=4" width="100px;" alt=""/><br /><sub><b>SarthakKeshari</b></sub></a><br /><a href="https://github.com/smv1999/CompetitiveProgrammingQuestionBank/commits?author=SarthakKeshari" title="Code">💻</a></td>
77-
</tr>
78-
<tr>
79-
<td align="center"><a href="https://github.com/MannyP31"><img src="https://avatars.githubusercontent.com/u/76945004?v=4" width="100px;" alt=""/><br /><sub><b>MannyP31</b></sub></a><br /><a href="https://github.com/smv1999/CompetitiveProgrammingQuestionBank/commits?author=MannyP31" title="Code">💻</a></td>
80-
<td align="center"><a href="https://github.com/dsrao711"><img src="https://avatars.githubusercontent.com/u/59830064?v=4" width="100px;" alt=""/><br /><sub><b>dsrao711</b></sub></a><br /><a href="https://github.com/smv1999/CompetitiveProgrammingQuestionBank/commits?author=dsrao711" title="Code">💻</a></td>
81-
<td align="center"><a href="https://github.com/UG-SEP"><img src="https://avatars.githubusercontent.com/u/75884061?v=4" width="100px;" alt=""/><br /><sub><b>UG-SEP</b></sub></a><br /><a href="https://github.com/smv1999/CompetitiveProgrammingQuestionBank/commits?author=UG-SEP" title="Code">💻</a></td>
82-
<td align="center"><a href="https://github.com/gaurav24072002"><img src="https://avatars.githubusercontent.com/u/77109758?v=4" width="100px;" alt=""/><br /><sub><b>gaurav24072002</b></sub></a><br /><a href="https://github.com/smv1999/CompetitiveProgrammingQuestionBank/commits?author=gaurav24072002" title="Code">💻</a></td>
83-
<td align="center"><a href="https://github.com/version0chiro"><img src="https://avatars.githubusercontent.com/u/56084650?v=4" width="100px;" alt=""/><br /><sub><b>version0chiro</b></sub></a><br /><a href="https://github.com/smv1999/CompetitiveProgrammingQuestionBank/commits?author=version0chiro" title="Code">💻</a></td>
84-
<td align="center"><a href="https://github.com/Abhishek-072"><img src="https://avatars.githubusercontent.com/u/79994128?v=4" width="100px;" alt=""/><br /><sub><b>Abhishek-072</b></sub></a><br /><a href="https://github.com/smv1999/CompetitiveProgrammingQuestionBank/commits?author=Abhishek-072" title="Code">💻</a></td>
85-
<td align="center"><a href="https://github.com/jahnvisrivastava100"><img src="https://avatars.githubusercontent.com/u/60891187?v=4" width="100px;" alt=""/><br /><sub><b>jahnvisrivastava100</b></sub></a><br /><a href="https://github.com/smv1999/CompetitiveProgrammingQuestionBank/commits?author=jahnvisrivastava100" title="Code">💻</a></td>
86-
</tr>
87-
<tr>
88-
<td align="center"><a href="https://github.com/ManavArora26"><img src="https://avatars.githubusercontent.com/u/72567005?v=4" width="100px;" alt=""/><br /><sub><b>ManavArora26</b></sub></a><br /><a href="https://github.com/smv1999/CompetitiveProgrammingQuestionBank/commits?author=ManavArora26" title="Code">💻</a></td>
89-
<td align="center"><a href="https://github.com/Iamtripathisatyam"><img src="https://avatars.githubusercontent.com/u/69134468?v=4" width="100px;" alt=""/><br /><sub><b>Iamtripathisatyam</b></sub></a><br /><a href="https://github.com/smv1999/CompetitiveProgrammingQuestionBank/commits?author=Iamtripathisatyam" title="Code">💻</a></td>
90-
<td align="center"><a href="https://github.com/pri1311"><img src="https://avatars.githubusercontent.com/u/64613009?v=4" width="100px;" alt=""/><br /><sub><b>pri1311</b></sub></a><br /><a href="https://github.com/smv1999/CompetitiveProgrammingQuestionBank/commits?author=pri1311" title="Code">💻</a></td>
91-
</tr>
92-
</table>
65+
<a href="https://github.com/smv1999/CompetitiveProgrammingQuestionBank/graphs/contributors">
66+
<img src="https://contrib.rocks/image?repo=smv1999/CompetitiveProgrammingQuestionBank" />
67+
</a>
9368

9469
🚀 **Contributions** of any kind is welcome!
9570

0 commit comments

Comments
 (0)