├── 24_hour_time_travel.cpp ├── Balanced parantheses.cpp ├── FACTORIAL ├── Trailing zeroes of a factorial.cpp ├── distict digits of a factorial.cpp ├── factorial of a large number.cpp └── n! mod m.cpp ├── Fibonacci ├── DP.cpp ├── Matrix.cpp ├── Matrix_exponentiation.cpp └── recursive.cpp ├── KNAPSACK ├── README.md ├── ZERO ONE KNAPSACK dynamic programming complexity O(nc).cpp └── ZERO ONE KNAPSACK naive recursive solution complexity 2^n.cpp ├── LAB ├── FIFO.cpp ├── LRU.cpp ├── banker's algo.cpp ├── best fit.cpp ├── dining philosopher monitor.cpp ├── dining philosopher semaphore.cpp ├── first fit.cpp ├── non premtive sjf.cpp ├── optimal page replacement.cpp ├── preemtive sjf.cpp ├── priority scheduling.cpp ├── round robin.cpp └── worst fit.cpp ├── Operating Systems Lab └── FCFS.cpp ├── Primefactorization.cpp ├── README.md ├── Read ,Write and Append in a text file in c.c ├── Rootfinding-Methods-And-Least-Square-Methods ├── Least square.cpp ├── Readme.md ├── bisection3.cpp ├── falseposition2.cpp ├── generalized-newton.cpp ├── generalized.cpp ├── gn-modified.cpp ├── iteration.cpp ├── newton raphson.cpp ├── ramanujan.cpp ├── secant.cpp └── wighted least square.cpp ├── SHORT_NOTES_on_Euler's Totient Function.md ├── SHORT_NOTES_on_STL.md ├── Tower of Hanoi minimum cost DP.cpp ├── Tower of Hanoi.cpp ├── Tree Traversal.c ├── bubble sort.cpp ├── combinations nCr.cpp ├── insertsion sort.cpp ├── josephus o(n).cpp ├── josephus.cpp ├── matrix multiplication.cpp ├── permutation.cpp ├── quicksort.cpp ├── roman number.c ├── seive of eratosthenes(final).cpp ├── selection sort .cpp ├── spell.c └── transpose of a matrice.c /24_hour_time_travel.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | ///...................................*****................................................./// 3 | /// Author : Raihan Khan Raka ( raihankhanraka@gmail.com ) /// 4 | /// Department of Computer Science /// 5 | /// & Engineering /// 6 | /// Comilla University , Bangladesh. /// 7 | ///...................................*****................................................./// 8 | 9 | /*....................................Values................................................*/ 10 | #define inf 1<<30 11 | #define p5 100007 12 | #define p6 1000007 13 | #define PI acos(-1) 14 | #define M 1000000007 15 | 16 | /*....................................Functions.............................................*/ 17 | #define sqr(x) x*x 18 | #define sc scanf 19 | #define pf printf 20 | #define scin(x) sc("%d",&(x)) 21 | #define scin2(x,y) sc("d",&(x),&(y)) 22 | #define scin3(x,y,z) sc("d%d",&(x),&(y),&(z)) 23 | #define scln(x) sc("%lld",&(x)) 24 | #define min3(a,b,c) min(a,min(b,c)) 25 | #define max3(a,b,c) max(a,max(b,c)) 26 | #define all(v) v.begin(), v.end() 27 | #define ok cout << "ok" << endl 28 | #define mem(x,y) memset(x,y,sizeof(x)) 29 | #define READ(f) freopen(f, "r", stdin) 30 | #define WRITE(f) freopen(f, "w", stdout) 31 | 32 | /*...................................Data_Types............................................*/ 33 | #define lli long long int 34 | #define ull unsigned long long int 35 | #define pii pair < int, int> 36 | #define pll pair < ll, ll> 37 | #define veci vector 38 | #define vecl vector 39 | #define vecp vector< pair > 40 | #define mapstrint map< string , int > 41 | #define mapstrstr map< string , string > 42 | #define mapint map< int, int > 43 | #define uset unordered_set 44 | #define umap unordered_map 45 | #define pq priority_queue 46 | 47 | #define pb push_back 48 | #define mp make_pair 49 | #define ss stringstream 50 | 51 | /*.....................................Loops...............................................*/ 52 | #define rep( i , a , b ) for( i=a ; i=b ; i--) 54 | #define repx( i ,a,b, x) for( i=a ; i > v; 76 | 77 | string s,hour="" , minute="" , res; 78 | 79 | int i,len; 80 | 81 | cin >> s; 82 | len=s.length(); 83 | 84 | rep(i , 0 , len) 85 | { 86 | if(i<=1) hour+=s[i]; 87 | if(i>=3) minute+=s[i]; 88 | } 89 | 90 | //cout << hour << " //" << minute << endl; 91 | 92 | bool okk=true; 93 | 94 | while(okk) 95 | { 96 | if(minute[1]<='9') 97 | minute[1]++; 98 | 99 | if(minute[1]>'9') 100 | { 101 | minute[1]='0'; 102 | minute[0]++; 103 | } 104 | 105 | if(minute=="60") 106 | { 107 | minute="00"; 108 | hour[1]++; 109 | 110 | if(hour[1]>'9'){ 111 | hour[1]='0'; 112 | hour[0]++; 113 | } 114 | } 115 | 116 | 117 | if(hour=="24") hour="00" ; 118 | 119 | 120 | cout << hour << " " << minute << endl; 121 | 122 | } 123 | 124 | 125 | 126 | 127 | 128 | #ifdef HOME 129 | cerr << "Time elapsed: " << clock() / 1000 << " ms" << endl; 130 | #endif 131 | return 0; 132 | } 133 | 134 | -------------------------------------------------------------------------------- /Balanced parantheses.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #define PI acos(-1) 3 | #define min3(a,b,c) min(a,min(b,c)) 4 | #define max3(a,b,c) max(a,max(b,c)) 5 | #define READ(f) freopen(f, "r", stdin) 6 | #define WRITE(f) freopen(f, "w", stdout) 7 | #define lli long long int 8 | #define ull unsigned long long int 9 | #define pii pair < int, int> 10 | #define pll pair < ll, ll> 11 | #define sc scanf 12 | #define scin(x) sc("%d",&(x)) 13 | #define scln(x) sc("%lld",&(x)) 14 | #define pf printf 15 | #define ms(a,b) memset(a,b,sizeof(a)) 16 | #define veci vector 17 | #define vecl vector 18 | #define pb push_back 19 | long power(long int x, long int y){ int temp; if( y == 0) return 1; temp = power(x, y/2); if (y%2 == 0) return temp*temp; else return x*temp*temp; } 20 | /*lli gcd(lli x,lli y) 21 | { 22 | if(x==0) return y; 23 | return gcd(y%x,x); 24 | }*/ 25 | 26 | 27 | 28 | 29 | /* Your 30 | Code 31 | Starts 32 | Here 33 | */ 34 | 35 | 36 | using namespace std; 37 | 38 | void Parseparenthesis(string input) 39 | { 40 | stack s; 41 | char a, b, c; 42 | int line=0; 43 | 44 | for (int i=0; i 2 | #define PI acos(-1) 3 | #define min3(a,b,c) min(a,min(b,c)) 4 | #define max3(a,b,c) max(a,max(b,c)) 5 | #define READ(f) freopen(f, "r", stdin) 6 | #define WRITE(f) freopen(f, "w", stdout) 7 | #define lli long long int 8 | #define ull unsigned long long int 9 | #define pii pair < int, int> 10 | #define pll pair < ll, ll> 11 | #define sc scanf 12 | #define scin(x) sc("%d",&(x)) 13 | #define scln(x) sc("%lld",&(x)) 14 | #define pf printf 15 | #define ms(a,b) memset(a,b,sizeof(a)) 16 | #define veci vector 17 | #define vecl vector 18 | #define pb push_back 19 | long power(long int x, long int y){ int temp; if( y == 0) return 1; temp = power(x, y/2); if (y%2 == 0) return temp*temp; else return x*temp*temp; } 20 | /*lli gcd(lli x,lli y) 21 | { 22 | if(x==0) return y; 23 | return gcd(y%x,x); 24 | }*/ 25 | using namespace std; 26 | 27 | int main() 28 | { 29 | lli n,a,t,zero; 30 | scin(t); 31 | while(t--) 32 | { 33 | scln(n); 34 | a=5; 35 | zero=0; 36 | while(a<=n) 37 | { 38 | zero+=n/a; 39 | a=a*a; 40 | } 41 | cout << zero << endl; 42 | } 43 | 44 | 45 | return 0; 46 | } 47 | 48 | 49 | 50 | -------------------------------------------------------------------------------- /FACTORIAL/distict digits of a factorial.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #define PI acos(-1) 3 | #define min3(a,b,c) min(a,min(b,c)) 4 | #define max3(a,b,c) max(a,max(b,c)) 5 | #define READ(f) freopen(f, "r", stdin) 6 | #define WRITE(f) freopen(f, "w", stdout) 7 | #define lli long long int 8 | #define ull unsigned long long int 9 | #define pii pair < int, int> 10 | #define pll pair < ll, ll> 11 | #define sc scanf 12 | #define scin(x) sc("%d",&(x)) 13 | #define scln(x) sc("%lld",&(x)) 14 | #define pf printf 15 | #define ms(a,b) memset(a,b,sizeof(a)) 16 | #define veci vector 17 | #define vecl vector 18 | #define pb push_back 19 | long power(long int x, long int y){ int temp; if( y == 0) return 1; temp = power(x, y/2); if (y%2 == 0) return temp*temp; else return x*temp*temp; } 20 | /*lli gcd(lli x,lli y) 21 | { 22 | if(x==0) return y; 23 | return gcd(y%x,x); 24 | }*/ 25 | using namespace std; 26 | int multiply(int fact[],int fsize,int x) 27 | { 28 | int carry=0,temp,i; 29 | for(i=0;i=0;i--) 54 | digit[fact[i]]++; 55 | cout << n << "! --" << endl; 56 | for(i=0;i<10;i++) 57 | { 58 | if(i==5) cout << endl; 59 | cout << fixed << setw(5) << "(" << i << ")" << setw(5) << digit[i]; 60 | } 61 | cout << endl; 62 | } 63 | int main() 64 | { 65 | int n; 66 | while(scin(n)!=0) 67 | { 68 | factorial(n); 69 | } 70 | 71 | 72 | return 0; 73 | } 74 | 75 | 76 | 77 | 78 | -------------------------------------------------------------------------------- /FACTORIAL/factorial of a large number.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #define PI acos(-1) 3 | #define min3(a,b,c) min(a,min(b,c)) 4 | #define max3(a,b,c) max(a,max(b,c)) 5 | #define READ(f) freopen(f, "r", stdin) 6 | #define WRITE(f) freopen(f, "w", stdout) 7 | #define lli long long int 8 | #define ull unsigned long long int 9 | #define pii pair < int, int> 10 | #define pll pair < ll, ll> 11 | #define sc scanf 12 | #define scin(x) sc("%d",&(x)) 13 | #define scln(x) sc("%lld",&(x)) 14 | #define pf printf 15 | #define ms(a,b) memset(a,b,sizeof(a)) 16 | #define veci vector 17 | #define vecl vector 18 | #define pb push_back 19 | long power(long int x, long int y){ int temp; if( y == 0) return 1; temp = power(x, y/2); if (y%2 == 0) return temp*temp; else return x*temp*temp; } 20 | /*lli gcd(lli x,lli y) 21 | { 22 | if(x==0) return y; 23 | return gcd(y%x,x); 24 | }*/ 25 | using namespace std; 26 | int multiply(int fact[],int fsize,int x) 27 | { 28 | int carry=0,temp,i; 29 | for(i=0;i=0;i--) 52 | cout << fact[i] ; 53 | cout << endl; 54 | } 55 | int main() 56 | { 57 | int n,t; 58 | cin >> t ; 59 | while(t--) 60 | { 61 | cin >> n ; 62 | factorial(n); 63 | } 64 | 65 | 66 | return 0; 67 | } 68 | 69 | 70 | 71 | -------------------------------------------------------------------------------- /FACTORIAL/n! mod m.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #define PI acos(-1) 3 | #define min3(a,b,c) min(a,min(b,c)) 4 | #define max3(a,b,c) max(a,max(b,c)) 5 | #define READ(f) freopen(f, "r", stdin) 6 | #define WRITE(f) freopen(f, "w", stdout) 7 | #define lli long long int 8 | #define ull unsigned long long int 9 | #define pii pair < int, int> 10 | #define pll pair < ll, ll> 11 | #define sc scanf 12 | #define scin(x) sc("%d",&(x)) 13 | #define scln(x) sc("%lld",&(x)) 14 | #define pf printf 15 | #define ms(a,b) memset(a,b,sizeof(a)) 16 | #define veci vector 17 | #define vecl vector 18 | #define pb push_back 19 | long power(long int x, long int y){ int temp; if( y == 0) return 1; temp = power(x, y/2); if (y%2 == 0) return temp*temp; else return x*temp*temp; } 20 | /*lli gcd(lli x,lli y) 21 | { 22 | if(x==0) return y; 23 | return gcd(y%x,x); 24 | }*/ 25 | using namespace std; 26 | 27 | int main() 28 | { 29 | 30 | lli n,ans,i,m; 31 | while(scln(n)!= EOF) 32 | { 33 | scln(m); 34 | ans=1; 35 | for(i=1;i<=n;i++) 36 | { 37 | ans=ans*(i%m); 38 | } 39 | cout << ans%m << endl; 40 | } 41 | return 0; 42 | } 43 | 44 | 45 | 46 | -------------------------------------------------------------------------------- /Fibonacci/DP.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #define lli long long int 3 | #define veci vector 4 | #define vecl vector 5 | #define pb push_back 6 | 7 | using namespace std; 8 | inline lli fib(lli n) 9 | { 10 | lli i; 11 | vecl v; 12 | v.pb(0); 13 | v.pb(1); 14 | for(i=2;i<=n;i++) 15 | v.pb(v[i-1]+v[i-2]); 16 | return v[n]; 17 | } 18 | int main() 19 | { 20 | lli a; 21 | cin >> a ; 22 | cout << fib(a) << endl; 23 | return 0; 24 | } 25 | 26 | -------------------------------------------------------------------------------- /Fibonacci/Matrix.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #define lli long long int 3 | #define veci vector 4 | #define vecl vector 5 | #define pb push_back 6 | 7 | using namespace std; 8 | 9 | inline lli multiply(lli a[2][2],lli b[2][2]) 10 | { 11 | lli c,d,e,f; 12 | c=a[0][0]*b[0][0]+a[0][1]*b[1][0]; 13 | d=a[0][0]*b[0][1]+a[0][1]*b[1][1]; 14 | e=a[1][0]*b[0][0]+a[1][1]*b[1][0]; 15 | f=a[1][0]*b[0][1]+a[1][1]*b[1][1]; 16 | a[0][0]=c; 17 | a[0][1]=d; 18 | a[1][0]=e; 19 | a[1][1]=f; 20 | 21 | } 22 | inline lli power(lli a[2][2],int n) 23 | { 24 | lli i; 25 | lli b[2][2]={{1,1},{1,0}}; 26 | for(i=2;i<=n;i++) 27 | multiply(a,b); 28 | 29 | } 30 | inline lli fib(int n) 31 | { 32 | lli a[2][2]={{1,1},{1,0}}; 33 | if(n==0) return 0; 34 | else { 35 | power(a,n-1); 36 | return a[0][0]; 37 | } 38 | 39 | } 40 | int main() 41 | { 42 | int a=199; 43 | //cin >> a ; 44 | cout << fib(a) << endl; 45 | return 0; 46 | } 47 | 48 | 49 | -------------------------------------------------------------------------------- /Fibonacci/Matrix_exponentiation.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #define lli long long int 3 | #define veci vector 4 | #define vecl vector 5 | #define pb push_back 6 | 7 | using namespace std; 8 | 9 | inline lli multiply(lli a[2][2],lli b[2][2]) 10 | { 11 | lli c,d,e,f; 12 | c=a[0][0]*b[0][0]+a[0][1]*b[1][0]; 13 | d=a[0][0]*b[0][1]+a[0][1]*b[1][1]; 14 | e=a[1][0]*b[0][0]+a[1][1]*b[1][0]; 15 | f=a[1][0]*b[0][1]+a[1][1]*b[1][1]; 16 | a[0][0]=c; 17 | a[0][1]=d; 18 | a[1][0]=e; 19 | a[1][1]=f; 20 | 21 | } 22 | inline lli power(lli a[2][2],int n) 23 | { 24 | lli i; 25 | lli b[2][2]={{1,1},{1,0}}; 26 | if(n==0 or n==1) return a[0][0]; 27 | power(a,n/2); 28 | multiply(a,a); 29 | 30 | if(n%2==1) multiply(a,b); 31 | 32 | } 33 | inline lli fib(int n) 34 | { 35 | lli a[2][2]={{1,1},{1,0}}; 36 | if(n==0 or n==1) return n; 37 | else { 38 | power(a,n-1); 39 | return a[0][0]; 40 | } 41 | 42 | } 43 | int main() 44 | { 45 | int a=199; 46 | // cin >> a ; 47 | cout << fib(a) << endl; 48 | return 0; 49 | } 50 | 51 | 52 | 53 | -------------------------------------------------------------------------------- /Fibonacci/recursive.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #define lli long long int 3 | using namespace std; 4 | inline lli fib(lli n) 5 | { 6 | if(n<=1) 7 | return n; 8 | else return fib(n-1)+fib(n-2); 9 | } 10 | int main() 11 | { 12 | lli a; 13 | cin >> a ; 14 | cout << fib(a) << endl; 15 | return 0; 16 | } 17 | -------------------------------------------------------------------------------- /KNAPSACK/README.md: -------------------------------------------------------------------------------- 1 | KNAPSACK.. 2 | 3 | Following link will help understand the source Codes... 4 | 5 | For Zero One Knapsack: 6 | 7 | https://www.youtube.com/watch?v=xOlhR_2QCXY 8 | -------------------------------------------------------------------------------- /KNAPSACK/ZERO ONE KNAPSACK dynamic programming complexity O(nc).cpp: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | #define PI acos(-1) 4 | #define min3(a,b,c) min(a,min(b,c)) 5 | #define max3(a,b,c) max(a,max(b,c)) 6 | #define READ(f) freopen(f, "r", stdin) 7 | #define WRITE(f) freopen(f, "w", stdout) 8 | #define lli long long int 9 | #define ull unsigned long long int 10 | #define pii pair < int, int> 11 | #define pll pair < ll, ll> 12 | #define sc scanf 13 | #define scin(x) sc("%d",&(x)) 14 | #define scln(x) sc("%lld",&(x)) 15 | #define pf printf 16 | #define veci vector 17 | #define vecl vector 18 | #define vecp vector< pair > 19 | #define pb push_back 20 | #define inf 1<<30 21 | #define mp make_pair 22 | #define ss stringstream 23 | #define all(v) v.begin(), v.end() 24 | #define mem(x,y) memset(x,y,sizeof(x)) 25 | #define FastRead ios_base::sync_with_stdio(0);cin.tie(0) 26 | #define M 1000000007 27 | //int month[]={31,28,31,30,31,30,31,31,30,31,30,31}; 28 | //long power(long int x, long int y){ int temp; if( y == 0) return 1; temp = power(x, y/2); if (y%2 == 0) return temp*temp; else return x*temp*temp; } 29 | /*lli gcd(lli x,lli y) 30 | { 31 | if(x==0) return y; 32 | return gcd(y%x,x); 33 | }*/ 34 | using namespace std; 35 | int weight[10005]; 36 | int cost[10005]; 37 | int dp[1000][10000]; 38 | int KNAPSACK(int n,int c) 39 | { 40 | int result; 41 | if(dp[n][c]!=-1) return dp[n][c]; 42 | if(n==0 or c==0) return 0; // base case 43 | 44 | else if (weight[n]>c) result = KNAPSACK(n-1,c); 45 | 46 | else 47 | { 48 | int tmp1=KNAPSACK(n-1,c); 49 | int tmp2=cost[n]+KNAPSACK(n-1,c-weight[n]); 50 | result = max(tmp1,tmp2); 51 | dp[n][c]=result; 52 | } 53 | return result; 54 | } 55 | 56 | int main() 57 | { 58 | int n,c,i,j,result; 59 | 60 | cin >> n ; 61 | for(i=1;i<=n;i++) cin >> weight [i]; 62 | for(i=1;i<=n;i++) cin >> cost [i]; 63 | 64 | cin >> c ; 65 | mem(dp,-1); 66 | result=KNAPSACK(n,c); 67 | 68 | cout << result << endl; 69 | 70 | return 0; 71 | } 72 | 73 | -------------------------------------------------------------------------------- /KNAPSACK/ZERO ONE KNAPSACK naive recursive solution complexity 2^n.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | #define PI acos(-1) 4 | #define min3(a,b,c) min(a,min(b,c)) 5 | #define max3(a,b,c) max(a,max(b,c)) 6 | #define READ(f) freopen(f, "r", stdin) 7 | #define WRITE(f) freopen(f, "w", stdout) 8 | #define lli long long int 9 | #define ull unsigned long long int 10 | #define pii pair < int, int> 11 | #define pll pair < ll, ll> 12 | #define sc scanf 13 | #define scin(x) sc("%d",&(x)) 14 | #define scln(x) sc("%lld",&(x)) 15 | #define pf printf 16 | #define veci vector 17 | #define vecl vector 18 | #define vecp vector< pair > 19 | #define pb push_back 20 | #define inf 1<<30 21 | #define mp make_pair 22 | #define ss stringstream 23 | #define all(v) v.begin(), v.end() 24 | #define mem(x,y) memset(x,y,sizeof(x)) 25 | #define FastRead ios_base::sync_with_stdio(0);cin.tie(0) 26 | #define M 1000000007 27 | //int month[]={31,28,31,30,31,30,31,31,30,31,30,31}; 28 | //long power(long int x, long int y){ int temp; if( y == 0) return 1; temp = power(x, y/2); if (y%2 == 0) return temp*temp; else return x*temp*temp; } 29 | /*lli gcd(lli x,lli y) 30 | { 31 | if(x==0) return y; 32 | return gcd(y%x,x); 33 | }*/ 34 | using namespace std; 35 | int weight[100005]; 36 | int cost[100005]; 37 | 38 | int KNAPSACK(int n,int c) 39 | { 40 | int result; 41 | if(n==0 or c==0) return 0; // base case 42 | 43 | else if (weight[n]>c) result = KNAPSACK(n-1,c); 44 | 45 | else 46 | { 47 | int tmp1=KNAPSACK(n-1,c); 48 | int tmp2=cost[n]+KNAPSACK(n-1,c-weight[n]); 49 | result = max(tmp1,tmp2); 50 | } 51 | return result; 52 | } 53 | 54 | int main() 55 | { 56 | int n,c,i,j,result; 57 | 58 | cin >> n ; 59 | for(i=1;i<=n;i++) cin >> weight [i]; 60 | for(i=1;i<=n;i++) cin >> cost [i]; 61 | 62 | cin >> c ; 63 | 64 | result=KNAPSACK(n,c); 65 | 66 | cout << result << endl; 67 | 68 | return 0; 69 | } 70 | -------------------------------------------------------------------------------- /LAB/FIFO.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | int main() 5 | { 6 | int i,j,n,a[50],frame[10],no,k,hit,fault=0; 7 | printf("Enter the number of pages :\n"); 8 | scanf("%d",&n); 9 | 10 | printf("Enter the reference string :\n"); 11 | for(i=1; i<=n; i++) 12 | scanf("%d",&a[i]); 13 | 14 | printf("Enter the number of frames :"); 15 | scanf("%d",&no); 16 | 17 | for(i=0; i 2 | using namespace std; 3 | 4 | int main() 5 | { 6 | int q[20],p[50],c=0,c1,d,f,i,j,k=0,n,r,t,b[20],c2[20]; 7 | 8 | printf("Enter no of pages:"); 9 | scanf("%d",&n); 10 | printf("Enter the reference string:"); 11 | 12 | for(i=0; i 2 | int current[5][5], maximum_claim[5][5], available[5]; 3 | int allocation[5] = {0, 0, 0, 0, 0}; 4 | int maxres[5], running[5], safe = 0; 5 | int counter = 0, i, j, exec, resources, processes, k = 1; 6 | 7 | int main() 8 | { 9 | printf("\nEnter number of processes: "); 10 | scanf("%d", &processes); 11 | 12 | for (i = 0; i < processes; i++) 13 | { 14 | running[i] = 1; 15 | counter++; 16 | } 17 | 18 | printf("\nEnter number of resources: "); 19 | scanf("%d", &resources); 20 | 21 | printf("\nEnter Claim Vector:"); 22 | for (i = 0; i < resources; i++) 23 | { 24 | scanf("%d", &maxres[i]); 25 | } 26 | 27 | printf("\nEnter Allocated Resource Table:\n"); 28 | for (i = 0; i < processes; i++) 29 | { 30 | for(j = 0; j < resources; j++) 31 | { 32 | scanf("%d", ¤t[i][j]); 33 | } 34 | } 35 | 36 | printf("\nEnter Maximum Claim Table:\n"); 37 | for (i = 0; i < processes; i++) 38 | { 39 | for(j = 0; j < resources; j++) 40 | { 41 | scanf("%d", &maximum_claim[i][j]); 42 | } 43 | } 44 | 45 | printf("\nThe Claim Vector is: "); 46 | for (i = 0; i < resources; i++) 47 | { 48 | printf("\t%d", maxres[i]); 49 | } 50 | 51 | printf("\nThe Allocated Resource Table:\n"); 52 | for (i = 0; i < processes; i++) 53 | { 54 | for (j = 0; j < resources; j++) 55 | { 56 | printf("\t%d", current[i][j]); 57 | } 58 | printf("\n"); 59 | } 60 | 61 | printf("\nThe Maximum Claim Table:\n"); 62 | for (i = 0; i < processes; i++) 63 | { 64 | for (j = 0; j < resources; j++) 65 | { 66 | printf("\t%d", maximum_claim[i][j]); 67 | } 68 | printf("\n"); 69 | } 70 | 71 | for (i = 0; i < processes; i++) 72 | { 73 | for (j = 0; j < resources; j++) 74 | { 75 | allocation[j] += current[i][j]; 76 | } 77 | } 78 | 79 | printf("\nAllocated resources:"); 80 | for (i = 0; i < resources; i++) 81 | { 82 | printf("\t%d", allocation[i]); 83 | } 84 | 85 | for (i = 0; i < resources; i++) 86 | { 87 | available[i] = maxres[i] - allocation[i]; 88 | } 89 | 90 | printf("\nAvailable resources:"); 91 | for (i = 0; i < resources; i++) 92 | { 93 | printf("\t%d", available[i]); 94 | } 95 | printf("\n"); 96 | 97 | while (counter != 0) 98 | { 99 | safe = 0; 100 | for (i = 0; i < processes; i++) 101 | { 102 | if (running[i]) 103 | { 104 | exec = 1; 105 | for (j = 0; j < resources; j++) 106 | { 107 | if (maximum_claim[i][j] - current[i][j] > available[j]) 108 | { 109 | exec = 0; 110 | break; 111 | } 112 | } 113 | if (exec) 114 | { 115 | printf("\nProcess%d is executing\n", i + 1); 116 | running[i] = 0; 117 | counter--; 118 | safe = 1; 119 | 120 | for (j = 0; j < resources; j++) 121 | { 122 | available[j] += current[i][j]; 123 | } 124 | break; 125 | } 126 | } 127 | } 128 | if (!safe) 129 | { 130 | printf("\nThe processes are in unsafe state.\n"); 131 | break; 132 | } 133 | else 134 | { 135 | printf("\nThe process is in safe state"); 136 | printf("\nAvailable vector:"); 137 | 138 | for (i = 0; i < resources; i++) 139 | { 140 | printf("\t%d", available[i]); 141 | } 142 | 143 | printf("\n"); 144 | } 145 | } 146 | return 0; 147 | } 148 | 149 | -------------------------------------------------------------------------------- /LAB/best fit.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | int main() 4 | { 5 | int a[20],p[20],i,j,n,m; 6 | printf("Enter no of Blocks.\n"); 7 | scanf("%d",&n); 8 | for(i=0; i 2 | using namespace std; 3 | #define hungry 100 4 | #define eating 200 5 | #define thinking 300 6 | #define wait 400 7 | #define signal 500 8 | enum {philosopher1,philosopher2,philosopher3,philosopher4,philosopher5}; 9 | class monitor 10 | { 11 | int state[5]; 12 | int self[5]; 13 | public: 14 | Pickup(int i) 15 | { 16 | 17 | state[i] = hungry; 18 | cout<<"philosopher "< 2 | 3 | int p[6][2] = {-1,-1,-1,-1,-1,-1,-1,-1,-1,-1}; 4 | int chopstickState[5] = {0,0,0,0,0}; 5 | int State[5] = {0,0,0,0,0}; 6 | 7 | void wait(int chopstick, int philosopher) 8 | { 9 | chopstickState[chopstick] = 1; 10 | if(chopstick == philosopher) p[philosopher][1] = chopstick; 11 | else p[philosopher][0] = chopstick; 12 | } 13 | 14 | void signal(int philosopher) 15 | { 16 | chopstickState[p[philosopher][1]] = 0; 17 | chopstickState[p[philosopher][0]] = 0; 18 | p[philosopher][1] = -1; 19 | p[philosopher][0] = -1; 20 | } 21 | 22 | void reset(int x) 23 | { 24 | chopstickState[x] = 0; 25 | p[x][1] = -1; 26 | p[x][0] = -1; 27 | State[x] = 0; 28 | } 29 | 30 | int main() 31 | { 32 | int eatcomplete = 0; 33 | int philosopher = 0; 34 | int chopstick; 35 | int count = 0; 36 | do 37 | { 38 | if(philosopher % 2 != 0 && State[philosopher] < 2) 39 | { 40 | if(State[philosopher] == 0) 41 | { 42 | chopstick = philosopher; 43 | if(chopstickState[chopstick] == 0) 44 | { 45 | wait(chopstick, philosopher); 46 | State[philosopher] = 1; 47 | printf("Philosopher %d is taking %dth Chopstick\n", philosopher+1, chopstick+1); 48 | } 49 | } 50 | else if(State[philosopher] == 1) 51 | { 52 | chopstick = philosopher - 1; 53 | if(chopstick < 0) chopstick = 4; 54 | if(chopstickState[chopstick] == 0) 55 | { 56 | wait(chopstick, philosopher); 57 | State[philosopher] = 2; 58 | printf("Philosopher %d is taking %dth Chopstick\n", philosopher+1, chopstick+1); 59 | printf("-------------------------------------Philosopher %d is eating\n",philosopher+1); 60 | eatcomplete++; 61 | signal(philosopher); 62 | } 63 | } 64 | } 65 | else if(philosopher % 2 == 0 && State[philosopher] < 2) 66 | { 67 | if(State[philosopher] == 0) 68 | { 69 | chopstick = philosopher - 1; 70 | if(chopstick < 0) chopstick = 4; 71 | if(chopstickState[chopstick] == 0) 72 | { 73 | wait(chopstick, philosopher); 74 | State[philosopher] = 1; 75 | printf("Philosopher %d is taking %dth Chopstick\n", philosopher+1, chopstick+1); 76 | } 77 | } 78 | else if(State[philosopher] == 1) 79 | { 80 | chopstick = philosopher; 81 | if(chopstickState[chopstick] == 0) 82 | { 83 | wait(chopstick, philosopher); 84 | State[philosopher] = 2; 85 | printf("Philosopher %d is taking %dth Chopstick\n", philosopher+1, chopstick+1); 86 | printf("-------------------------------------Philosopher %d is eating\n",philosopher+1); 87 | eatcomplete++; 88 | signal(philosopher); 89 | } 90 | } 91 | } 92 | philosopher = (philosopher + 1) % 5; 93 | count++; 94 | } 95 | while(eatcomplete < 5); 96 | int i; 97 | for(i = 0; i < 5; i++) reset(i); 98 | eatcomplete = 0; 99 | return 0; 100 | } 101 | 102 | 103 | -------------------------------------------------------------------------------- /LAB/first fit.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | int main() 4 | { 5 | int bsize[10], psize[10], bno, pno, flags[10], allocation[10], i, j; 6 | 7 | for(i = 0; i < 10; i++) 8 | { 9 | flags[i] = 0; 10 | allocation[i] = -1; 11 | } 12 | printf("Enter no. of blocks: "); 13 | scanf("%d", &bno); 14 | printf("\nEnter size of each block: "); 15 | for(i = 0; i < bno; i++) 16 | scanf("%d", &bsize[i]); 17 | 18 | printf("\nEnter no. of processes: "); 19 | scanf("%d", &pno); 20 | printf("\nEnter size of each process: "); 21 | for(i = 0; i < pno; i++) 22 | scanf("%d", &psize[i]); 23 | for(i = 0; i < pno; i++) //allocation as per first fit 24 | for(j = 0; j < bno; j++) 25 | if(flags[j] == 0 && bsize[j] >= psize[i]) 26 | { 27 | allocation[j] = i; 28 | flags[j] = 1; 29 | break; 30 | } 31 | //display allocation details 32 | printf("\nBlock no.\tsize\t\tprocess no.\t\tsize"); 33 | for(i = 0; i < bno; i++) 34 | { 35 | printf("\n%d\t\t%d\t\t", i+1, bsize[i]); 36 | if(flags[i] == 1) 37 | printf("%d\t\t\t%d",allocation[i]+1,psize[allocation[i]]); 38 | 39 | 40 | 41 | 42 | 43 | 44 | else 45 | printf("Not allocated"); 46 | } 47 | } 48 | 49 | -------------------------------------------------------------------------------- /LAB/non premtive sjf.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | int main() 4 | { 5 | int bt[20],p[20],wt[20],tat[20],i,j,n,total=0,pos,temp; 6 | float avg_wt,avg_tat; 7 | printf("Enter number of process:"); 8 | scanf("%d",&n); 9 | 10 | printf("\nEnter Burst Time:\n"); 11 | for(i=0; i 2 | 3 | int main() 4 | { 5 | int no_of_frames, no_of_pages, frames[10], pages[30], temp[10], flag1, flag2, flag3, i, j, k, pos, max, faults = 0; 6 | printf("Enter number of frames: "); 7 | scanf("%d", &no_of_frames); 8 | 9 | printf("Enter number of pages: "); 10 | scanf("%d", &no_of_pages); 11 | 12 | printf("Enter page reference string: "); 13 | 14 | for(i = 0; i < no_of_pages; ++i) 15 | { 16 | scanf("%d", &pages[i]); 17 | } 18 | 19 | for(i = 0; i < no_of_frames; ++i) 20 | { 21 | frames[i] = -1; 22 | } 23 | 24 | for(i = 0; i < no_of_pages; ++i) 25 | { 26 | flag1 = flag2 = 0; 27 | 28 | for(j = 0; j < no_of_frames; ++j) 29 | { 30 | if(frames[j] == pages[i]) 31 | { 32 | flag1 = flag2 = 1; 33 | break; 34 | } 35 | } 36 | 37 | if(flag1 == 0) 38 | { 39 | for(j = 0; j < no_of_frames; ++j) 40 | { 41 | if(frames[j] == -1) 42 | { 43 | faults++; 44 | frames[j] = pages[i]; 45 | flag2 = 1; 46 | break; 47 | } 48 | } 49 | } 50 | 51 | if(flag2 == 0) 52 | { 53 | flag3 =0; 54 | 55 | for(j = 0; j < no_of_frames; ++j) 56 | { 57 | temp[j] = -1; 58 | 59 | for(k = i + 1; k < no_of_pages; ++k) 60 | { 61 | if(frames[j] == pages[k]) 62 | { 63 | temp[j] = k; 64 | break; 65 | } 66 | } 67 | } 68 | 69 | for(j = 0; j < no_of_frames; ++j) 70 | { 71 | if(temp[j] == -1) 72 | { 73 | pos = j; 74 | flag3 = 1; 75 | break; 76 | } 77 | } 78 | 79 | if(flag3 ==0) 80 | { 81 | max = temp[0]; 82 | pos = 0; 83 | 84 | for(j = 1; j < no_of_frames; ++j) 85 | { 86 | if(temp[j] > max) 87 | { 88 | max = temp[j]; 89 | pos = j; 90 | } 91 | } 92 | } 93 | frames[pos] = pages[i]; 94 | faults++; 95 | } 96 | 97 | printf("\n"); 98 | 99 | for(j = 0; j < no_of_frames; ++j) 100 | { 101 | printf("%d\t", frames[j]); 102 | } 103 | } 104 | 105 | printf("\n\nTotal Page Faults = %d", faults); 106 | 107 | return 0; 108 | } 109 | -------------------------------------------------------------------------------- /LAB/preemtive sjf.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | int main() 4 | { 5 | int arrival_time[10], burst_time[10], temp[10]; 6 | int i, smallest, count = 0, time, limit; 7 | double wait_time = 0, turnaround_time = 0, end; 8 | float average_waiting_time, average_turnaround_time; 9 | printf("\nEnter the Total Number of Processes:\t"); 10 | 11 | 12 | 13 | 14 | 15 | scanf("%d", &limit); 16 | printf("\nEnter Details of %d Processes\n", limit); 17 | for(i = 0; i < limit; i++) 18 | { 19 | printf("\nEnter Arrival Time:\t"); 20 | scanf("%d", &arrival_time[i]); 21 | printf("Enter Burst Time:\t"); 22 | scanf("%d", &burst_time[i]); 23 | temp[i] = burst_time[i]; 24 | } 25 | burst_time[9] = 9999; 26 | for(time = 0; count != limit; time++) 27 | { 28 | smallest = 9; 29 | for(i = 0; i < limit; i++) 30 | { 31 | if(arrival_time[i] <= time && burst_time[i] < burst_time[smallest] && burst_time[i] > 0) 32 | { 33 | smallest = i; 34 | } 35 | } 36 | burst_time[smallest]--; 37 | if(burst_time[smallest] == 0) 38 | { 39 | count++; 40 | end = time + 1; 41 | wait_time = wait_time + end - arrival_time[smallest] - temp[smallest]; 42 | turnaround_time = turnaround_time + end - arrival_time[smallest]; 43 | } 44 | } 45 | average_waiting_time = wait_time / limit; 46 | average_turnaround_time = turnaround_time / limit; 47 | printf("\n\nAverage Waiting Time:\t%lf\n", average_waiting_time); 48 | printf("Average Turnaround Time:\t%lf\n", average_turnaround_time); 49 | return 0; 50 | } 51 | 52 | -------------------------------------------------------------------------------- /LAB/priority scheduling.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | int main() 4 | { 5 | int bt[20],p[20],wt[20],tat[20],pr[20],i,j,n,total=0,pos,temp,avg_wt,avg_tat; 6 | printf("Enter Total Number of Process:"); 7 | scanf("%d",&n); 8 | 9 | printf("\nEnter Burst Time and Priority\n"); 10 | for(i=0; i 2 | 3 | int main() 4 | { 5 | 6 | int count,j,n,time,remain,flag=0,time_quantum; 7 | int wait_time=0,turnaround_time=0,at[10],bt[10],rt[10]; 8 | printf("Enter Total Process:\t "); 9 | scanf("%d",&n); 10 | remain=n; 11 | for(count=0; count0) 24 | { 25 | time+=rt[count]; 26 | rt[count]=0; 27 | flag=1; 28 | } 29 | else if(rt[count]>0) 30 | { 31 | rt[count]-=time_quantum; 32 | time+=time_quantum; 33 | } 34 | if(rt[count]==0 && flag==1) 35 | { 36 | remain--; 37 | printf("P[%d]\t|\t%d\t|\t%d\n",count+1,time-at[count],time-at[count]-bt[count]); 38 | wait_time+=time-at[count]-bt[count]; 39 | 40 | 41 | 42 | 43 | 44 | turnaround_time+=time-at[count]; 45 | flag=0; 46 | } 47 | if(count==n-1) 48 | count=0; 49 | else if(at[count+1]<=time) 50 | count++; 51 | else 52 | count=0; 53 | } 54 | printf("\nAverage Waiting Time= %f\n",wait_time*1.0/n); 55 | printf("Avg Turnaround Time = %f",turnaround_time*1.0/n); 56 | 57 | return 0; 58 | } 59 | 60 | -------------------------------------------------------------------------------- /LAB/worst fit.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | int main() 4 | { 5 | int fragments[10], blocks[10], files[10]; 6 | int m, n, number_of_blocks, number_of_files, temp, top = 0; 7 | static int block_arr[10], file_arr[10]; 8 | printf("\nEnter the Total Number of Blocks:\t"); 9 | scanf("%d",&number_of_blocks); 10 | printf("Enter the Total Number of Files:\t"); 11 | scanf("%d",&number_of_files); 12 | printf("\nEnter the Size of the Blocks:\n"); 13 | for(m = 0; m < number_of_blocks; m++) 14 | { 15 | printf("Block No.[%d]:\t", m + 1); 16 | scanf("%d", &blocks[m]); 17 | } 18 | printf("Enter the Size of the Files:\n"); 19 | for(m = 0; m < number_of_files; m++) 20 | { 21 | printf("File No.[%d]:\t", m + 1); 22 | scanf("%d", &files[m]); 23 | } 24 | for(m = 0; m < number_of_files; m++) 25 | { 26 | for(n = 0; n < number_of_blocks; n++) 27 | { 28 | if(block_arr[n] != 1) 29 | { 30 | temp = blocks[n] - files[m]; 31 | if(temp >= 0) 32 | { 33 | if(top < temp) 34 | { 35 | file_arr[m] = n; 36 | top = temp; 37 | } 38 | } 39 | } 40 | fragments[m] = top; 41 | block_arr[file_arr[m]] = 1; 42 | top = 0; 43 | } 44 | } 45 | printf("\nFile Number\tFile Size\tBlock Number\tBlock Size\tFragment"); 46 | for(m = 0; m < number_of_files; m++) 47 | { 48 | printf("\n%d\t\t%d\t\t%d\t\t%d\t\t%d", m, files[m], file_arr[m], blocks[file_arr[m]], fragments[m]); 49 | } 50 | printf("\n"); 51 | return 0; 52 | } 53 | 54 | -------------------------------------------------------------------------------- /Operating Systems Lab/FCFS.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | int main() 4 | { 5 | int n,bt[20],wt[20],tat[20],avwt=0,avtat=0,i,j; 6 | printf("Enter total number of processes(maximum 20):"); 7 | scanf("%d",&n); 8 | 9 | printf("\nEnter Process Burst Time\n"); 10 | for(i=0; i 2 | ///...................................*****................................................./// 3 | /// Author : Raihan Khan Raka ( raihankhanraka@gmail.com ) /// 4 | /// Department of Computer Science /// 5 | /// & Engineering /// 6 | /// Comilla University , Bangladesh. /// 7 | ///...................................*****................................................./// 8 | 9 | /*....................................Values................................................*/ 10 | #define inf 1<<30 11 | #define p5 100007 12 | #define p6 1000007 13 | #define PI acos(-1) 14 | #define M 1000000007 15 | 16 | /*....................................Functions.............................................*/ 17 | #define sqr(x) x*x 18 | #define sc scanf 19 | #define pf printf 20 | #define scin(x) sc("%d",&(x)) 21 | #define scin2(x,y) sc("d",&(x),&(y)) 22 | #define scin3(x,y,z) sc("d%d",&(x),&(y),&(z)) 23 | #define scln(x) sc("%lld",&(x)) 24 | #define min3(a,b,c) min(a,min(b,c)) 25 | #define max3(a,b,c) max(a,max(b,c)) 26 | #define all(v) v.begin(), v.end() 27 | #define ok cout << "ok" << endl 28 | #define mem(x,y) memset(x,y,sizeof(x)) 29 | #define READ(f) freopen(f, "r", stdin) 30 | #define WRITE(f) freopen(f, "w", stdout) 31 | 32 | /*...................................Data_Types............................................*/ 33 | #define lli long long int 34 | #define ull unsigned long long int 35 | #define pii pair < int, int> 36 | #define pll pair < ll, ll> 37 | #define veci vector 38 | #define vecl vector 39 | #define vecp vector< pair > 40 | #define mapstrint map< string , int > 41 | #define mapstrstr map< string , string > 42 | #define mapint map< int, int > 43 | #define uset unordered_set 44 | #define umap unordered_map 45 | #define pq priority_queue 46 | 47 | #define pb push_back 48 | #define mp make_pair 49 | #define ss stringstream 50 | 51 | /*.....................................Loops...............................................*/ 52 | #define rep( i , a , b ) for( i=a ; i=b ; i--) 54 | #define repx( i ,a,b, x) for( i=a ; i>1; 82 | 83 | cnt++; 84 | } 85 | //cout << cnt << endl; 86 | for(i=3; i*i<=n; i+=2) 87 | { 88 | if(n%i==0) 89 | { 90 | cnt++; 91 | while(n%i==0) 92 | n=n/i; 93 | } 94 | } 95 | 96 | if(n>2) 97 | { 98 | cnt++; 99 | } 100 | 101 | return cnt; 102 | 103 | 104 | } 105 | 106 | int main() 107 | { 108 | lli n,p; 109 | //READ("input.txt"); 110 | //WRITE("output.txt"); 111 | while(scln(n) and n) 112 | pf("Total distinct primefactor = %lld\n",primefact(n)); 113 | 114 | 115 | #ifdef HOME 116 | cerr << "Time elapsed: " << clock() / 1000 << " ms" << endl; 117 | #endif 118 | return 0; 119 | } 120 | 121 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # Algorithms 2 | -------------------------------------------------------------------------------- /Read ,Write and Append in a text file in c.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | 4 | int main() 5 | { 6 | printf("Enter 1,2,3,4 for the corresponding command:\n"); 7 | printf("1. WRITE\n2. READ\n3. Append\n4. Exit\n"); 8 | 9 | int command; 10 | char name[20]; 11 | char ID[20]; 12 | char Department[20]; 13 | char CGPA[20]; 14 | 15 | while(1) 16 | { 17 | scanf("%d",&command); 18 | 19 | if(command==1) 20 | { 21 | FILE *f; 22 | f= fopen("textfile.txt","w"); 23 | printf("Enter your name: "); 24 | scanf("%s",name); 25 | fputs(name,f); 26 | fputs(" ",f); 27 | 28 | printf("Enter your ID: "); 29 | scanf("%s",ID); 30 | fputs(ID,f); 31 | fputs(" ",f); 32 | 33 | printf("Enter your Department: "); 34 | scanf("%s",Department); 35 | fputs(Department,f); 36 | fputs(" ",f); 37 | 38 | printf("Enter your CGPA: "); 39 | scanf("%s",CGPA); 40 | fputs(CGPA,f); 41 | fputs("\n",f); 42 | 43 | fclose(f); 44 | } 45 | else if(command==2) 46 | { 47 | FILE *f; 48 | f= fopen("textfile.txt","r"); 49 | if(f==NULL) 50 | { 51 | printf("File Not Exist\n"); 52 | continue; 53 | } 54 | 55 | char ch=fgetc(f); 56 | 57 | while(ch!=EOF) 58 | { 59 | printf("%c",ch); 60 | ch=fgetc(f); 61 | } 62 | } 63 | else if(command==3) 64 | { 65 | FILE *f; 66 | f= fopen("textfile.txt","a"); 67 | if(f==NULL) 68 | { 69 | printf("File Not Exist\n"); 70 | continue; 71 | } 72 | printf("Enter your name: "); 73 | scanf("%s",name); 74 | fputs(name,f); 75 | fputs(" ",f); 76 | 77 | printf("Enter your ID: "); 78 | scanf("%s",ID); 79 | fputs(ID,f); 80 | fputs(" ",f); 81 | 82 | printf("Enter your Department: "); 83 | scanf("%s",Department); 84 | fputs(Department,f); 85 | fputs(" ",f); 86 | 87 | printf("Enter your CGPA: "); 88 | scanf("%s",CGPA); 89 | fputs(CGPA,f); 90 | fputs("\n",f); 91 | 92 | fclose(f); 93 | } 94 | else if(command==4) 95 | { 96 | exit(0); 97 | } 98 | else 99 | { 100 | printf("Invalid Input. Enter again.\n"); 101 | } 102 | } 103 | 104 | return 0; 105 | } 106 | -------------------------------------------------------------------------------- /Rootfinding-Methods-And-Least-Square-Methods/Least square.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | int main() 4 | { 5 | int m,i; 6 | cout<<"Enter the number of data sets: "; 7 | cin>>m; 8 | cout<>a>>b; 14 | x=x+a; 15 | y=y+b; 16 | x2=x2+ceil(pow(a,2)); 17 | xy=xy+(a*b); 18 | } 19 | cout< 2 | #define accuracy 0.0001 3 | using namespace std; 4 | int a,b,c,d,degree; 5 | double m,n,x,z,sum; 6 | int arr[10]; 7 | double fun(double x) 8 | { 9 | sum=0; 10 | int i; z=degree; 11 | for(i=0;i<=degree;i++) 12 | { 13 | sum+=arr[i]*pow(x,z); 14 | z--; 15 | } 16 | return sum; 17 | } 18 | int main() 19 | { 20 | int i; 21 | cout <<"enter the value of the highest degree of the equation" << endl; 22 | cin >> degree; 23 | cout <<"Enter the co-efficients :" <> arr[i]; 26 | cout << "Enter two real number assuming the root is between them" << endl; 27 | cin >> m >> n ; 28 | while(fun(m)*fun(n)>=0) 29 | { 30 | cout << "Enter two real number assuming the root is between them (again)" << endl; 31 | cin >> m >> n ; 32 | } 33 | while(abs(n-m)>accuracy) 34 | { 35 | x=(m+n)/2; 36 | if(fun(x)==0) break; 37 | else if(fun(m)*fun(x)<0) n=x; 38 | else m=x; 39 | } 40 | printf(" the root of the equation is\n %.4lf",x); 41 | return 0; 42 | } 43 | -------------------------------------------------------------------------------- /Rootfinding-Methods-And-Least-Square-Methods/falseposition2.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #define accuracy 0.05 3 | using namespace std; 4 | int a,b,c,d,degree; 5 | double m,n,x,z,sum,k,loop=0; 6 | int arr[100]; 7 | double f(double x) 8 | { 9 | sum=0; 10 | int i; z=degree; 11 | for(i=0;i<=degree;i++) 12 | { 13 | sum+=arr[i]*pow(x,z); 14 | z--; 15 | } 16 | return sum; 17 | } 18 | int main() 19 | { 20 | int i; 21 | cout <<"enter the value of the highest degree of the equation" << endl; 22 | cin >> degree; 23 | cout <<"Enter the co-efficients :" <> arr[i]; 26 | cout << "Enter two real number assuming the root is between them" << endl; 27 | cin >> m >> n ; 28 | while(f(m)*f(n)>=0) 29 | { 30 | cout << "Enter two real number assuming the root is between them (again)" << endl; 31 | cin >> m >> n ; 32 | } 33 | while(1) 34 | { 35 | x=(m*f(n)-n*f(m))/float(f(n)-f(m)); 36 | if(loop>0) 37 | { 38 | if(abs( (( 1-(k/x) )*100) <=accuracy ) ) break; 39 | } 40 | if(f(x)==0) break; 41 | if(f(x)*f(m)<0) n=x; 42 | else m=x; 43 | k=x; 44 | loop++; 45 | } 46 | cout << " the root of the equation is"< 2 | #include 3 | #include 4 | using namespace std; 5 | 6 | double f(double v,double *coeff,int n) 7 | { 8 | double sum = 0; 9 | 10 | for(int i=0;i<=n;i++) 11 | { 12 | if(coeff[i]==0) 13 | continue; 14 | sum += coeff[i] * pow(v,n-i); 15 | 16 | } 17 | return sum; 18 | } 19 | double derivative(double v,double coeff[],int n) 20 | { 21 | double sum1 = 0; 22 | 23 | for(int i=0;i> dec; 34 | double e = 1.0 / pow(10,dec); 35 | 36 | cout << "\nEnter the approximate root\n"; 37 | double x0; 38 | cin >> x0; 39 | 40 | int p = n-1; 41 | 42 | double xi[2]; 43 | 44 | while (1) 45 | { 46 | xi[0] = x0 - p * (f(x0,arr,n)/derivative(x0,arr,n)); 47 | xi[1] = x0 - (p-1) * (f(x0,arr,n)/derivative(x0,arr,n)); 48 | 49 | if (fabs( f(xi[0],arr,n) ) <= e) 50 | { 51 | cout <> n; 71 | 72 | double coeff[n+1]; 73 | 74 | cout << "\nEnter "<> coeff[i]; 77 | 78 | newton(coeff,n); 79 | 80 | return 0; 81 | } 82 | -------------------------------------------------------------------------------- /Rootfinding-Methods-And-Least-Square-Methods/generalized.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #define accuracy 0.05 3 | using namespace std; 4 | int a,b,c,d,degree,i; 5 | double m,n,x,z,sum,sum1,sum2,k,loop=0; 6 | int arr[100]; 7 | double f1(double x) 8 | { 9 | sum1=0; 10 | z=degree; 11 | for(i=0;i<=degree;i++) 12 | { 13 | sum1+=arr[i]*pow(x,z); 14 | z--; 15 | } 16 | return sum1; 17 | } 18 | double f2(double x) 19 | { 20 | z=degree; 21 | sum=0; 22 | for(i=0;i> degree; 40 | cout <<"Enter the co-efficients :" <> arr[i]; 43 | cout << "Enter a real number assuming the root " << endl; 44 | cin >> m ; 45 | 46 | while(1) 47 | { 48 | x=f(m); 49 | if(loop>0) 50 | { 51 | if(abs( (( 1-(k/x) )*100) <=accuracy ) ) break; 52 | } 53 | m=x; 54 | k=x; 55 | loop++; 56 | } 57 | cout << " the root of the equation is"< 2 | using namespace std; 3 | double func(double v,double coeff[],int n) 4 | { 5 | double sum = 0; 6 | 7 | for(int i=0;i<=n;i++) 8 | { 9 | if(coeff[i]==0) 10 | continue; 11 | sum += coeff[i] * pow(v,n-i); 12 | } 13 | return sum; 14 | } 15 | double derivative(double v,double coeff[],int n) 16 | { 17 | double sum1 = 0; 18 | for(int i=0;i> x0; 29 | cout << "\nCorrection to how many decimal places\n"; 30 | double dec; 31 | cin >> dec; 32 | double e = 1.0 / pow(10,dec); 33 | int p = n; 34 | double xi; 35 | while (p--) 36 | { 37 | xi = x0 - p * (func(x0,arr,n)/derivative(x0,arr,n)); 38 | if (fabs( func(xi,arr,n) ) <= e) 39 | { 40 | cout <> n; 57 | double coeff[n+1]; 58 | cout << "\nEnter "<> coeff[i]; 61 | newton(coeff,n); 62 | return 0; 63 | } 64 | -------------------------------------------------------------------------------- /Rootfinding-Methods-And-Least-Square-Methods/iteration.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | double function(double x,double *coeff,int n) 4 | { 5 | double sum = 0; 6 | double arr[n+1]; 7 | 8 | for(int i=1;i<=n;i++) 9 | { 10 | arr[i] = -1.0 * (coeff[i]/coeff[0]); 11 | } 12 | 13 | for(int i=1;i<=n;i++) 14 | { 15 | sum += arr[i] * pow(x,n-i); 16 | } 17 | sum = pow(sum,1.0/n); 18 | 19 | return sum; 20 | } 21 | double derivative(double x,double *coeff,int n) 22 | { 23 | double sum1 = 0; 24 | double arr1[n+1]; 25 | 26 | for(int i=1;i<=n;i++) 27 | { 28 | arr1[i] = -1.0 * (coeff[i]/coeff[0]); 29 | } 30 | 31 | for(int i=1;i<=n;i++) 32 | { 33 | sum1 += arr1[i] * pow(x,n-i); 34 | } 35 | 36 | sum1 = (1.0/n) * pow(sum1,((1.0/n)-1)); 37 | 38 | double sum2 = 0; 39 | for(int i=1;i<=n-1;i++) 40 | { 41 | sum2 += (arr1[i]*(n-i)) * pow(x,n-i-1); 42 | } 43 | 44 | sum1 = sum1 * sum2; 45 | 46 | return sum1; 47 | } 48 | int main() 49 | { 50 | cout << "Enter the degree of the equation\n"; 51 | int n; 52 | cin >> n; 53 | double cff[n+1]; 54 | cout << "\nEnter "<> cff[i]; 57 | cout << "\nEnter boundary values or terminal points\n"; 58 | double a,b; 59 | cin >> a >> b; 60 | cout << "\nCorrection to how many decimal places\n"; 61 | double dec; 62 | cin >> dec; 63 | double e = 1.0 / pow(10,dec); 64 | double k = ((1.0-derivative(b,cff,n))/derivative(b,cff,n)) * e; 65 | double x1 = (a+b)/2; 66 | double x2 = function(x1,cff,n); 67 | while (fabs(x2-x1)>k) 68 | { 69 | x1 = x2; 70 | x2 = function(x1,cff,n); 71 | } 72 | cout < 2 | #define accuracy 0.05 3 | using namespace std; 4 | int a,b,c,d,degree; 5 | double m,n,x,z,sum,sum1,sum2,k,loop=0; 6 | int arr[100]; 7 | double f(double x) 8 | { 9 | sum1=0,sum2=0,sum=0; 10 | int i; 11 | z=degree; 12 | for(i=0;i<=degree;i++) 13 | { 14 | sum1+=arr[i]*pow(x,z); 15 | z--; 16 | } 17 | z=degree; 18 | for(i=0;i> degree; 31 | cout <<"Enter the co-efficients :" <> arr[i]; 34 | cout << "Enter a real number assuming the root " << endl; 35 | cin >> m ; 36 | 37 | while(1) 38 | { 39 | x=f(m); 40 | if(loop>0) 41 | { 42 | if(abs( (( 1-(k/x) )*100) <=accuracy ) ) break; 43 | } 44 | m=x; 45 | k=x; 46 | loop++; 47 | } 48 | cout << " the root of the equation is"< 2 | #define accuracy 0.0001 3 | using namespace std; 4 | int n,i,j,devider; 5 | double power,arr[15],x,k,now,before; 6 | double a[100],b[100] ; 7 | int v=1; 8 | double func(int j) 9 | { 10 | double sum=0; int y=j; 11 | for(i=1;i<=3;i++) 12 | sum+=a[i]*b[y--]; 13 | return sum; 14 | } 15 | int main() 16 | { 17 | cout << "Enter the highest degree of the equation" << endl; 18 | cin >> n ; 19 | cout << "Enter the co-efficients and powers of the equation in the form of ax^" << n << "+ bx^" << n-1 << " + ........... + constant " << endl; 20 | for(i=0;i<=n ;i++) 21 | { 22 | cin >> arr[i]; 23 | } 24 | for(i=n;i>=0;i--) 25 | { 26 | if(arr[i]!=0) {devider= i; break;} 27 | } 28 | k=arr[devider]; 29 | for(i=devider-1;i>=0;i--) 30 | { 31 | a[v]=(-1) * arr[i]/k; 32 | v++; 33 | } 34 | b[1]=1; 35 | b[2]=a[1] ; 36 | b[3]=a[1]*b[2] + b[1]*a[2]; 37 | j=3; 38 | before=b[1]/b[2]; 39 | now=b[2]/b[3]; 40 | while( abs (now-before)>=0.0001) 41 | { 42 | before=now; 43 | b[j+1]=func(j); 44 | now=b[j]/b[j+1]; 45 | j++; 46 | } 47 | cout << "the root of the equation is : " << now << endl; 48 | return 0; 49 | } 50 | -------------------------------------------------------------------------------- /Rootfinding-Methods-And-Least-Square-Methods/secant.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #define accuracy 0.05 3 | using namespace std; 4 | int a,b,c,d,degree; 5 | double m,n,x,z,sum,k,loop=0; 6 | int arr[100]; 7 | double f(double x) 8 | { 9 | sum=0; 10 | int i; z=degree; 11 | for(i=0;i<=degree;i++) 12 | { 13 | sum+=arr[i]*pow(x,z); 14 | z--; 15 | } 16 | return sum; 17 | } 18 | int main() 19 | { 20 | int i; 21 | cout <<"enter the value of the highest degree of the equation" << endl; 22 | cin >> degree; 23 | cout <<"Enter the co-efficients :" <> arr[i]; 26 | cout << "Enter two real number assuming the root is between them" << endl; 27 | cin >> m >> n ; 28 | while(f(m)*f(n)>=0) 29 | { 30 | cout << "Enter two real number assuming the root is between them (again)" << endl; 31 | cin >> m >> n ; 32 | } 33 | while(1) 34 | { 35 | x=(m*f(n)-n*f(m))/float(f(n)-f(m)); 36 | if(loop>1) 37 | { 38 | if(abs( (( 1-(k/x) )*100) <=accuracy ) ) break; 39 | } 40 | if(f(x)==0) break; 41 | m=n; 42 | n=x; 43 | k=x; 44 | loop++; 45 | } 46 | cout << " the root of the equation is"< 2 | using namespace std; 3 | int main() 4 | { 5 | int n,i; 6 | cout<<"Enter the number of data sets: "; 7 | cin>>n; 8 | cout<>a>>b>>c; 14 | x=x+a; 15 | y=y+b; 16 | w=w+c; 17 | wx=wx+(a*c); 18 | wy=wy+(b*c); 19 | wx2=wx2+c*pow(a,2); 20 | wxy=wxy+(a*b*c); 21 | } 22 | 23 | cout<mm ; 7 | unordered_mapmm ; 8 | 9 | ### mm.find(k) 10 | Returns an iterator of the specified key value if it's been found in the map or returns an iterator to mm.end() if element not found. 11 | 12 | mapmm; 13 | 14 | mm[4]=9; 15 | 16 | mm[3]=2; 17 | 18 | auto it=mm.find(4); 19 | 20 | if(it==mm.end()) 21 | 22 | cout << "element not found" << endl; 23 | 24 | else 25 | 26 | cout << it->first << " " << it->second << endl; // prints- 4 9 27 | 28 | complexity: 29 | 30 | for map complexity is logarithmic. 31 | 32 | for unordered_map complexity is constant(average), linear(worst). 33 | -------------------------------------------------------------------------------- /Tower of Hanoi minimum cost DP.cpp: -------------------------------------------------------------------------------- 1 | // https://codeforces.com/contest/392/problem/B 2 | #include 3 | ///...................................*****................................................./// 4 | /// Author : Raihan Khan Raka ( raihankhanraka@gmail.com ) /// 5 | /// Department of Computer Science /// 6 | /// & Engineering /// 7 | /// Comilla University , Bangladesh. /// 8 | ///...................................*****................................................./// 9 | 10 | /*....................................Values................................................*/ 11 | #define p5 100007 12 | #define p6 1000007 13 | #define PI acos(-1) 14 | #define M 1000000007 15 | #define inf 1LL << 62 16 | #define white 0 17 | #define gray 1 18 | #define black 2 19 | /*....................................Functions.............................................*/ 20 | #define sqr(x) x*x 21 | #define sc scanf 22 | #define pf printf 23 | #define pfn printf("\n") 24 | #define scin(x) sc("%d",&(x)) 25 | #define scin2(xx,zz) scanf("%d %d",&xx,&zz) 26 | #define scln(x) sc("%lld",&(x)) 27 | #define scln2(xx,zz) scanf("%lld %lld",&xx,&zz) 28 | #define min3(a,b,c) min(a,bc?b:c) 30 | #define all(v) v.begin(), v.end() 31 | #define ok cout << "ok" << endl 32 | #define mem(x,y) memset(x,y,sizeof(x)) 33 | #define clr(a) a.clear() 34 | #define READ(f) freopen(f, "r", stdin) 35 | #define WRITE(f) freopen(f, "w", stdout) 36 | 37 | /*...................................Data_Types............................................*/ 38 | #define lli long long int 39 | #define ull unsigned long long int 40 | #define pii pair < int, int> 41 | #define pll pair < ll, ll> 42 | #define veci vector 43 | #define vecl vector 44 | #define vecp vector< pair > 45 | #define mapstrint map< string , int > 46 | #define mapstrstr map< string , string > 47 | #define mapint map< int, int > 48 | #define uset unordered_set 49 | #define umap unordered_map 50 | #define pq priority_queue 51 | 52 | #define pb push_back 53 | #define mp make_pair 54 | #define ff first 55 | #define ss second 56 | 57 | /*.....................................Loops...............................................*/ 58 | #define rep( i , a , b ) for( i=a ; i=b ; i--) 60 | #define repx( i ,a,b, x) for( i=a ; i= M ? a - M : a;} 70 | inline lli sub(lli a, lli b) {a -= b; return a < 0 ? a + M : a;} 71 | inline lli mul(lli a, lli b) {return (a * b) % M;} 72 | lli gcd(lli x,lli y) 73 | { 74 | if(x==0) return y; 75 | return gcd(y%x,x); 76 | } 77 | lli bigmod(lli n, lli k ) 78 | { 79 | lli ans=1; 80 | while(k) 81 | { 82 | if(k&1) 83 | ans=(ans*n)%M; 84 | k=k>>1; 85 | n=(n*n)%M; 86 | } 87 | 88 | return ans; 89 | } 90 | */ 91 | ///----------------------------------Graph moves---------------------------------------- 92 | /* 93 | int dx4[5] = {1, -1, 0, 0}; 94 | int dy4[5] = {0, 0, 1, -1}; 95 | int dx8[9] = {0, 0, 1, -1, -1, 1, -1, 1}; 96 | int dy8[9] = {-1, 1, 0, 0, 1, 1, -1, -1}; 97 | int knightx[9] = {-2, -2, -1, -1, 1, 1, 2, 2}; 98 | int knighty[9] = {-1, 1, -2, 2, -2, 2, -1, 1}; 99 | bool valid( int r , int c , int x , int y ){ if( x >= 1 && x <= r && y >= 1 && y <= c ) return 1 ; return 0 ; } 100 | */ 101 | 102 | using namespace std; 103 | int cost[4][4]; 104 | lli dp[43][4][4][4]; 105 | 106 | lli towerhanoi(int n,int A,int B,int C) 107 | { 108 | if(n==0) return 0; 109 | 110 | lli &ret=dp[n][A][B][C]; 111 | if(ret) return ret; 112 | 113 | lli way1 = towerhanoi(n-1,A,C,B) + cost[A][B] + towerhanoi(n-1,C,B,A); 114 | // move n-1 disks A-C then nth disk A-B then return n-1 disks C-A 115 | lli way2 = towerhanoi(n-1,A,B,C) + cost[A][C] + towerhanoi(n-1,B,A,C) + cost[C][B] + towerhanoi(n-1,A,B,C); 116 | //move n-1 disks A-B then nth disk A-C then return n-1 disks B-A then nth disk C-B then n-1 disks A-B 117 | 118 | return ret = min(way1,way2); 119 | 120 | } 121 | 122 | 123 | int main() 124 | { 125 | int n,i,j; 126 | 127 | rep(i , 1 , 4) 128 | rep(j , 1 , 4) 129 | scin(cost[i][j]); 130 | 131 | scin(n); 132 | 133 | lli minimoves= towerhanoi(n,1,3,2); // from,to,aux 134 | 135 | cout << minimoves << endl; 136 | 137 | 138 | #ifdef HOME 139 | cerr << "Time elapsed: " << clock() / 1000 << " ms" << endl; 140 | #endif 141 | return 0; 142 | } 143 | 144 | -------------------------------------------------------------------------------- /Tower of Hanoi.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #define PI acos(-1) 3 | #define min3(a,b,c) min(a,min(b,c)) 4 | #define max3(a,b,c) max(a,max(b,c)) 5 | #define READ(f) freopen(f, "r", stdin) 6 | #define WRITE(f) freopen(f, "w", stdout) 7 | #define lli long long int 8 | #define ull unsigned long long int 9 | #define pii pair < int, int> 10 | #define pll pair < ll, ll> 11 | #define sc scanf 12 | #define scin(x) sc("%d",&(x)) 13 | #define scln(x) sc("%lld",&(x)) 14 | #define pf printf 15 | #define ms(a,b) memset(a,b,sizeof(a)) 16 | #define veci vector 17 | #define vecl vector 18 | #define pb push_back 19 | 20 | using namespace std ; 21 | 22 | long long int m=0; 23 | 24 | 25 | 26 | void towers(int num, char frompeg, char topeg, char auxpeg) 27 | { 28 | if (num == 1) 29 | { 30 | printf("\n Move disk 1 from peg %c to peg %c", frompeg, topeg); //move 1 disk from A to C 31 | m++; 32 | return; 33 | } 34 | towers(num - 1, frompeg, auxpeg, topeg); //move n-1 disks from A to B using C 35 | printf("\n Move disk %d from peg %c to peg %c", num, frompeg, topeg); 36 | m++; 37 | towers(num - 1, auxpeg, topeg, frompeg); //move n-1 disks from B to C using A 38 | } 39 | 40 | 41 | int main() 42 | { 43 | int num; 44 | 45 | cout << "Enter the number of disks : " << endl; 46 | cin >> num ; 47 | 48 | 49 | towers(num, 'A', 'C', 'B'); 50 | 51 | cout <data != data) 72 | { 73 | if(current != NULL) 74 | printf("%d ",current->data); 75 | 76 | 77 | if(current->data > data) 78 | { 79 | current = current->leftChild; 80 | } 81 | 82 | else 83 | { 84 | current = current->rightChild; 85 | } 86 | 87 | 88 | if(current == NULL) 89 | { 90 | return NULL; 91 | } 92 | } 93 | 94 | return current; 95 | } 96 | 97 | void pre_order_traversal(struct node* root) 98 | { 99 | if(root != NULL) 100 | { 101 | printf("%d ",root->data); 102 | pre_order_traversal(root->leftChild); 103 | pre_order_traversal(root->rightChild); 104 | } 105 | } 106 | 107 | void inorder_traversal(struct node* root) 108 | { 109 | if(root != NULL) 110 | { 111 | inorder_traversal(root->leftChild); 112 | printf("%d ",root->data); 113 | inorder_traversal(root->rightChild); 114 | } 115 | } 116 | 117 | void post_order_traversal(struct node* root) 118 | { 119 | if(root != NULL) 120 | { 121 | post_order_traversal(root->leftChild); 122 | post_order_traversal(root->rightChild); 123 | printf("%d ", root->data); 124 | } 125 | } 126 | 127 | int main() 128 | { 129 | int i; 130 | int array[7] = { 27, 14, 35, 10, 19, 31, 42 }; 131 | 132 | for(i = 0; i < 7; i++) 133 | insert(array[i]); 134 | 135 | i = 31; 136 | struct node * temp = search(i); 137 | 138 | if(temp != NULL) 139 | { 140 | printf("[%d] Element found.", temp->data); 141 | printf("\n"); 142 | } 143 | else 144 | { 145 | printf("[ x ] Element not found (%d).\n", i); 146 | } 147 | 148 | i = 15; 149 | temp = search(i); 150 | 151 | if(temp != NULL) 152 | { 153 | printf("[%d] Element found.", temp->data); 154 | printf("\n"); 155 | } 156 | else 157 | { 158 | printf("[ x ] Element not found (%d).\n", i); 159 | } 160 | 161 | printf("\nPreorder traversal: "); 162 | pre_order_traversal(root); 163 | 164 | printf("\nInorder traversal: "); 165 | inorder_traversal(root); 166 | 167 | printf("\nPost order traversal: "); 168 | post_order_traversal(root); 169 | 170 | return 0; 171 | } 172 | -------------------------------------------------------------------------------- /bubble sort.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | int main() 4 | { 5 | int i,n,j ,temp; 6 | cin >> n ; 7 | 8 | int arr[n]; 9 | 10 | for(i=0;i> arr[i] ; 12 | 13 | for(i=0;iarr[j+1]) 17 | { 18 | temp=arr[j+1]; 19 | arr[j+1]=arr[j]; 20 | arr[j] = temp ; 21 | } 22 | } 23 | for(i=0;i 2 | #define PI acos(-1) 3 | #define min3(a,b,c) min(a,min(b,c)) 4 | #define max3(a,b,c) max(a,max(b,c)) 5 | #define READ(f) freopen(f, "r", stdin) 6 | #define WRITE(f) freopen(f, "w", stdout) 7 | #define lli long long int 8 | #define ull unsigned long long int 9 | #define pii pair < int, int> 10 | #define pll pair < ll, ll> 11 | #define sc scanf 12 | #define scin(x) sc("%d",&(x)) 13 | #define scln(x) sc("%lld",&(x)) 14 | #define pf printf 15 | #define ms(a,b) memset(a,b,sizeof(a)) 16 | #define veci vector 17 | #define vecl vector 18 | #define pb push_back 19 | long power(long int x, long int y){ int temp; if( y == 0) return 1; temp = power(x, y/2); if (y%2 == 0) return temp*temp; else return x*temp*temp; } 20 | using namespace std; 21 | 22 | lli gcd(lli x,lli y) 23 | { 24 | if(x==0) return y; 25 | return gcd(y%x,x); 26 | } 27 | void divgcd(lli &a , lli &b) 28 | { 29 | lli g= gcd(a,b); 30 | a/=g; 31 | b/=g; 32 | } 33 | lli C(lli n , lli k) 34 | { 35 | lli numerator=1, denumerator=1,tomul,todiv,i; 36 | if(k>n/2) k=n-k; 37 | for(i=k;i>0;i--) 38 | { 39 | tomul=n-k+i; 40 | todiv=i; 41 | 42 | divgcd(tomul,todiv); 43 | divgcd(numerator,tomul); 44 | divgcd(denumerator,todiv); 45 | 46 | numerator*=tomul; 47 | denumerator*=todiv; 48 | 49 | } 50 | 51 | return numerator/denumerator; 52 | } 53 | 54 | 55 | int main() 56 | { 57 | lli n,k; 58 | cin >> n >> k ; 59 | 60 | cout << n << "C" << k << " = " << C(n,k) << endl; 61 | 62 | 63 | return 0; 64 | } 65 | 66 | 67 | 68 | -------------------------------------------------------------------------------- /insertsion sort.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #define PI acos(-1) 3 | #define min3(a,b,c) min(a,min(b,c)) 4 | #define max3(a,b,c) max(a,max(b,c)) 5 | #define READ(f) freopen(f, "r", stdin) 6 | #define WRITE(f) freopen(f, "w", stdout) 7 | #define lli long long int 8 | #define ull unsigned long long int 9 | #define pii pair < int, int> 10 | #define pll pair < ll, ll> 11 | #define sc scanf 12 | #define scin(x) sc("%d",&(x)) 13 | #define scln(x) sc("%lld",&(x)) 14 | #define pf printf 15 | #define ms(a,b) memset(a,b,sizeof(a)) 16 | #define veci vector 17 | #define vecl vector 18 | #define pb push_back 19 | 20 | using namespace std; 21 | 22 | void insertionSort(int arr[], int n) 23 | { 24 | int i, key, j; 25 | for (i = 1; i < n; i++) 26 | { 27 | key = arr[i]; 28 | j = i-1; 29 | 30 | 31 | while (j >= 0 && arr[j] > key) 32 | { 33 | arr[j+1] = arr[j]; 34 | j--; 35 | } 36 | arr[j+1] = key; 37 | } 38 | } 39 | 40 | 41 | void printArray(int arr[], int n) 42 | { 43 | int i; 44 | for (i=0; i < n; i++) 45 | printf("%d ", arr[i]); 46 | printf("\n"); 47 | } 48 | 49 | 50 | 51 | int main() 52 | { 53 | int n,i; 54 | cin >> n ; 55 | int arr[n]; 56 | for(i=0;i> arr[i] ; 58 | insertionSort(arr, n); 59 | printArray(arr, n); 60 | 61 | return 0; 62 | } 63 | 64 | 65 | -------------------------------------------------------------------------------- /josephus o(n).cpp: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | #define PI acos(-1) 4 | #define min3(a,b,c) min(a,min(b,c)) 5 | #define max3(a,b,c) max(a,max(b,c)) 6 | #define READ(f) freopen(f, "r", stdin) 7 | #define WRITE(f) freopen(f, "w", stdout) 8 | #define lli long long int 9 | #define ull unsigned long long int 10 | #define pii pair < int, int> 11 | #define pll pair < ll, ll> 12 | #define sc scanf 13 | #define scin(x) sc("%d",&(x)) 14 | #define scln(x) sc("%lld",&(x)) 15 | #define pf printf 16 | #define veci vector 17 | #define vecl vector 18 | #define vecp vector< pair > 19 | #define pb push_back 20 | #define inf 1<<30 21 | #define mp make_pair 22 | #define ss stringstream 23 | #define all(v) v.begin(), v.end() 24 | #define mem(x,y) memset(x,y,sizeof(x)) 25 | #define FastRead ios_base::sync_with_stdio(0);cin.tie(0) 26 | #define M 1000000007 27 | //int month[]={31,28,31,30,31,30,31,31,30,31,30,31}; 28 | //long power(long int x, long int y){ int temp; if( y == 0) return 1; temp = power(x, y/2); if (y%2 == 0) return temp*temp; else return x*temp*temp; } 29 | /*lli gcd(lli x,lli y) 30 | { 31 | if(x==0) return y; 32 | return gcd(y%x,x); 33 | }*/ 34 | using namespace std; 35 | int j ( int n , int k ) 36 | { 37 | if(n==1 ) return 1; 38 | return ( j(n - 1 , k ) + k-1 ) % n + 1 ; 39 | } 40 | 41 | int main() 42 | { 43 | int n , k ; 44 | cin >> n >> k ; 45 | 46 | cout << "The last person to stay alive is at position : " << j ( n , k ) << endl; 47 | 48 | 49 | return 0; 50 | } 51 | -------------------------------------------------------------------------------- /josephus.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #define PI acos(-1) 3 | #define min3(a,b,c) min(a,min(b,c)) 4 | #define max3(a,b,c) max(a,max(b,c)) 5 | #define READ(f) freopen(f, "r", stdin) 6 | #define WRITE(f) freopen(f, "w", stdout) 7 | #define lli long long int 8 | #define ull unsigned long long int 9 | #define pii pair < int, int> 10 | #define pll pair < ll, ll> 11 | #define sc scanf 12 | #define scin(x) sc("%d",&(x)) 13 | #define scln(x) sc("%lld",&(x)) 14 | #define pf printf 15 | #define ms(a,b) memset(a,b,sizeof(a)) 16 | #define veci vector 17 | #define vecl vector 18 | #define vecp vector< pair > 19 | #define pb push_back 20 | using namespace std; 21 | 22 | 23 | int findmsb(int n) 24 | { 25 | int position=0; 26 | 27 | while(n!=0) 28 | { 29 | position++; 30 | n=n>>1 ; 31 | } 32 | 33 | return position; 34 | } 35 | 36 | int PersonToLive(int n) 37 | { 38 | int length = findmsb(n); 39 | 40 | int xor_oparand = 1 << (length-1); 41 | 42 | n = n ^ xor_oparand; 43 | 44 | n = n << 1; 45 | 46 | n = n|1; 47 | 48 | return n; 49 | } 50 | 51 | int main() 52 | { 53 | 54 | int NumberOfSoldiers; 55 | 56 | cin >> NumberOfSoldiers; 57 | 58 | cout << PersonToLive(NumberOfSoldiers) << endl; 59 | 60 | return 0; 61 | } 62 | -------------------------------------------------------------------------------- /matrix multiplication.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #define PI acos(-1) 3 | #define min3(a,b,c) min(a,min(b,c)) 4 | #define max3(a,b,c) max(a,max(b,c)) 5 | #define READ(f) freopen(f, "r", stdin) 6 | #define WRITE(f) freopen(f, "w", stdout) 7 | #define lli long long int 8 | #define ull unsigned long long int 9 | #define pii pair < int, int> 10 | #define pll pair < ll, ll> 11 | #define sc scanf 12 | #define scin(x) sc("%d",&(x)) 13 | #define scln(x) sc("%lld",&(x)) 14 | #define pf printf 15 | #define ms(a,b) memset(a,b,sizeof(a)) 16 | #define veci vector 17 | #define vecl vector 18 | #define pb push_back 19 | long power(long int x, long int y){ int temp; if( y == 0) return 1; temp = power(x, y/2); if (y%2 == 0) return temp*temp; else return x*temp*temp; } 20 | /*lli gcd(lli x,lli y) 21 | { 22 | if(x==0) return y; 23 | return gcd(y%x,x); 24 | }*/ 25 | using namespace std; 26 | 27 | 28 | int main() 29 | { 30 | 31 | int i,j,k; 32 | 33 | cout << "Enter the number rows and columns of first matrix " << endl; 34 | int r1,c1; 35 | cin >> r1 >> c1 ; 36 | cout << "Enter the elements" << endl; 37 | int A[r1][c1]; 38 | for(i=0;i> A[i][j] ; 42 | } 43 | 44 | cout << "Enter the number rows and columns of second matrix " << endl; 45 | int r2,c2; 46 | cin >> r2 >> c2 ; 47 | while(r1!=c2 && r2!=c1) 48 | { 49 | cout << "The number of rows of first matrix must eqaul the number of columns of second matrix"<> r2 >> c2 ; 52 | } 53 | cout << "Enter the elements" << endl; 54 | int B[r2][c2]; 55 | for(i=0;i> B[i][j] ; 59 | } 60 | 61 | int C[r1][r1]; 62 | memset(C,0,r1*r1*sizeof(C[0][0])); 63 | 64 | for(i=0;i 2 | 3 | using namespace std; 4 | 5 | int main() 6 | { 7 | char a[11]; 8 | int n; 9 | scanf("%d",&n); 10 | while(n--) 11 | { 12 | scanf("%s",a); 13 | int len = strlen(a); 14 | sort(a,a+len); 15 | do 16 | { 17 | printf("%s\n",a); 18 | }while(next_permutation(a,a+len)); 19 | printf("\n"); 20 | } 21 | return 0; 22 | } 23 | -------------------------------------------------------------------------------- /quicksort.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #define PI acos(-1) 3 | #define min3(a,b,c) min(a,min(b,c)) 4 | #define max3(a,b,c) max(a,max(b,c)) 5 | #define READ(f) freopen(f, "r", stdin) 6 | #define WRITE(f) freopen(f, "w", stdout) 7 | #define lli long long int 8 | #define ull unsigned long long int 9 | #define pii pair < int, int> 10 | #define pll pair < ll, ll> 11 | #define sc scanf 12 | #define scin(x) sc("%d",&(x)) 13 | #define scln(x) sc("%lld",&(x)) 14 | #define pf printf 15 | #define ms(a,b) memset(a,b,sizeof(a)) 16 | #define veci vector 17 | #define vecl vector 18 | #define pb push_back 19 | long power(long int x, long int y){ int temp; if( y == 0) return 1; temp = power(x, y/2); if (y%2 == 0) return temp*temp; else return x*temp*temp; } 20 | /*lli gcd(lli x,lli y) 21 | { 22 | if(x==0) return y; 23 | return gcd(y%x,x); 24 | }*/ 25 | using namespace std; 26 | 27 | int partition (int arr[], int low, int high) 28 | { 29 | int pivot = arr[high]; 30 | int i = (low - 1); 31 | 32 | for (int j = low; j <= high-1; j++) 33 | { 34 | 35 | if (arr[j] <= pivot) 36 | { 37 | i++; 38 | swap(arr[i], arr[j]); 39 | } 40 | } 41 | swap(arr[i + 1], arr[high]); 42 | return (i + 1); 43 | } 44 | 45 | void quickSort(int arr[], int low, int high) 46 | { 47 | if (low < high) 48 | { 49 | 50 | int pi = partition(arr, low, high); 51 | 52 | 53 | quickSort(arr, low, pi - 1); 54 | quickSort(arr, pi + 1, high); 55 | } 56 | } 57 | 58 | 59 | void printArray(int arr[], int n) 60 | { 61 | int i; 62 | for (i=0; i < n ; i++) 63 | cout << arr[i] << " " ; 64 | } 65 | 66 | 67 | int main() 68 | { 69 | int n,i; 70 | cout << "Enter the number of elements " << endl; 71 | cin >> n ; 72 | cout << "Enter the elements" << endl; 73 | int arr[n]; 74 | for(i=0;i> arr[i] ; 76 | 77 | quickSort(arr, 0, n-1); 78 | cout << "Sorted array: " << endl; 79 | printArray(arr, n); 80 | cout << endl; 81 | return 0; 82 | } 83 | -------------------------------------------------------------------------------- /roman number.c: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | void predigit(char num1, char num2); 4 | void postdigit(char c, int n); 5 | 6 | char romanval[1000]; 7 | int i = 0; 8 | int main() 9 | { 10 | int j; 11 | long number; 12 | 13 | printf("Enter the number: "); 14 | scanf("%d", &number); 15 | if (number <= 0) 16 | { 17 | printf("Invalid number"); 18 | return 0; 19 | } 20 | while (number != 0) 21 | { 22 | if (number >= 1000) 23 | { 24 | postdigit('M', number / 1000); 25 | number = number - (number / 1000) * 1000; 26 | } 27 | else if (number >= 500) 28 | { 29 | if (number < (500 + 4 * 100)) 30 | { 31 | postdigit('D', number / 500); 32 | number = number - (number / 500) * 500; 33 | } 34 | else 35 | { 36 | predigit('C','M'); 37 | number = number - (1000-100); 38 | } 39 | } 40 | else if (number >= 100) 41 | { 42 | if (number < (100 + 3 * 100)) 43 | { 44 | postdigit('C', number / 100); 45 | number = number - (number / 100) * 100; 46 | } 47 | else 48 | { 49 | predigit('L', 'D'); 50 | number = number - (500 - 100); 51 | } 52 | } 53 | else if (number >= 50 ) 54 | { 55 | if (number < (50 + 4 * 10)) 56 | { 57 | postdigit('L', number / 50); 58 | number = number - (number / 50) * 50; 59 | } 60 | else 61 | { 62 | predigit('X','C'); 63 | number = number - (100-10); 64 | } 65 | } 66 | else if (number >= 10) 67 | { 68 | if (number < (10 + 3 * 10)) 69 | { 70 | postdigit('X', number / 10); 71 | number = number - (number / 10) * 10; 72 | } 73 | else 74 | { 75 | predigit('X','L'); 76 | number = number - (50 - 10); 77 | } 78 | } 79 | else if (number >= 5) 80 | { 81 | if (number < (5 + 4 * 1)) 82 | { 83 | postdigit('V', number / 5); 84 | number = number - (number / 5) * 5; 85 | } 86 | else 87 | { 88 | predigit('I', 'X'); 89 | number = number - (10 - 1); 90 | } 91 | } 92 | else if (number >= 1) 93 | { 94 | if (number < 4) 95 | { 96 | postdigit('I', number / 1); 97 | number = number - (number / 1) * 1; 98 | } 99 | else 100 | { 101 | predigit('I', 'V'); 102 | number = number - (5 - 1); 103 | } 104 | } 105 | } 106 | printf("Roman number is: "); 107 | for(j = 0; j < i; j++) 108 | printf("%c", romanval[j]); 109 | return 0; 110 | } 111 | 112 | void predigit(char num1, char num2) 113 | { 114 | romanval[i++] = num1; 115 | romanval[i++] = num2; 116 | } 117 | 118 | void postdigit(char c, int n) 119 | { 120 | int j; 121 | for (j = 0; j < n; j++) 122 | romanval[i++] = c; 123 | } 124 | -------------------------------------------------------------------------------- /seive of eratosthenes(final).cpp: -------------------------------------------------------------------------------- 1 | #include 2 | ///...................................*****................................................./// 3 | /// Author : Raihan Khan Raka ( raihankhanraka@gmail.com ) /// 4 | /// Department of Computer Science /// 5 | /// & Engineering /// 6 | /// Comilla University , Bangladesh. 7 | /// 8 | ///...................................*****................................................./// 9 | 10 | /*....................................Values................................................*/ 11 | #define p5 100007 12 | #define p6 1000007 13 | #define PI acos(-1) 14 | #define M 1000000007 15 | #define inf 1LL << 62 16 | #define white 0 17 | #define gray 1 18 | #define black 2 19 | /*....................................Functions.............................................*/ 20 | #define sqr(x) x*x 21 | #define sc scanf 22 | #define pf printf 23 | #define pfn printf("\n") 24 | #define scin(x) sc("%d",&(x)) 25 | #define scin2(xx,zz) scanf("%d %d",&xx,&zz) 26 | #define scln(x) sc("%lld",&(x)) 27 | #define scln2(xx,zz) scanf("%lld %lld",&xx,&zz) 28 | #define min3(a,b,c) min(a,min(b,c)) 29 | #define max3(a,b,c) max(a,max(b,c)) 30 | #define all(v) v.begin(), v.end() 31 | #define ok cout << "ok" << endl 32 | #define mem(x,y) memset(x,y,sizeof(x)) 33 | #define clr(a) a.clear() 34 | #define READ(f) freopen(f, "r", stdin) 35 | #define WRITE(f) freopen(f, "w", stdout) 36 | 37 | /*...................................Data_Types............................................*/ 38 | #define lli long long int 39 | #define ull unsigned long long int 40 | #define pii pair < int, int> 41 | #define pll pair < ll, ll> 42 | #define veci vector 43 | #define vecl vector 44 | #define vecp vector< pair > 45 | #define mapstrint map< string , int > 46 | #define mapstrstr map< string , string > 47 | #define mapint map< int, int > 48 | #define uset unordered_set 49 | #define umap unordered_map 50 | #define pq priority_queue 51 | 52 | #define pb push_back 53 | #define mp make_pair 54 | #define ff first 55 | #define ss second 56 | 57 | /*.....................................Loops...............................................*/ 58 | #define rep( i , a , b ) for( i=a ; i=b ; i--) 60 | #define repx( i ,a,b, x) for( i=a ; i>1; 80 | n=(n*n)%M; 81 | } 82 | 83 | return ans; 84 | } 85 | */ 86 | /* 87 | int dx4[5] = {1, -1, 0, 0 }; 88 | int dy4[5] = {0, 0, 1, -1}; 89 | int dx8[9] = { 0 , 0 , -1 , 1 , -1 , -1 , 1 , 1 } ; 90 | int dy8[9] = { -1 , 1 , 0 , 0 , -1 , 1 , -1 , 1 } ; 91 | 92 | int knightx[9] = { -1 , 1 , -2 , 2 , -2 , 2 , -1 , 1 } ; 93 | int knighty[9] = { -2 , -2 , -1 , -1 , 1 , 1 , 2 , 2 } ; 94 | 95 | bool valid( int r , int c , int x , int y ){ if( x >= 1 && x <= r && y >= 1 && y <= c ) return 1 ; return 0 ; } 96 | */ 97 | 98 | using namespace std; 99 | bool arr[100000003]; 100 | vecl v; 101 | 102 | // This is a very efficient seive of eratosthenes. Time complexity: O(nloglogn) . 103 | // Can store 5*10^6 prime numbers below 10^8 on c++14 ideone compiler. 104 | 105 | void seive(int n) 106 | { 107 | lli root=sqrt(n)+1,p,i,j; 108 | arr[0]=arr[1]=1; 109 | v.pb(2); 110 | for(i=3;i<=root;i+=2) 111 | { 112 | if(!arr[i]) 113 | { 114 | v.pb(i); 115 | p=i<<1; 116 | for( j=i*i ; j<=n ; j+=p) 117 | arr[j]=1; 118 | } 119 | } 120 | 121 | for( i=root&1?root+2:root+1 ; i<=n ; i+=2 ) 122 | if(!arr[i]) 123 | v.pb(i); 124 | } 125 | int main() 126 | { 127 | int n,i,j; 128 | scin(n); 129 | 130 | seive(n); 131 | int p=v.size(); 132 | for(i=0;i 2 | using namespace std; 3 | int main() 4 | { 5 | int n,i,j,temp; 6 | cin >> n ; 7 | int arr[n]; 8 | for(i=0;i> arr[i]; 10 | 11 | for(i=0;i 2 | int main() 3 | { 4 | int num; 5 | scanf("%d",&num); 6 | char digit [21][10] = { "", "one", "two", "three", "four", "five", "six", "seven", 7 | "eight", "nine", "ten", "eleven", "twelve", "thirteen", 8 | "fourteen", "fifteen", "sixteen", "seventeen", "eighteen", 9 | "nineteen"}; 10 | char tens [11][10] = { "", "", "twenty", "thirty", "forty", "fifty", "sixty", 11 | "seventy", "eighty", "ninety"}; 12 | char str[1000] = {0}; 13 | int prev=0, div=1000; 14 | strcpy(str, ""); 15 | 16 | while(div) { 17 | 18 | if ((num / div) % 10 > 0 || (div == 10 && (num%100) > 0)) { 19 | 20 | if (prev) { 21 | strcat(str, " and"); 22 | prev = 0; 23 | } 24 | 25 | switch(div) { 26 | case 1000: 27 | if (strlen(str) > 0 && str[strlen(str) - 1] != ' ') 28 | strcat(str, " "); 29 | strcat(str, digit[(num / div) % 10]); 30 | 31 | if (((num / div) % 10) > 1) 32 | strcat(str, " thousands"); 33 | else 34 | strcat(str, " thousand"); 35 | prev = 1; 36 | break; 37 | case 100: 38 | if (strlen(str) > 0 && str[strlen(str) - 1] != ' ') 39 | strcat(str, " "); 40 | 41 | strcat(str, digit[(num / div) % 10]); 42 | 43 | if (((num / div) % 10) > 1) 44 | strcat(str, " hundreds"); 45 | else 46 | strcat(str, " hundred"); 47 | 48 | prev = 1; 49 | break; 50 | case 10: 51 | if ( (num%100) >= 10 && (num%100) <= 19) 52 | { 53 | if (strlen(str) > 0 && str[strlen(str) - 1] != ' ') 54 | strcat(str, " "); 55 | 56 | strcat(str, digit[num%100]); 57 | } 58 | else { 59 | if (strlen(str) > 0 && str[strlen(str) - 1] != ' ') 60 | strcat(str, " "); 61 | strcat(str, tens[(num%100)/10]); 62 | 63 | if (strlen(str) > 0 && str[strlen(str) - 1] != ' ') 64 | strcat(str, " "); 65 | 66 | strcat(str, digit[num%10]); 67 | } 68 | break; 69 | } 70 | } 71 | 72 | div /= 10; 73 | } 74 | printf("%d %s\n", num, str); 75 | 76 | } 77 | -------------------------------------------------------------------------------- /transpose of a matrice.c: -------------------------------------------------------------------------------- 1 | #include 2 | int main() 3 | { 4 | int a[3][3],b[3][3],i,j; 5 | printf("enter the elements of the matrice\n"); 6 | for(i=0;i<3;i++) 7 | for(j=0;j<3;j++) 8 | scanf("%d",&a[i][j]); 9 | printf("the entered matrice is\n"); 10 | for(i=0;i<3;i++) 11 | { 12 | for(j=0;j<3;j++) 13 | printf("%d\t",a[i][j]); 14 | printf("\n"); 15 | } 16 | for(i=0;i<3;i++) 17 | for(j=0;j<3;j++) 18 | b[i][j]=a[j][i]; 19 | printf("the transpose of the following matrice is\n"); 20 | for(i=0;i<3;i++) 21 | { 22 | for(j=0;j<3;j++) 23 | printf("%d\t",b[i][j]); 24 | printf("\n"); 25 | } 26 | return 0; 27 | } 28 | --------------------------------------------------------------------------------