Richa Tibrewal
Questions (1)
Comments (2)
Reputation 20
 1of 1 vote
AnswerYou are given an array A[] with n elements. You are given S[], E[] and H[] array with each M elements.
 Richa Tibrewal in India
Apply an operation such that all the elements from A[ S[i] ] and A[ E[i] ] will be less than H[i].
Example :
given array A[] = [2,4,8,6,7]
S[0] = 1
E[0] = 4
H[0] = 5
So, after doing an operation, the resulting array is A[] = [ 2,4,5,5,5]
Thus, u need to do the same thing for all i.
After doing this, find out the maximum element in the array. Report Duplicate  Flag
Directi SDE1 Algorithm
Page:
1
Comment hidden because of low score. Click to expand.
Comment hidden because of low score. Click to expand.
0
of 0 vote
#include<cstdio>
#include<vector>
#include<climits>
#define min(a,b) (a<b?a:b)
#define MAXLEN 100
using namespace std;
int find_min(int dist[],bool visited[],int n)
{
int i,mx,m;
mx = INT_MAX;
for(i=0;i<n;i++)
{
if(dist[i]<mx && !visited[i])
{
mx = dist[i];
m = i;
}
}
return m;
}
int find_res(int start,int target,int graph[][MAXLEN],int n)
{
int u,v,w,dist[MAXLEN],cnt,i,j;
bool visited[MAXLEN];
for(i=0;i<n;i++)
{
dist[i] = INT_MAX;
visited[i] = false;
}
dist[start] = 0;
for(cnt = 1;cnt<n;cnt++)
{
u = find_min(dist,visited,n);
visited[u] = true;
for(v=0;v<n;v++)
{
if(graph[u][v]!=1 && !visited[v] && dist[u]+graph[u][v]<dist[v])
{
dist[v] = dist[u] + graph[u][v];
}
}
}
return dist[target];
}
int minCost(int node,vector<int> from,vector<int> to,vector<int> wt,int start,int target,int weight)
{
int res,graph[MAXLEN][MAXLEN],i,j;
start;
target;
for(i=0;i<node;i++)
{
for(j=0;j<node;j++)
graph[i][j] = 1;
}
for(i=0;i<from.size();i++)
{
from[i];
to[i];
graph[from[i]][to[i]] = graph[to[i]][from[i]] = wt[i];
}
res = find_res(start,target,graph,node);
// printf("res = %d\n",res);
for(i=0;i<node;i++)
{
for(j=0;j<node;j++)
{
if(graph[i][j]==1)
{
graph[i][j] = weight;
res = min(find_res(start,target,graph,node),res);
graph[i][j] = 1;
}
}
}
return res;
}
int main()
{
int node,edge,a,b,w,i,st,target;
vector<int> to,from,wt;
scanf("%d%d",&node,&edge);
for(i=0;i<edge;i++)
{
scanf("%d%d%d",&a,&b,&w);
to.push_back(a);
from.push_back(b);
wt.push_back(w);
}
scanf("%d%d%d",&st,&target,&w);
printf("%d\n",minCost(node,from,to,wt,st,target,w));
return 0;
}

Richa Tibrewal
October 24, 2016 Page:
1
CareerCup is the world's biggest and best source for software engineering interview preparation. See all our resources.
Open Chat in New Window
Open Chat in New Window
Its simple!!
 Richa Tibrewal October 24, 2016Let the starting node be start.
1. Go to the next right node of start. Let the node be right_node
2. Go to the extreme left node of right_node. Let this node be extreme_left
Thus this extreme_left node is the inorder successor of start.