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

added extra long factorial solution #47

Open
wants to merge 5 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
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
30 changes: 30 additions & 0 deletions Algorithms/Implementation/3D_Surface_Area.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
#include <bits/stdc++.h>

using namespace std;

int main(int argc, char **args) {
int H, W;
cin >> H >> W;
int A[H+2][W+2];
//pad the matrix with zero reference height
for (int i=0; i <= H+1; i++) {
for (int j=0; j<= W+1; j++) {
if (i==0 || j==0 || i>H || j>W) A[i][j] = 0;
else cin >> A[i][j];

}
}
int area=2*H*W; //upper and lower area, 100>=H,W>=1
for (int i=1; i<=H; i++) {
for (int j=1; j <=W; j++) {
//sum the area along the height forward, and backward
area += std::max(0, A[i][j]-A[i][j-1]);
area += std::max(0, A[i][j]-A[i][j+1]);
//sum the area along the width forward, and backward
area += std::max(0, A[i][j]-A[i-1][j]);
area += std::max(0, A[i][j]-A[i+1][j]);
}
}
std::cout << area;
return 0;
}
218 changes: 109 additions & 109 deletions Algorithms/Implementation/Absolute Permutation.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -7,118 +7,118 @@
using namespace std;

// While this solution works, there is a much more elegant solution I found after reading the editorial.
// If N is even, then we simply need to consider N as a bunch 2*K slots. Solve the problem
// If N is even, then we simply need to consider N as a bunch 2*K slots. Solve the problem
// for each "slot" instead of solving for N directly.
int main() {
int T;
cin>>T;
while(T > 0) {
long long N, K;
cin>>N>>K;
int T;
cin>>T;
while(T > 0) {
long long N, K;
cin>>N>>K;

// Algorithm basically reduces down to either an edge case or an outside->inside approach
if(K == 0) {
for(long long i = 1; i <= N; i++) {
cout<<i<<" ";
}
cout<<"\n";
T--;
continue;
}
if(K > N/2 || N%2 == 1) {
cout<<"-1\n";
T--;
continue;
}
// Algorithm basically reduces down to either an edge case or an outside->inside approach
if(K == 0) {
for(long long i = 1; i <= N; i++) {
cout<<i<<" ";
}
cout<<"\n";
T--;
continue;
}
if(K > N/2 || N%2 == 1) {
cout<<"-1\n";
T--;
continue;
}

// *sigh* here we go...
bool possible = true;
bool *used = new bool[N+1];
long long *answer = new long long[N];
memset(used, false, sizeof(used));
for(long long i = 1; i <= N/2; i++) {
// Solve left index
if(i+K <= N && i-K >= 1) {
if(!used[i+K] && !used[i-K]) {
used[i-K] = true;
answer[i-1] = i-K;
} else if(!used[i+K] && used[i-K]) {
used[i+K] = true;
answer[i-1] = i+K;
} else if(used[i+K] && !used[i-K]) {
used[i-K] = true;
answer[i-1] = i-K;
} else {
possible = false;
break;
}
} else if(i+K > N && i-K >= 1) {
if(!used[i-K]) {
used[i-K] = true;
answer[i-1] = i-K;
} else {
possible = false;
break;
}
} else if(i+K <= N && i-K <= 1) {
if(!used[i+K]) {
used[i+K] = true;
answer[i-1] = i+K;
} else {
possible = false;
break;
}
} else {
possible = false;
break;
}
// Solve right index
long long rightIndex = N-i+1;
if(rightIndex+K <= N && rightIndex-K >= 1) {
if(!used[rightIndex+K] && !used[rightIndex-K]) {
used[rightIndex+K] = true;
answer[rightIndex-1] = rightIndex+K;
} else if(!used[rightIndex+K] && used[rightIndex-K]) {
used[rightIndex+K] = true;
answer[rightIndex-1] = rightIndex+K;
} else if(used[rightIndex+K] && !used[rightIndex-K]) {
used[rightIndex-K] = true;
answer[rightIndex-1] = rightIndex-K;
} else {
possible = false;
break;
}
} else if(rightIndex+K > N && rightIndex-K >= 1) {
if(!used[rightIndex-K]) {
used[rightIndex-K] = true;
answer[rightIndex-1] = rightIndex-K;
} else {
possible = false;
break;
}
} else if(rightIndex+K <= N && rightIndex-K <= 1) {
if(!used[rightIndex+K]) {
used[rightIndex+K] = true;
answer[rightIndex-1] = rightIndex+K;
} else {
possible = false;
break;
}
} else {
possible = false;
break;
}
}
// I can start to see why some people may not want to do this for a living...
if(!possible) {
cout<<"-1";
} else {
for(int i = 0; i < N; i++) {
cout<<answer[i]<<" ";
}
}
cout<<"\n";
T--;
}
// *sigh* here we go...
bool possible = true;
bool *used = new bool[N+1];
long long *answer = new long long[N];
memset(used, false, sizeof(used));
for(long long i = 1; i <= N/2; i++) {
// Solve left index
if(i+K <= N && i-K >= 1) {
if(!used[i+K] && !used[i-K]) {
used[i-K] = true;
answer[i-1] = i-K;
} else if(!used[i+K] && used[i-K]) {
used[i+K] = true;
answer[i-1] = i+K;
} else if(used[i+K] && !used[i-K]) {
used[i-K] = true;
answer[i-1] = i-K;
} else {
possible = false;
break;
}
} else if(i+K > N && i-K >= 1) {
if(!used[i-K]) {
used[i-K] = true;
answer[i-1] = i-K;
} else {
possible = false;
break;
}
} else if(i+K <= N && i-K <= 1) {
if(!used[i+K]) {
used[i+K] = true;
answer[i-1] = i+K;
} else {
possible = false;
break;
}
} else {
possible = false;
break;
}
// Solve right index
long long rightIndex = N-i+1;
if(rightIndex+K <= N && rightIndex-K >= 1) {
if(!used[rightIndex+K] && !used[rightIndex-K]) {
used[rightIndex+K] = true;
answer[rightIndex-1] = rightIndex+K;
} else if(!used[rightIndex+K] && used[rightIndex-K]) {
used[rightIndex+K] = true;
answer[rightIndex-1] = rightIndex+K;
} else if(used[rightIndex+K] && !used[rightIndex-K]) {
used[rightIndex-K] = true;
answer[rightIndex-1] = rightIndex-K;
} else {
possible = false;
break;
}
} else if(rightIndex+K > N && rightIndex-K >= 1) {
if(!used[rightIndex-K]) {
used[rightIndex-K] = true;
answer[rightIndex-1] = rightIndex-K;
} else {
possible = false;
break;
}
} else if(rightIndex+K <= N && rightIndex-K <= 1) {
if(!used[rightIndex+K]) {
used[rightIndex+K] = true;
answer[rightIndex-1] = rightIndex+K;
} else {
possible = false;
break;
}
} else {
possible = false;
break;
}
}
// I can start to see why some people may not want to do this for a living...
if(!possible) {
cout<<"-1";
} else {
for(int i = 0; i < N; i++) {
cout<<answer[i]<<" ";
}
}
cout<<"\n";
T--;
}
return 0;
}
39 changes: 39 additions & 0 deletions Algorithms/Implementation/Extra_Long_Factorials.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
/*
* complexity: 1/10*O(N^2)
* author: mohab metwally
*/
#include <vector>
#include <iostream>

using namespace std;

int main() {
int n;
cin >> n;

vector<int> bigint;
bigint.push_back(1);

for (int i=2; i <=n; i++) {
//(1) calculate factorial
for (auto it=bigint.begin(); it!=bigint.end(); it++) {
*it *=i;
}
//(2) move overflowing numbers (>=10) to new integer
for (int d=0; d<=bigint.size(); d++) {
//(3) detect over flow
if (bigint[d]<10) continue;

if (d==bigint.size()-1) bigint.push_back(0);

//(4) add overflow to subsequent int
bigint[d+1] += bigint[d]/10;
//(5) keep reminder to the current int
bigint[d]%= 10;
}
}
for (auto it = bigint.rbegin(); it!=bigint.rend(); it++) {
cout << *it;
}
return 0;
}
40 changes: 40 additions & 0 deletions Algorithms/Implementation/Larry's_Array.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
/*
* 1) A rotation does not change the parity of the number of inversions.
* 2) If the array is sortable, then the initial number of inversions is even.
* 3) If the initial number of inversions is even, then the array is sortable.
*/

#include <cmath>
#include <cstdio>
#include <vector>
#include <iostream>
#include <algorithm>
using namespace std;

int main() {

int t;
cin>>t;
for(int i=0;i<t;i++){
int n;
cin>>n;
vector<int>v(n);
for(int j=0;j<n;j++)
cin>>v[j];
int swaps=0;
for(int i=0;i<v.size();i++){
for(int j=0;j<v.size()-1;j++){
if(v[j]>v[j+1]){
swap(v[j],v[j+1]);
swaps++;
}
}
}
if(swaps%2==0)
cout<<"YES"<<endl;
else
cout<<"NO\n";

}
return 0;
}
14 changes: 7 additions & 7 deletions Algorithms/Implementation/Non-divisible Subset.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,11 @@
using namespace std;

// Algorithm basically boils down to cleverly applying modulo. We know that no 2 elements can be
// divisible by k, which also means the mod(k) of no two numbers can sum to k. For example with
// k = 10, we can't have two numbers where first_number%k = 1 and second_number%k = 9, since those
// would sum to a multiple k. Thus we'll either take all of the numbers whose value mod k is 1, or
// divisible by k, which also means the mod(k) of no two numbers can sum to k. For example with
// k = 10, we can't have two numbers where first_number%k = 1 and second_number%k = 9, since those
// would sum to a multiple k. Thus we'll either take all of the numbers whose value mod k is 1, or
// all of the numbers whose value mod k is 9, whichever is bigger. Don't forget that we can also
// include one multiple of k itself (since we only require that the SUM of any two numbers isn't
// include one multiple of k itself (since we only require that the SUM of any two numbers isn't
// divisible by k).

int main() {
Expand All @@ -20,7 +20,7 @@ int main() {
long long *modArr = new long long[k];
memset(modArr, 0, sizeof(modArr));
for(long long i = 0; i < n; i++) {
long long tmp;
long long tmp;
cin>>tmp;
modArr[tmp%k]++;
}
Expand All @@ -37,9 +37,9 @@ int main() {
if(k%2 == 0 && i == k/2 && modArr[k/2] > 0) {
result++;
} else {
result += tmp;
result += tmp;
}

}
cout<<result;
return 0;
Expand Down
Loading