int main() { int i,j,sum,r; scanf("%d",&n); for (i = 1; i <= n; i++) { for (j = 1; j<=n ; j++) { scanf("%d",&dis[i][j]); } } j = findmin(1); fill[1] = 0; sum = dis[1][j]; //printf("%d\n",sum); for (i = 1; i<n-1 ;i++) { r = findmin(j); fill[r] = 0; sum = sum + dis[j][r]; //printf("%d %d\n",dis[j][r],sum); j = r; } //r = findmin(j); //printf("%d %d\n",r,sum); sum = sum + dis[r][1]; //printf("%d %d\n",r,sum); printf("%d",sum); return 0; }
ui findmin(int i,ui val){ if(i==n-1) return val; ui a,b,c,m; m=val* (arr[i+1]); a=findmin(i+1,m); m=val- (arr[i+1]); b=findmin(i+1,m); m=val+ (arr[i+1]); c=findmin(i+1,m); m=gmin(gmin(a,b),c); return m; }
//four conditions //1.2. recursively run the bst to check its validation //3.4. root value should obey the rules bool isValidBST(TreeNode *root) { if(!root) return true; return isValidBST(root->left) && isValidBST(root->right) && (!root->left || (findmax(root->left) < root->val)) && (!root->right || (findmin(root->right) > root->val)); }
int main () { int option,n,i,m; min=NIL; while(1) {printf("\nmenu\n"); printf("1:create fibonacci heap\n"); printf("2:insert in fibonacci heap\n"); printf("3: find min in fibonacci heap \n"); printf("4:display\n"); printf("5: exit \n"); scanf ("%d",&option); switch(option) { case 1 :create_fib(); break; case 2: printf("\nenter the element= "); scanf("%d",&n); Finsert(n); break; case 3: findmin(); break; case 4: display(min1); break; case 5 :exit(1); default: printf("\nwrong choice... try again \n "); } } }
int main() { clrscr(); int num_of_pages,k; int reference_string[20]; cout<<"Enter reference string "; for(int i=0;i<20;i++) { cin>>reference_string[i]; } cout<<"Enter the number of pages "; cin>>num_of_pages; page repr[10]; for(i=0;i<10;i++) { repr[i].number=-1; repr[i].time=0; } cout<<"The pages will be replaced as follows (-1 denotes empty slot)\n"; for(i=0;i<20;i++) { int temp=reference_string[i]; if(findrepr(repr,num_of_pages,temp)) continue; else { int k=findmin(repr,num_of_pages); repr[k].number=temp;repr[k].time=i+1; print(repr,num_of_pages);cout<<"\n"; } } getch(); return 0; }
int main() { char s1[] = { "Hello" }; char s2[] = { "raBiD" }; int compres1 = strcmp_opp(s1, s2); int compres2 = strcmp_capital(s1, s2); printf("Comparison 1st function: %d\nComparison 2nd function: %d\n\n", compres1, compres2); int (*cmpfnc) (const char*, const char*); cmpfnc = &strcmp_opp; printf("Comparison 1st function: %s\n", findmin(s1, s2, cmpfnc)); cmpfnc = &strcmp_capital; printf("Comparison 2nd function: %s\n", findmin(s1, s2, cmpfnc)); return 0; }
int main(){ char end; scanf("%d",&end); char a[1000][3],check[3]; int n=0; for(int i=0;check!='A A';i++){ scanf("%d",a[i]); check=a[i]; n++; } int *nodes; *nodes=0; int *routes; *routes=0; int *minimum; *minimum=0; char route[1000][2000],f='F'; node(a,f,n,nodes,end,routes,route); int routes1=*routes; if(routes1==0){ printf("No Route Available from F to Z"); } else{ int minroute=findmin(route,routes1,minimum); int mins=*minimum; printf("Total Routes: %d",routes1); printf("Shortest Route Length: %d",mins); printf("Shortest Route after Sorting of Routes of length 4:"); for(int p=0;route[ret][p]!='\0') printf(" %c",route[ret][p]); } printf("\n"); return 0; }
void sort(int array[], int length) { int lcv1;//loop control vairable int lcv2;//loop control vairable int lcv3 = 0;//loop control variable int temp;//temp variable to hold value int loc = 0;//location of temp variable while ((!checkascend(array, length)) && lcv3 < length) { temp = 999999999;//set temp to max value for(lcv1 = 0; lcv1 < length; lcv1++) { if(findmin(array, lcv1, length)) { if(temp > array[lcv1]) { temp = array[lcv1]; loc = lcv1; } } } for(lcv2 = loc; lcv2 < length - 1; lcv2++) { array[lcv2] = array[lcv2 + 1]; } array[length - 1] = temp; printdata(array, length, lcv3 + 1); lcv3++; } printmoves(lcv3); }
struct bsnode* Delete( struct bsnode* root,char data[]) { if(root==NULL) return root; else if (toint(root->data)>toint(data)) root->left= Delete(root->left,data); else if (toint(root->data)<toint(data)) root->right=Delete(root->right,data); else { if(root->right==NULL && root->left==NULL) //free root; root=NULL; else if (root->right==NULL) { struct bstree* temp=root; root = root->left; //free temp; } else if (root->left==NULL) { struct bstree* temp1=root; root= root->right;} else { struct bsnode* temp=findmin(root->right ); strcpy( root->data,temp->data); root->right=Delete(root->right,temp->data); } } return root; }
int FindPath(int city[500][500],int team[],int n,int c1,int c2) { int i,j,t; int ct[500]={0}; for(i=0;i<n;i++) path[i]=100000; path[c1]=0; people[c1]=team[c1]; ct[c1]=1; while(1) { t=findmin(path,n,visited); if(t==c2) break; visited[t]=1; for(i=0;i<n;i++) if(city[t][i]!=0&&visited[i]!=1) { if(path[i]>path[t]+city[t][i]) { path[i]=path[t]+city[t][i]; ct[i]=ct[t]; people[i]=people[t]+team[i]; } else if(path[i]==path[t]+city[t][i]) { ct[i]+=ct[t]; if(people[i]<people[t]+team[i]) people[i]=people[t]+team[i]; } } } return ct[c2]; }
node * deleteval( node * t, int x ) { node * temp; if( x < t->val ) t->left = deleteval( t->left, x ); else if( x > t->val ) t->right = deleteval( t->right, x ); else // x == t->val { if ( t->left == NULL && t->right == NULL ) { t = NULL; } else if ( t->left && t->right ) { temp = findmin(t->right); t->val = temp->val; t->right = deleteval(t->right, t->val); } else if (t->left == NULL) t = t->right; else t = t->left; } }
void makequeue(struct p_info arr[n]) //sorting with bubble sort { int i=0,j=0; struct p_info temp; for(i=0;i<n;i++) { for(j=0;j<n-1;j++) { if(arr[j].artime>arr[j+1].artime) { temp.extime=arr[j].extime; temp.artime=arr[j].artime; temp.et=arr[j].et; arr[j].extime=arr[j+1].extime; arr[j].artime=arr[j+1].artime; arr[j].et=arr[j+1].et; arr[j+1].extime=temp.extime; arr[j+1].artime=temp.artime; arr[j+1].et=temp.et; } } } arr[0].qintime=arr[0].artime; pqin=arr[0].qintime; arr[0].qouttime=arr[0].artime+arr[0].extime; pqout=arr[0].qouttime; arr[0].state=0; arr[0].et=0 ; pqin=pqout; int x=findmin(arr,arr[0].qouttime); //running from second process; for(i=1;i<n;i++) { if(x!=-1) // found with min ex.time with in pqout time { arr[x].qintime=pqin; arr[x].qouttime=pqin+arr[x].extime; arr[x].et,arr[x].state=0; pqout=arr[x].qouttime; i=1; } else // if not found running first of other process and eliminating ideal times { if(arr[i].state) { arr[i].qintime=arr[i].artime; pqin=arr[i].artime; arr[i].qouttime=pqin+arr[i].extime; arr[i].et,arr[i].state=0; pqout=arr[i].qouttime; } } pqin=pqout; x=findmin(arr,pqout); } }
node *findmin(node *root) { node *temp; if(root==NULL) return NULL; else if(!root->left) return root; else return findmin(root->left); }
struct node * findmin(struct node *p) { if (p==NULL) printf("error: (avl_any) findmin: empty tree\n"); else if (p->left == NULL) return p; else return findmin(p->left); }
int main(void){ int i,t;scanf("%d",&t); assert(1<=t && t<=100000); while(t--){scanf("%d",&n); assert(n>=1&&n<=10); for(i=0;i<n;i++){ scanf("%d",arr+i); assert(arr[i]>=-9 && arr[i]<=9); } printf("%lld\n",findmin(0,arr[0])); } return 0; }
main() { int n,value; bst t=NULL,x; while(1) { printf("enter 1 to insert any number in the tree\n"); printf("enter 2 to find any number.\n"); printf("enter 3 to find minimum\n"); printf("enter 4 to find maximum\n"); printf("enter 5 to delete any number\n"); printf("enter 6 to exit\n "); scanf("%d",&n); if(n==1) { printf("Enter the number to insert : "); scanf("%d",&value); t=insert(value,t); // continue; } if(n==2) { printf("Enter the number to find : "); scanf("%d",&value); find (value,t); } if(n==3) {// printf("in min loop\n"); x=findmin(t); if(x!=NULL) printf("%d\n",x->item); } if(n==4) { x=findmax(t); if(x!=NULL) printf("%d\n",x->item); } if(n==5) { int value; printf("enter the number to be deleted : "); scanf("%d",&value); del(value,t); printf("the number is deleted\n"); } if(n==6) break; } }
int main(){ int i,T; float max,min; scanf("%d", &T); float* arr = malloc(sizeof(float*)*T); for (i = 0; i < T; i++){ scanf("%f", &arr[i]); } max = findmax(arr, T); min = findmin(arr, T); printf("%.2f", max-min); return 0; }
bool isBST(Node* root) { if (!root) return 1; if (root->left && findmax(root->left)->data > (root->data)) return 0; if (root->right && findmin(root->right)->data < (root->data)) return 0; if (!isBST(root->left) || !isBST(root->right)) return 0; return 1; }
struct node * del(avl_any *t, any x, struct node *p) { struct node * n; // do nothing - data not in tree if (p == NULL) ; else if (LT(x,p->item)) p->left = del(t, x, p->left); else if (LT(p->item,x)) p->right = del(t, x, p->right); // found and has two children // get smallest in right tree // overwrite the item to be deleted // del the duplicate item in right tree else if (p->left!=NULL && p->right!=NULL) { n = findmin(p->right); p->item = n->item; p->right = del(t, p->item, p->right); } // found and has at most one child else { n = p; if(p->left == NULL) // promote the right tree p = p->right; else if (p->right == NULL) // promote the left tree p = p->left; (t->size)--; // reduce the count SAFE_FREE(n); // reclaim memory } return ensure_balance(p); }
void tree::remove(node* current, node* parent, int d) { if(current) { if(d < current->data) { remove(current->left, current, d); } else if(d > current->data) { remove(current->right, current, d); } else { if(current->left && current->right) { current->data = findmin(current->right)->data; remove(current->right, current, d); } else if (current == parent->left) { parent->left = current->left ? current->left : current->right; delete current; } else if (current == parent->right) { parent->right = current->left ? current->left : current->right; delete current; } } } }
int minDistance(string word1, string word2) { // Start typing your C/C++ solution below // DO NOT write int main() function int len1=word1.size(),len2=word2.size(); vector<vector<int> > res(len1+1,vector<int>(len2+1,0)); for(int i=0;i<len1+1;i++) res[i][0]=i; for(int i=0;i<len2+1;i++) res[0][i]=i; for(int i=1;i<len1+1;i++){ for(int j=1;j<len2+1;j++){ if(word1[i-1]==word2[j-1]) res[i][j]=res[i-1][j-1]; else res[i][j]=1+findmin(res[i-1][j],res[i][j-1],res[i-1][j-1]); } } return res[len1][len2]; }
void main() { char ans='n'; printf("Want to create a BST (y/n) ? :"); scanf("%c",&ans); node *root=NULL; int data=0,count=0,k=0; while(ans=='y') { node *new_node; printf("\nEnter the element = "); scanf("%d",&data); new_node=get_node(data); if(root==NULL) root=new_node; else insert(root,new_node,data); printf("\nWant to add more elements(y/n)? : "); ans=getch(); } printf("\nThe BST created is: "); display(root); printf("\nsmallest node= %d",findmin(root)->data); printf("\nlargest node= %d",findmax(root)->data); printf("\nEnter the value of k for kth smallest node = "); scanf("%d",&k); findkthsmallest(root,k); printf("\nDo you want to delete a node (y/n)? :"); ans=getch(); if(ans=='y') { printf("\nEnter the element you want to delete = "); scanf("%d",&data); root=deletenode(root,data); printf("\nThe BST after deletion is: "); display(root); } }
int main() { int n, cha, i, j, b[30001], max, counter, min, max1; scanf("%d %d", &n, &cha); for (i = 0; i < n; i++) scanf("%d", &a[i]); for (i = 0; i < n; i++) { max = min = a[i]; for (j = i + 1, counter = 1; j < n; j++) { min = findmin(i, j); max = findmax(i, j); if (max - min > cha) break; counter++; } b[i] = counter; } max1 = b[0]; for (i = 1; i < n; i++) if (b[i] > max1) max1 = b[i]; printf("%d\n", max1); return 0; }
int main (void) { FILE *fin, *fout; int amount, farmer, i, sum, min, quan; Provider milk[MAXF]; fin = fopen("milk.in", "r"); fout = fopen("milk.out", "w"); fscanf(fin, "%d %d", &amount, &farmer); for (i = 0; i < farmer; i++) fscanf(fin, "%d %d", &milk[i].price, &milk[i].amount); for (sum = 0; amount; milk[min].price = 1001) { min = findmin(milk, farmer); quan = checkamount(milk[min], amount); sum += quan * milk[min].price; amount -= quan; } fprintf(fout, "%d\n", sum); return 0; }
void dijkstra(int graph[V][V] , int source) { bool spt[V]; int wgtspt[V]; //matrix to store the min path value found yet int parent[V]; int i; for(i=0;i<V;i++) { spt[i] = 0; wgtspt[i] = INT_MAX; } wgtspt[source] = 0; parent[source] = source; int count; for(count=0;count<V-1;count++) { int u = findmin(spt,wgtspt); spt[u] = 1; int v; for(v=0;v<V;v++) { if(spt[v] == 0 && graph[u][v] && wgtspt[u] != INT_MAX && wgtspt[u] + graph[u][v] < wgtspt[v]) { wgtspt[v] = wgtspt[u] + graph[u][v]; parent[v] = u; } } } printspt(graph,parent,wgtspt); }
long init_time( char *path, double *start_time, double *end_time) { int access_return_status; int read_input_directory( char *, int); int verbose_flag= VERBOSE_FLAG, return_status = 0; /* check directory path for exisistence & READ permission */ if (verbose_flag) { printf( "INSIDE init_time(): path = %s\n", path); } access_return_status = access( path, R_OK); if (verbose_flag) { printf( "access( %s, R_OK ) = %d\n", path, access_return_status); } if (access_return_status != 0) { printf( "\nERROR: will not be able to read output from -->%s<-- \n", path); exit(1); } else { if (verbose_flag) { printf( "\ninput directory %s READ OK...\n", path); } } /* start reading contents of the directory */ return_status = read_input_directory( path, verbose_flag); if (return_status <= 1) { printf( "\n%s - ERROR:\tNot enough time steps in directory %s\nUse regular " "interpolator for single files...\nEXITING PROGRAM.\n", __FILE__, path ); exit( EXIT_FAILURE );} start_time[0] = findmin( rootPtr ); end_time[0] = findmax( rootPtr ); /*** lets keep a copy of these for ourselves, use later on inside time_interpolate for bounds checking ***/ stored_start_time = start_time[0]; stored_end_time = end_time[0]; return 0; }
node* tree::findmin(node* current) { if(current->left) return findmin(current->left); else return current; }
int split_expense(char * mem[16], struct name * arruser[16]){ int array_expense[16]; int i = 0, min, max; int sum, count = 0; char arr1[64]; struct data * curr; struct list *curr1; while(i != 16){ sum = 0; if(arruser[i] == NULL){ array_expense[i] = INT_MIN; i++; } else{ curr = arruser[i]->q; while(curr){ sum = sum + curr->amt; curr = curr->n; } array_expense[i] = sum; i++; } } for(i = 0; i< 16; i++){ if(array_expense[i] != INT_MIN){ count++; sum = sum + array_expense[i]; } } if(count == 0){ printf(" all accounts are settled\n"); print_line(); return 1; } sum = sum / count; for(i = 0; i< 16; i++){ if(array_expense[i] != INT_MIN){ array_expense[i] = array_expense[i] - sum; } } min = findmin(array_expense); max = findmax(array_expense); while(min != (-1) && max != (-1) && (max!= min)){ printf("%s pays %s an amount of rs %d\n",mem[min], mem[max], (-1) * array_expense[min]); if(arruser[min] == NULL){ arruser[min] = (struct name*)malloc(sizeof(struct name)); arruser[min]->num_stmt = 0; arruser[min]->num_data = 0; arruser[min]->p = NULL; arruser[min]->q = NULL; } if(arruser[min]->p == NULL){ arruser[min]->p = (struct list*) malloc(sizeof(struct list)); sprintf(arr1, "you owe %s an amount of rs %d", mem[max], (-1)*array_expense[min]); arruser[min]->p->stmt = (char*) malloc(64); strcpy(arruser[min]->p->stmt, arr1); arruser[min]->p->next = NULL; } else{ curr1 = arruser[min]->p; while(curr1->next){ curr1 = curr1->next; } curr1->next = (struct list*) malloc(sizeof(struct list)); sprintf(arr1, "you owe %s an amount of rs %d", mem[max], (-1)*array_expense[min]); curr1->next->stmt = (char *)malloc(64); strcpy(curr1->next->stmt, arr1); curr1->next->next = NULL; } if(arruser[max] == NULL){ arruser[max] = (struct name*)malloc(sizeof(struct name)); arruser[max]->num_stmt = 0; arruser[max]->num_data = 0; arruser[max]->p = NULL; arruser[max]->q = NULL; } if(arruser[max]->p == NULL){ arruser[max]->p = (struct list*) malloc(sizeof(struct list)); sprintf(arr1, "%s owes you an amount of rs %d", mem[min], (-1)*array_expense[min]); arruser[max]->p->stmt = (char*) malloc(64); strcpy(arruser[max]->p->stmt, arr1); arruser[max]->p->next = NULL; } else{ curr1 = arruser[max]->p; while(curr1->next){ curr1 = curr1->next; } curr1->next = (struct list*) malloc(sizeof(struct list)); sprintf(arr1, "%s owes you an amount of rs %d", mem[min], (-1)*array_expense[min]); curr1->next->stmt = (char*)malloc(64); strcpy(curr1->next->stmt, arr1); curr1->next->next = NULL; } arruser[min]->num_stmt += 1; arruser[max]->num_stmt += 1; array_expense[max] += array_expense[min]; array_expense[min] = 0; min = findmin(array_expense); max = findmax(array_expense); } reset_data(arruser); print_line(); return 1; }
tree *findmin(tree *root){ if((*root) == NULL ||root == NULL) return NULL; if((*root)->left == NULL) return root; return findmin(&((*root)->left)); }
void deletemin(tree *root,void *key,int (*cmp)(const void *,const void *)){ if(key == NULL || root == NULL) { PERROR; exit(1); } if(*root == NULL) { return ; } tree * found = search(root,key,cmp); if(found == NULL || *found == NULL ) { fprintf(stderr,"ERROR : not found key !! \n"); return ; } else { if(((*found)->count) > 1) { ((*found)->count)--; return ; } if(isLeaf(*found)) { // if leaf free((*found)->element); // free key free((*found)); // free node (*found) = NULL; return ; } else { if((*found)->left == NULL || (*found)->right == NULL) { if((*found)->left == NULL) { tree del = (*found); (*found) = (*found)->right; free(del->element); free(del); } if((*found)->right == NULL) { tree del = (*found); (*found) = (*found)->left; free(del->element); free(del); } } else { tree *min = findmin(&((*found)->right)); // find min if(!isLeaf(*min)) { tree *max = findmax(&((*found)->left)); if(!isLeaf(*max)){ if(nb_nodes((*found)->right) > nb_nodes((*found)->left)) { SWAP((*found)->element,(*min)->element); tree del = *min; (*min) = (*min)->right; free((del)->element); free((del)); (del) = NULL; } else { SWAP((*found)->element,(*max)->element); tree del = *max; (*max) = (*max)->left; free((del)->element); free((del)); (del) = NULL; } } else { SWAP((*max)->element,(*found)->element); free((*max)->element); free((*max)); (*max) = NULL; } } else { SWAP((*min)->element,(*found)->element); free((*min)->element); free((*min)); (*min) = NULL; } } } } }