Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Competitive programming #7

Merged
merged 47 commits into from
Nov 27, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
47 commits
Select commit Hold shift + click to select a range
a4ddae4
Create interpolation_sort.md
SB2318 Nov 8, 2021
5b71b94
Final update of interpolation_sort.md
SB2318 Nov 8, 2021
5c22b3b
This commit fixes issue #7806
anjalikumawat2002 Nov 13, 2021
665a31a
Merge two arrays according to constraints added.
priyankapiba Nov 16, 2021
c10a5aa
Update EOF Error.md
anjalikumawat2002 Nov 16, 2021
b831b8d
Merge pull request #7842 from anjalikumawat2002/Competitive_Programming
ErzaTitania-2001 Nov 17, 2021
e6c97f6
This commit fixes issue #8008
anjalikumawat2002 Nov 17, 2021
39808a0
N queens Added
SidharthMishra-07 Nov 18, 2021
cd5ec8a
Merge branch 'Competitive_Programming' of https://github.com/Sidharth…
SidharthMishra-07 Nov 18, 2021
5a3df57
Code Added
SidharthMishra-07 Nov 18, 2021
0bf7491
Image Added
SidharthMishra-07 Nov 18, 2021
6540218
Source Added
SidharthMishra-07 Nov 18, 2021
d238b9f
Update EOL Error.md
anjalikumawat2002 Nov 19, 2021
b85029d
Merge pull request #8010 from anjalikumawat2002/Competitive_Programming
ErzaTitania-2001 Nov 19, 2021
8dcb905
Competitive programming: Bucket Sort With Implementation in Python (#…
Jinal4502 Nov 19, 2021
072196b
Competitive Programming: Binomial Coefficient Using Bottom-Up Tabular…
Jinal4502 Nov 20, 2021
0896cac
This commit fixes issue #8116
anjalikumawat2002 Nov 20, 2021
19d9acc
Update AttributeError.md
anjalikumawat2002 Nov 20, 2021
7460e65
Update AttributeError.md
anjalikumawat2002 Nov 21, 2021
8084749
Rename .md file
anjalikumawat2002 Nov 21, 2021
b84a5bb
Change folder name
anjalikumawat2002 Nov 22, 2021
3689187
Merge pull request #8119 from anjalikumawat2002/Competitive_Programming
ErzaTitania-2001 Nov 22, 2021
022a34c
Merge pull request #7543 from SB2318/Competitive_Programming
ErzaTitania-2001 Nov 22, 2021
2ed971b
Merge branch 'girlscript:Competitive_Programming' into Competitive_Pr…
priyankapiba Nov 23, 2021
fe51df6
Create MagicNumber.md
Jinal4502 Nov 24, 2021
fe575d2
Update Competitive_Programming/C++/Recursion & Backtracking/Backtrack…
SidharthMishra-07 Nov 24, 2021
1b158a6
Update Competitive_Programming/C++/Recursion & Backtracking/Backtrack…
SidharthMishra-07 Nov 24, 2021
c1e905c
Merge pull request #4 from iam-sakshi/cp
iam-sakshi Nov 24, 2021
6ee8525
Cp 1 (#8208)
iam-sakshi Nov 25, 2021
d67be13
Delete TopKthFrequentElement.md
iam-sakshi Nov 25, 2021
9e2e4ab
Update EuclidAlgorithm.md
iam-sakshi Nov 25, 2021
b1bc9d4
Spiral Matrix Program (#7257)
yellowberard Nov 25, 2021
8a8f4ee
Merge pull request #8023 from SidharthMishra-07/Competitive_Programming
ErzaTitania-2001 Nov 25, 2021
8985737
Competitive programming: Greedy-Added CarSell problem (#8212)
subodh-kasaudhan Nov 26, 2021
2d9c394
Update EuclidAlgorithm.md
iam-sakshi Nov 26, 2021
638de22
Create ReverseWordInAString.md
iam-sakshi Nov 26, 2021
abfa9fa
Merge pull request #5 from iam-sakshi/CP_1
iam-sakshi Nov 26, 2021
8fa3c6f
Merge branch 'girlscript:Competitive_Programming' into Competitive_Pr…
priyankapiba Nov 26, 2021
01ec965
Rename MagicNumber.md to NthMagicNumber.md
Jinal4502 Nov 26, 2021
9a28d2b
header file and heading added.
priyankapiba Nov 26, 2021
2996298
Rename Competitive_Programming/Python/Maths & Number Theory/MagicNumb…
Jinal4502 Nov 26, 2021
190919b
Delete ReverseWordInAString.md
iam-sakshi Nov 26, 2021
5da4b33
Merge branch 'girlscript:Competitive_Programming' into Competitive_Pr…
iam-sakshi Nov 26, 2021
09f26b6
Create ReverseWordInAString.md (#6)
iam-sakshi Nov 26, 2021
02d22c7
Merge pull request #7956 from priyankapiba/Competitive_Programming
ErzaTitania-2001 Nov 27, 2021
009cd41
Merge pull request #8350 from Jinal4502/patch-3
ErzaTitania-2001 Nov 27, 2021
a0bdd9c
Merge pull request #7931 from iam-sakshi/Competitive_Programming
ErzaTitania-2001 Nov 27, 2021
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
106 changes: 106 additions & 0 deletions Competitive_Programming/C++/Array/Merge-two-arrays.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,106 @@
# Merge two arrays by satisfying given constraints
Given two sorted arrays X[] and Y[] of size m and n each where m >= n and X[] has exactly n vacant cells, merge elements of Y[] in their correct position in array X[], i.e., merge (X, Y) by keeping the sorted order.

For example,

## Input:

X[] = { 0, 2, 0, 3, 0, 5, 6, 0, 0 }
Y[] = { 1, 8, 9, 10, 15 }

The vacant cells in X[] is represented by 0

## Output:

X[] = { 1, 2, 3, 5, 6, 8, 9, 10, 15 }

## Approach:
The idea is simple – move non-empty elements of X[] at the beginning of X[] and then merge X[] with Y[] starting from the end. The merge process is similar to the merge routine of the merge sort algorithm.

## Code:
``` Cpp
#include<bits/stdc++.h

// Function to merge `X[0… m]` and `Y[0… n]` into `X[0… m+n+1]`
void merge(int X[], int Y[], int m, int n)
{
// size of `X[]` is `k+1`
int k = m + n + 1;

// run if X[] or Y[] has elements left
while (m >= 0 && n >= 0)
{
// put the next greater element in the next free position in `X[]` from the end
if (X[m] > Y[n]) {
X[k--] = X[m--];
}
else {
X[k--] = Y[n--];
}
}

// copy the remaining elements of `Y[]` (if any) to `X[]`
while (n >= 0) {
X[k--] = Y[n--];
}

// fill `Y[]` with all zeroes
for (int i = 0; i < n; i++) {
Y[i] = 0;
}
}

// The function moves non-empty elements in `X[]` in the
// beginning and then merge them with `Y[]`
void rearrange(int X[], int Y[], int m, int n)
{
// return if `X` is empty
if (m == 0) {
return;
}

// moves non-empty elements of `X[]` at the beginning
int k = 0;
for (int i = 0; i < m; i++)
{
if (X[i] != 0) {
X[k++] = X[i];
}
}

// merge `X[0 … k-1]` and `Y[0 … n-1]` into `X[0 … m-1]`
merge(X, Y, k - 1, n - 1);
}

int main()
{
// vacant cells in `X[]` is represented by 0
int X[] = { 0, 2, 0, 3, 0, 5, 6, 0, 0 };
int Y[] = { 1, 8, 9, 10, 15 };

int m = sizeof(X) / sizeof(X[0]);
int n = sizeof(Y) / sizeof(Y[0]);

/* Validate input before calling `rearrange()`
1. Both arrays `X[]` and `Y[]` should be sorted (ignore 0's in `X[]`)
2. Size of array `X[]` >= size of array `Y[]` (i.e., `m >= n`)
3. Total number of vacant cells in array `X[]` = size of array `Y[]` */

// merge `Y[]` into `X[]`
rearrange(X, Y, m, n);

// print merged array
for (int i = 0; i < m; i++) {
printf("%d ", X[i]);
}

return 0;
}
```

## Output:

1 2 3 5 6 8 9 10 15

## Time Complexity
Time complexity of the above solution is O(m + n) and runs in constant space. Here, m and n are the size of the first and second array, respectively.
58 changes: 58 additions & 0 deletions Competitive_Programming/C++/Greedy/CarSell_Problem/CarSell.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
# Car Sell Problem

## Problem Definition

MAXIMUM PROFIT:

A man owns N cars (numbered from 1 to N). He wishes to sell all cars over N days by selling exactly one car per day. For each valid i, the initial price of the i-th car is Pi. Due to depreciation, the price of each car decreases by 1 unit per day until it is sold. Note that the price of a car cannot drop below 0 no matter how many days have passed, i.e. when the price of a car reaches 0 on some day then it remains 0 in all subsequent days. Find the maximum profit he can make if he sells his cars in an optimal way in order to maximise his profit. The output can be very large so compute it to modulo of 1000000007(10^9+7).


## EXPLANATION
We use the greedy approach for solving this problem.
As we already know the price of any given car can never become negative. So, we will start by selling the car with the maximum price and then proceed to the car with a lower price.
We store the prices in ascending or descending order and then add prices accordingly until we get negative value for price of any car after depreciation on anyday.


## Example

### Input
```
5
2 4 5 6 3
```
### Output
```
12
```
### CODE πŸ’»
```cpp
#include <bits/stdc++.h>
using namespace std;
int main()
{
long long int n,k,i;
cin>>n;
k=0;
//create a vector to store price of cars
vector <long long int> v(n);
//storing prices of cars in v
for(i=0;i<n;i++)
{
cin>>v[i];
}
//we sort this vector using sort() function
sort(v.begin(),v.end());
//calculation of price of cars after depreciation and then adding their sum in k
for(i=n-1;i>=0;i--)
{
if(v[i]-n+1+i>0)k+=v[i]-n+1+i;
else break;
}
k=k%1000000007;
//printing maximum possible profit
cout<<k<<endl;
return 0;
}
```

## Time Complexity: N(Log N)
Original file line number Diff line number Diff line change
@@ -0,0 +1,99 @@
# Kth Largest Element


Find the kth largest element means that the output will be the kth largest element in the whole array.
<br>

eg : 8 3 5 9 1 2 5 6

<br>

if k = 2
<br>
output: 8

explanation:

the second largest in the array is 8 ,
sorted array: 1 2 3 5 5 6 8 9

similarily,
array[]= 8 3 5 9 1 2 5 6
if k=5

output: 3


The Brute Force method to find the kth largest element is to first sort and then find the k-1 index element. This will take O(n log n) time.

```c++
//c++
#include <bits/stdc++.h>
using namespace std;

int main ()
{
vector < int >v = { 6, 5, 2, 7, 1, 8, 4, 9, 10, 11 };
int k = 3;
sort (v.begin (), v.end ());
cout << "Kth largest element " << v[n - k];
return 0;
}
```
>OUTPUT:
>
>Kth largest element 9
>

>
>*Time Complexity*
>
>*O(n log n)*
where n is the size of the array/vector and sorting takes O(n log n).
Optimized way to solve this is to use min-heap, and this will take O(n log K) time.

- *The property of the min-heap structure is that the minimum element remains at the top*

- so since, we have to find only the kth largest element so after maintaining the heap of k size

- we can just pop the next top element after we have maintain the k size heap, as the top will always be smaller than the elements below it.

- since we iterate to each element in the array , so it takes O(n) time and for heap function it takes O(log k) ,
so the total complexity becomes O(n log k).


The answer will be at the top of the min heap.

```c++
//c++
#include <bits/stdc++.h>
using namespace std;

int findKthLargest(vector<int>nums, int k) {
priority_queue<int, vector<int>, greater<int>> minh;
for (auto i = nums.begin(); i != nums.end(); i++){
minh.push(*i);
if(minh.size()>k){
minh.pop();
}
}
return minh.top();
}
int main(){
vector<int> v={6,5,2,7,1,8,4,9,10,11};
int k=3;
int ele;
ele=findKthLargest(v,k);
cout<<"Kth largest element "<<ele;
return 0;
}
```
>OUTPUT:
>
>Kth largest element 9
>

>
>*Time Complexity*
>
>*O(n log k)*
Loading