Maximum Subarray Sum Using Divide and Conquer Approach: Code: - (Without Array Elements Print)
Maximum Subarray Sum Using Divide and Conquer Approach: Code: - (Without Array Elements Print)
Maximum Subarray Sum Using Divide and Conquer Approach: Code: - (Without Array Elements Print)
Approach
Code :-[without array elements print]
#include <stdio.h>
#include <limits.h>
int max(int x, int y) {
return (x > y) ? x : y;
}
int maximum_sum(int nums[], int low, int high)
{
if (high <= low) {
return nums[low];
}
int mid = (low + high) / 2;
int left_max = INT_MIN;
int sum = 0;
for (int i = mid; i >= low; i--)
{
sum += nums[i];
if (sum > left_max) {
left_max = sum;
}
}
int right_max = INT_MIN;
sum = 0; // reset sum to 0
for (int i = mid + 1; i <= high; i++)
{
sum += nums[i];
if (sum > right_max) {
right_max = sum;
}
}
int max_left_right = max(maximum_sum(nums, low, mid),
maximum_sum(nums, mid + 1, high));
return max(max_left_right, left_max + right_max);
}
int main(void)
{
int arr[] = { 2, -4, 1, 9, -6, 7, -3 };
int n = sizeof(arr) / sizeof(arr[0]);
printf("The maximum sum of the subarray is %d",
maximum_sum(arr, 0, n - 1));
return 0;
}
---------------------------------
#include <stdio.h>
#include <limits.h>
return (a > b) ? a : b;
int maxCrossingSum(int arr[], int l, int m, int r, int *start, int *end) {
int sum = 0;
left_sum = sum;
*start = i;
sum = 0;
right_sum = sum;
*end = i;
if (l == r) {
*start = l;
*end = r;
return arr[l];
int m = l + (r - l) / 2;
*start = left_start;
*end = left_end;
return max_left_sum;
*start = right_start;
*end = right_end;
return max_right_sum;
} else {
*start = cross_start;
*end = cross_end;
return max_cross_sum;
int main() {
int n,i;
scanf("%d",&n);
int arr[n];
for(i=0;i<n;i++)
scanf("%d",&arr[i]);
printf("\n");
return 0;
}
Code :-
// A C program to find minimum possible time by the car chassis to complete
#include <stdio.h>
#define NUM_LINE 2
//#define NUM_STATION 4
int NUM_STATION;
int e[NUM_LINE],x[NUM_LINE];
int main()
{
int i,j;
printf("enter the no of stations:");
scanf("%d",&NUM_STATION);
int a[NUM_LINE][NUM_STATION];
int t[NUM_LINE][NUM_STATION];
printf("\nenter the processing times:");
for(i=0;i<NUM_LINE;i++)
{
for(j=0;j<NUM_STATION;j++)
{
scanf("%d",&a[i][j]);
}
}
printf("\nenter the transfer times:");
for(i=0;i<NUM_LINE;i++)
{
for(j=0;j<NUM_STATION;j++)
{
scanf("%d",&t[i][j]);
}
}
for(i=0;i<NUM_LINE;i++)
{
scanf("%d",&x[i]);
}
// int e[NUM_LINE] = {10, 12}, x[NUM_LINE] = {18, 7};
printf("\nThe min time is :%d", carAssembly(a, t, e, x));
return 0; }
Longest Common Subsequence :-
(length of lcs and table and also the words print)
Code :-
#include <stdio.h>
#include <string.h>
int main() {
char str1[MAX_LEN], str2[MAX_LEN];
int len1, len2, i, j;
int dp[len1+1][len2+1];
char lcs[MAX_LEN];
int index = dp[len1][len2];
lcs[index] = '\0';
i = len1;
j = len2;
int main() {
int n;
floyd_warshall(graph, n);
for(int i = 0; i < n; i++) {
free(graph[i]);
}
free(graph);
return 0;
}
Grahams Scan :-
#include <stdio.h>
#include <math.h>
#include <string.h>
#include <iostream>
#include <vector>
#include <list>
#include <string>
#include <algorithm>
#include <queue>
#include <stack>
#include <set>
#include <map>
#include <complex>
#define MAX_N 100001
#define INF 987654321
using namespace std;
typedef long long lld;
struct Point
{
double X, Y;
Point()
{
this->X = this->Y = 0;
}
Point(double x, double y)
{
this->X = x;
this->Y = y;
}
};
int n;
Point P[MAX_N];
Point R;
P[0] = P[n];
int HullSize = 1;
int main()
{
int i,a,b,p;
printf("enter the no of points:");
scanf("%d",&n);
for(int i=0;i<n;i++)
{
p=i+1;
scanf("%d %d",&a,&b);
P[p]=Point(a,b);
}
// P[1] = Point(-7, 8);
// P[2] = Point(-4, 6);
// P[3] = Point(2,6);
// P[4] = Point(6, 4);
// P[5] = Point(8, 6);
// P[6] = Point(7, -2);
// P[7] = Point(4,-6);
// P[8] = Point(8,-7);
// P[9] = Point(0, 0);
// P[10] = Point(3, -2);
// P[11] = Point(6,-10);
// P[12] = Point(0, -6);
// P[13] = Point(-9, -5);
// P[14] = Point(-8, -2);
// P[15] = Point(-8,0);
// P[16] = Point(-10,3);
// P[17] = Point(-2, 2);
// P[18] = Point(-10,4);
//P[16] = Point(-10,3);
vector<Point> cH;
int m = GrahamScan(cH);
return 0;
}
swap(&arr[i+1], &arr[high]);
return i+1;
}
quicksort(arr, 0, n-1);
return 0;
}
struct Table
{
int d, pi;
}V[N];
void init(int s)
{
for (int v = 0; v<N; v++)
{
V[v].d = INF;
V[v].pi = -1;
}
V[s].d = 0;
}
int main()
{
int G[N][N] = {{0, 6, 4, 5, 0, 0},
{0, 0, 0, 0, -1, 0},
{0, -2, 0, 0, 3, 0},
{0, 0, -2, 0, 0, -1},
{0, 0, 0, 0, 0, 3},
{0, 0, 0, 0, 0, 0}};
for(int i=0;i<N;i++)
{
for(int j=0;j<N;j++)
{
scanf("%d",&G[i][j]);
}
}
int s = 0;
bellmanFord(G, s);
struct BFSTable
{
int color;
int d;
int pi;
}V[N];
struct Queue
{
int q[100];
int f;
int r;
}Q;
void initQueue()
{
Q.f = Q.r = -1;
}
int isEmpty()
{
if ((Q.r == -1) || (Q.f == Q.r+1))
return 1;
else
return 0;
}
int dequeue()
{
if (!isEmpty())
{
return Q.q[Q.f++];
}
}
void init(int s)
{
for (int v=0; v<N; v++)
{
V[v].color = WHITE;
V[v].d = INF;
V[v].pi = -1;
}
V[s].d = 0;
V[s].pi = -1;
}
V[s].color = GRAY;
enqueue(s);
while (!isEmpty())
{
int u;
u = dequeue();
for (int v=0; v<N; v++)
{
if (G[u][v]!=0)
{
if ((V[v].color==WHITE) && (r[u][v]>0))
{
if (v==t)
{
V[v].pi = u;
V[v].d = V[u].d+1;
return 1;
}
V[v].color=GRAY;
V[v].d = V[u].d + 1;
V[v].pi = u;
enqueue(v);
}
}
}
V[u].color = BLACK;
}
return 0;
int mf = 0;
while (bfs(G, s, r, t))
{
int bnc = INF;
printTable();
printf("\n");
for (int v=t; v!=s; v=V[v].pi)
{
int u = V[v].pi;
bnc = min(bnc, r[u][v]);
}
mf = mf + bnc;
void printTable()
{
printf("\n BFS Table is : \n");
printf("\n Node \t CLR \t DIST \t PI \n");
printf("------------------------------------\n");
void main()
{
int G[N][N] = {{0, 7, 0, 0, 4, 0},
{0, 0, 5, 3, 0, 0},
{0, 0, 0, 0, 0, 8},
{0, 0, 3, 0, 0, 5},
{0, 3, 0, 2, 0, 0},
{0, 0, 0, 0, 0, 0}};
int s;
int t;
s = 0;
t = 5;
edmondKarp(G, s, t);
//printTable();
//printPath();
}
int graph[MAX_VERTICES][MAX_VERTICES];
int residual[MAX_VERTICES][MAX_VERTICES];
int parent[MAX_VERTICES];
int flow[MAX_VERTICES];
bool visited[MAX_VERTICES];
int main() {
printf("Enter the number of vertices: ");
scanf("%d", &n);
printf("Enter the adjacency matrix of the graph:\n");
for (int u = 0; u < n; u++) {
for (int v = 0; v < n; v++) {
scanf("%d", &graph[u][v]);
}
}
int s = 0, t = n-1; // Source and sink vertices
int max_flow = edmonds_karp(s, t);
printf("Maximum flow = %d\n", max_flow);
printf("Path of vertices with maximum flow: ");
int u = s;
while (u != t) {
for (int v = 0; v < n; v++) {
if (residual[u][v] < graph[u][v]) {
printf("%d ", u+1);
u = v;
break;
}
}
}
printf("%d\n", t+1);
return 0;
}
Fractional Knapsack :-
#include<stdio.h>
void knapsack(int n, float weight[], float profit[], float capacity)
{
float x[20], tp = 0;
int i, j, u;
u = capacity;
if (i < n)
x[i] = u / weight[i];
tp = tp + (x[i] * profit[i]);
printf("\nThe result vector is:- ");
for (i = 0; i < n; i++)
printf("%f\t", x[i]);
int main() {
float weight[20], profit[20], capacity;
int num, i, j;
float ratio[20], temp;
temp = weight[j];
weight[j] = weight[i];
weight[i] = temp;
temp = profit[j];
profit[j] = profit[i];
profit[i] = temp;
}
}
}
Naive algorithm :-
Code :-
#include<iostream>
#include<string.h>
using namespace std;
if (j== M)
cout << "The match is found: " << i+1 <<" - "<<i+j<< endl;
}
}
int main()
{
char txt[100] ;
char pat[100] ;
cin>>txt>>pat;
search(pat, txt);
return 0;
}
Rabin Karp :-
#include<iostream>
#include <string.h>
#define d 10
int m = strlen(pattern);
int n = strlen(text);
int i, j;
int p = 0;
int t = 0;
int h = 1;
h = (h * d) % q;
p = (d * p + pattern[i]) % q;
t = (d * t + text[i]) % q;
if (p == t) {
if (text[i + j] != pattern[j])
break;
if (j == m)
if (i < n - m) {
if (t < 0)
t = (t + q);
}
}
int main() {
char text[100];
char pattern[100];
cin>>text>>pattern;
int q = 13;
#define MJ 100
#define MT 100
struct J {
int p;
int d;
int t;
};
int main() {
struct J JOBS[MJ];
int n, i, j, tp = 0;
profit={5,10,6,3}
deadline={1,3,2,1}
time = {1,2,1,1}
*/
for (i = 0; i < n; i++) {
JOBS[i].p=p[i];
}
for (i = 0; i < n; i++) {
JOBS[i].d=d[i];
}
for (i = 0; i < n; i++) {
JOBS[i].t=t[i];
}
printf("max profit=%d",tp);
return 0;
}
return result;
}
// Base cases
if (n == 0) return 0;
if (n == 1) return multiplyiSingleBit(X, Y);
int fh = n/2;
int sh = (n-fh);
int main()
{
printf ("%ld\n", multiply("1100", "1010"));
printf ("%ld\n", multiply("110", "1010"));
printf ("%ld\n", multiply("11", "1010"));
printf ("%ld\n", multiply("1", "1010"));
printf ("%ld\n", multiply("0", "1010"));
printf ("%ld\n", multiply("111", "111"));
printf ("%ld\n", multiply("11", "11"));
}