You are on page 1of 11

Name Atul Kumar Gupta

SID 21103085

Branch CSE

Assignment No. 3

Submitted to Dr. Padmavati Khandnor


1. The goal in this problem is to find the minimum number of coins needed
to change the input value (an integer) into coins with denominations 1, 5,
and 10. The input consists of a single integer m. 1≤m≤103 Output the
minimum number of coins with denominations 1, 5, 10 that changes m
Soln.
CODE:

#include<bits/stdc++.h>
usingnamespacestd;

inthelper(intnumD,vector<int>deno,intvalue){
if(value<=0){return0;}
int* ans=newint[value+1];
for(inti=0;i<=value;i++){
ans[i]=INT_MAX;
}
ans[0]=0;
for(inti=1;i<=value;i++){
inta=INT_MAX,b=INT_MAX,c=INT_MAX;
a=ans[i-deno[0]]+1;
if(i-deno[1]>=0){
b=ans[i-deno[1]]+1;
}
if(i-deno[2]>=0){
c=ans[i-deno[2]]+1;
}
ans[i]=min(a,min(b,c));
}
returnans[value];
}

intmin_coin(intn){
vector<int>coins;
coins.push_back(1);
coins.push_back(5);
coins.push_back(10);
returnhelper(3,coins,n);
}

intmain(){
cout<<"Name: Atul kumar gupta SID: 21103085\n";
cout<<"Enter the value\n";
intn;
cin>>n;
cout<<"The minimun number of coins are: ";
cout<<min_coin(n)<<endl;;
}
OUTPUT:

2. You are going to travel to another city that is locatedd miles away from
your homecity. Your car can travel at mostmmiles on a full tank and you
start with a full tank. Along your way, there are gas stations at distances
stop1 stop2 . . . ,stopN from yourhome city. What is the minimum
number of refills needed?

Soln:

CODE:
#include<bits/stdc++.h>
usingnamespacestd;

intsolve(inttotal,intmiles,vector<int>&input){
inttemp=miles;
intans=0;
for(inti=0;i<input.size()-1;i++){
if(input[i+1]>=temp&&input[i]<=temp){
temp=input[i]+miles;
ans++;
if((total-input[i])==0){
--ans;
returnans;
}

}
}
return -1;
}

intmain(){
cout<<"Name: Atul Kumar Gupta SID: 21103085\n";
inttotal;
cout<<"Enter total miles: ";
cin>>total;
cout<<endl;

intmiles;
cout<<"Enter miles car can cover 1 time: ";
cin>>miles;
cout<<endl;

vector<int>input;
intk;

intj;
cout<<"Enter the value of j: ";
cin>>j;
// while(i!=-1){
// cin>>i;
// if(i!=-1){
// input.push_back(i);
// }

// }

cout<<"Enter here"<<endl;

for(inti=0;i<j;i++){
cin>>k;
input.push_back(k);
}
input.push_back(total);
input.push_back(5000000);

intans=solve(total,miles,input);

cout<<ans;
}
OUTPUT:

3. You are given a set of segments on a line and your goal is to mark as few
points on a line as possible so that each segment contains at least one
marked point.
Problem Description
Task. Given a set of n segments {[a(0) ,b(0) ],[a(1) ,b(1) ],…,[a(n)−1
,b(n)−1 ]} with integer coordinates on a line, find the minimum number
m of points such that each segment contains at least one point. That is,
find a set of integers X of the minimum size such that for any segment
[a(i) ,b(i) ] there is a point x ∈ X such that a(i) ≤ x ≤ b(i) .Input Format.
The first line of the input contains the number n of segments. Each of the
followingn lines contains two integers a(i) and b(i) (separated by a space)
defining the coordinates of endpoints of the i-th segment.
Constraints. 1 ≤ n ≤ 100; 0 ≤ a(i) ≤ b(i) ≤ 10^9 for all 0 ≤ i< n.
Output Format. Output the minimum number m of points on the first line
and the integercoordinates of m points (separated by spaces) on the second
line. You can output the points in any order. If there are many such sets of
points, you can output any set. (It is not difficult to see that there always
exist a set of points of the minimum size such that all the coordinates of the
points are integers.)
Soln:
CODE:

#include<bits/stdc++.h>
usingnamespacestd;

boolendpoint(constpair<int, int>p1,constpair<int,
int>p2)
{
returnp1.second<p2.second;
}

intmain(){
cout<<"Name: Atul kumar gupta SID:21103085\n";
cout<<"Enter number of lines\n";
intnum_of_lines;
cin>>num_of_lines;
vector<pair<int,int>>v(num_of_lines);
cout<<"Enter starting and endiing points of line
segments\n";
for(inti=0;i<num_of_lines;i++){
cin>>v[i].first;
cin>>v[i].second;
}
vector<int>points;
sort(v.begin(),v.end(),endpoint);
inti=0;
while(i<num_of_lines){
intmarked=v[i].second;
intstart=v[i].first;
points.push_back(marked);
if(i>=num_of_lines-1){
break;
}

while(v[i].first<=marked){
i++;
if(i>=num_of_lines){
break;
}
}

}
cout<<"The points are as follows\n";
cout<<points.size()<<endl;
for(inti=0;i<points.size();i++){
cout<<points[i]<<" ";
}cout<<endl;
return0;
}

OUTPUT:
4. Compute and print the total weight of the Minimum Spanning Tree
(MST) for the given graph using Kruskal’s algorithm.

Soln:
CODE:

#include<iostream>
#include<algorithm>
usingnamespacestd;
classedge{
public:
intv1;
intv2;
intweight;
};

boolcomp(edgea,edgeb){
returna.weight<b.weight;
}
intw=0;
voidkruskals(intv,inte,edge*edges){

int *parents = newint[v];


for(inti=0;i<v;i++){
parents[i]=i;
}
edge * ans =newedge[v-1];
intcount =0;
inti=0;
while(count<v-1){
edgetemp = edges[i];
ints = temp.v1;
inte = temp.v2;
intparentss =s;
intparente =e;
while(parentss!=parents[parentss]){
parentss= parents[parentss];
}
while(parente!=parents[parente]){
parente= parents[parente];
}
if(parentss!=parente){
ans[count]=temp;
count++;
parents[parentss]= parente;
}
i++;

}
cout<<"The MST is as follows:\n";
for(inti=0;i<v-1;i++){
if(ans[i].v1<ans[i].v2){
cout<<char(65+ans[i].v1)<<"
"<<char(65+ans[i].v2)<<" "<<ans[i].weight<<endl;
w+=ans[i].weight;
}
else{
cout<<char(65+ans[i].v2)<<"
"<<char(65+ans[i].v1)<<" "<<ans[i].weight<<endl;

w+=ans[i].weight;
}
}

intmain() {

cout<<"Name: Atul kumar gupta SID:21103085\n";


cout<<"Enter the number of vertices\n";
intv,e;
edgetemp;
cin>>v;
cout<<"Enter the number of edges\n";

cin>>e;
cout<<"Enter the edges starting point ending point
and weight\n";
edge *edges = newedge[e];
for(inti=0;i<e;i++){
charx,y;
intz;
cin>>x>>y>>z;
temp.v1=x-'A';
temp.v2=y-'A';
temp.weight=z;
edges[i] =temp;
}
sort(edges,edges+e,comp);

kruskals(v,e,edges);

cout<<"weight of the MST:"<<" "<<w<<endl;


}
OUTPUT:

You might also like