problem with the below HackerRank question, giving WA in three TestCases.

this code is giving WA in testcases number 3,10 and 11.
link to the question is-:
https://www.hackerrank.com/contests/w38/challenges/minute-to-win-it/problem

static int minuteToWinIt(int[] a , int k) {

int cnt = 0;

for(int i = 1 ; i<a.length;i++)
{
if((a[i]-a[i-1])!=k )
{
a[i]=a[i-1]+k;
cnt++;
}

}
return cnt ;
}
error 404

describe the problem and your solution (¿did you understand the question correctly? ¿is your approach properly implemented?)
and if you can, the input/output that fails.


by the way, your code is not c++
by the way, your code is not c++

It looks like Java. The C++ signature is more like:
 
static int minuteToWinIt(vector<int> a , int k);


Paraphrasing the question:
You are given integers n and k and then an array of n integers. You need to output the minimum number of changes to the elements that will make a[i+1] - a[i] == k true for all i from 0 to n-2.

E.g.,
5 2
1 3 4 7 10

Changing 4 to 5 and 10 to 9 gives:
1 3 5 7 9
which are all k == 2 apart.
Since we had to change 2 elements, the output should be 2.
@tpb, @ne555
can you code the problem in cpp for full marks
i got only 16 marks out of 20.

the cpp signature is-:
// Complete the minuteToWinIt function below.
int minuteToWinIt(vector<int> a, int k) {
// Return the minimum amount of time in minutes.

}

link to the question is-:
https://www.hackerrank.com/contests/w38/challenges/minute-to-win-it/problem
Last edited on
already told you, that link gives me error 404
going by tpb's description, this is an example input where you fail
5 2
1 2 4 6 8
that's the new question, please help to code the below problem-:

------A Time-saving Affair------

Janet is in an Uber on her way to an interview. The driver promises to take her to the venue as soon as possible. The driver is aware
that:-
-There are 'n' junctions in the city of Mumbai, numbered from '1' to 'n'.
-Janet's interview location is at junction 'n'. They are initially at junction '1'.
-There are 'm' bidirectional roads connecting some pairs of junctions, each one requiring some amount of time to pass through it.

At every junction, there are traffic lights denoting whether they are allowed to go further or to wait. Traffic lights have two colors,
'red' and 'green'. The driver can commute through junctions based on these conditions:
1. At any junction, if the traffic signal's light is green, then they can go immediately, otherwise, they have to wait until traffic signal
becomes green.
2. Traffic signal changes its color every 'k' seconds of time at all junctions simultaneously.

Initially, at the '0th' second, all traffic lights have changed to green color at all the junctions. If the cab driver reaches a junction at a second when the traffic light changes its color, then he sees the traffic light after the change.
Can you help the driver determine the least amount of time needed to reach the interview location?
Complete the function leastTimeToInterview which takes in three integers n,k and m and returns the least amount of time
needed to reach the interview location, in seconds. You need to take the information about the roads from the standard input. They
will be specified in 'm' lines, as described in the input format section below.

Input Format:-

-The first line contains an integer 'n', the number of junctions.
-The second line contains an integer 'k' denoting the time taken by a signal to change its color.
-The third line contains an integer 'm' denoting the number of roads.
-The next 'm' lines describe the roads. Each consist of three space-separated integers 'i','j' and 't' where 'i' and 'j' denotes a road between two junctions and 't' denotes time required to travel through it.

Constraints-:
- 1<= n <= pow(10,4)
- 1<= k <= pow(10,2)
- 1<= m <= pow(10,5)
- 1<= i,j <= n
- 1<= t <= pow(10,3)

- There can be self-loops, i.e., roads connecting the same pair of junctions.
- There is at least one path from junction '1' to junction 'n'.

Output Format-:

Print a single integer denoting the shortest amount of time required to reach junction 'n'.

Sample Input 0
7
4
7
1 2 3
2 3 1
1 4 4
4 6 7
7 5 2
3 5 1
4 5 5

Sample Output 0
11


Explanation 0-

- Junction number 1: The cab driver can visit any of the adjacent junctions. He chooses to visit junction 2. Since the traffic signal is
'green' at the '0th' second, He can visit junction 2 which takes 3 seconds.
- Junction number 2: The traffic signal is still 'green' since traffic signals change color every '4' seconds. The cab driver now chooses
to visit junction '3' which takes '1' second, and we are now at the '4th' second.
- Junction number 3: 4 seconds have passed, and the traffic signal has already become 'red'. They have to wait for 4 more seconds
until the signal again becomes 'green'.
- Junction number 5: The cab takes the route to junction 5 which takes 1 second. So far, 9 seconds have passed.
- Junction number 7: The traffic signal is still 'green' and the cab can go to junction 7. It takes 2 seconds to reach junction 7.

In total, it takes 11 seconds to go from junction 1 to junction 7 . It can be shown that this is the minimum amount of time possible.


#include <bits/stdc++.h>
using namespace std;

// Complete the leastTimeToInterview function below.
long leastTimeToInterview(int n, int k, int m) {

// Return the least amount of time needed to reach the interview location in seconds.
}

int main()
{
ofstream fout(getenv("OUTPUT_PATH"));
int n;
cin >> n;

cin.ignore(numeric_limits<streamsize>::max(), '\n');
int k;
cin >> k;

cin.ignore(numeric_limits<streamsize>::max(), '\n');
int m;
cin >> m;

cin.ignore(numeric_limits<streamsize>::max(), '\n');
long result = leastTimeToInterview(n, k, m);
fout << result << "\n";
fout.close();
return 0;
}


@ne555
please provide a code of the above problem
its giving wrong on test case #3 and #10
can anyone help me out with boundary conditions that i left?
or give some test cases.
answer for 11th test case is
n-r-1
where r is the number of pairs that satisfy the condition
a[i]-a[i-1]=k
@kaskish
have you done the 3rd question which is based on graph
even the partial code if you have written, just tell me.
in 3rd use Dijkstra’s shortest path algorithm
#include<bits/stdc++.h>
using namespace std;
#define ll long long
#define pb push_back

ll g[10005][10005];
vector<bool> visited(10005,0);
vector<ll> path(10005),ans;
ll path_index=0;
ll m,k;
void clr()
{
for(ll i=0; i<=m; i++)
for(ll j=1; j<=m; j++)
g[i][j]=0;
}

void calc(ll u, ll d)
{
visited[u] = true;
path[path_index] = u;
path_index++;
if(u==d)
{ ll t=0;
for(ll i=1; i<path_index; i++)
{ if(t%(2*k)>=k && t%(2*k)<=2*k-1)
t+=2*k-(t%(2*k));
t+=g[path[i]][path[i-1]];
}
ans.pb(t);
}
else
{
for(ll i=1; i<=m+1; i++)
if(g[u][i]!=0 && visited[i]!=1)
calc(i,d);
}
path_index--;
visited[u]=0;
}
int main()
{
ios::sync_with_stdio(0);
cin.tie(0); cout.tie(0);
ll n,a,b,x,i;
cin>>n>>k>>m;
clr();
for(i=1; i<=m; i++)
{
cin>>a>>b>>x;
g[a][b]=x;
g[b][a]=x;
}
calc(1,n);
sort(ans.begin(),ans.end());
cout<<ans[0]<<endl;
return 0;
}

i m getting segmentation fault
Topic archived. No new replies allowed.