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 3 files #92

Open
wants to merge 4 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
53 changes: 53 additions & 0 deletions General/SqrtWithPrec.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
//calculate sqrt using bitmasking

#include <iostream>
#include <cmath>

//this is our own created header file
#include "myHeader.cpp"

using namespace std;
const double epsilon = 1e-8; //equal to 100000000

double compUsingBS(double be, double en, double inc, double n) {
double mid;
while (be < en) {
mid = (be + en) / 2.0;
if (abs(mid * mid - n) < epsilon) return mid;
else if (mid * mid < n) {
be = mid + inc;
}
else {
en = mid - inc;
}
}
return mid - inc;
}


double mysqrt(int n, int precision) {
double be = 0;
double en = n;
double inc = 1;
double root = 0;

for (int p = 0; p <= precision; ++p) {
root = compUsingBS(be, en, inc, n);
be = root;4
en = root + inc;
inc = inc / 10.0;
}
return root;
}


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

double root = mysqrt(n, p);
cout << root;

return 0;

}
30 changes: 30 additions & 0 deletions Permutation.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
//programme for permutation of string
#include <iostream>
using namespace std;

//recurssive programme for computating permutation
void printPerm(char *input,int i){

if(input[i]=='\0'){
cout<<input<<endl;
return ;
}
for(int j=i;input[j]!='\0';j++){
swap(input[i],input[j]);
printPerm(input,i+1);

//swap again to maintain the order in the recurssive stack to get correct answer
swap(input[i],input[j]);
}

}

int main(){

//Input a string

char input[]="abcd";
printPerm(input,0);

return 0;
}
29 changes: 29 additions & 0 deletions Solutions to Known Problems/WineBottles.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
//Binary Search BOTTELES FOR MAXIMUM PROFIT

#include <iostream>
using namespace std;

int maxProfit(int arr[], int be, int en, int yr) { //O(2^n)
if (be > en) {
//no bottles left to sell
return 0; //0 is the maximum profit I can make by selling bottles on table
}

//lets sell the bottle at left
int leftProfit = yr * arr[be] + maxProfit(arr, be + 1, en, yr + 1);
int rightProfit = yr * arr[en] + maxProfit(arr, be, en - 1, yr + 1);
return max(leftProfit, rightProfit);


}


int main() {
int bottles[10];
int n;
cin >> n;
for (int i = 0; i < n; ++i) cin >> bottles[i];

int profit = maxProfit(bottles, 0, n - 1, 1);
cout << profit;
}
36 changes: 36 additions & 0 deletions Substring.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
#include<iostream>
using namespace std;

void printSubstrings(char *a){

// Use 2 loops for all values of i and j
for(int i=0;a[i]!='\0';i++){

//For every i, j will start from i and go till the end
for(int j=i;a[j]!='\0';j++){

//cout<<"{"<<i<<","<<j<<"}"<<" ";
//Print all the chars from i to j

for(int k =i;k<=j;k++){
cout<<a[k];
}
cout<<endl;
}

// Complexity of this code is O(n*n)
}

return ;
}

int main(){

char str[5] = "abcd";

//Substrings
printSubstrings(str);

return 0;

}