- Coding Videos/
- JavaScript /
- Competitive Programming in Javascript – Solving your first problem.

# Competitive Programming in Javascript – Solving your first problem.

### Download Video link >

Competitive Programming in Javascript – Solving your first problem.

Hey guys

This is the first video about Competitive programming in JavaScript where we’ll be solving a basic problem in Competitive programming and making our first submission.

Following this we’ll start solving more complex problems.

Do subscribe of you liked the video!

source

### View Comments source >

### Transcript view all >

00:00 [Music]

00:07 [Music]

00:12 hey guys imagine for the first video

00:14 about how you can start solving your

00:16 first competitive programming problem in

00:18 JavaScript targeting a very basic

00:20 problem today and you get to it but

00:22 before that if you don't know what

00:24 algorithmic questions are there

00:26 basically questions that we want a

00:28 machine to solve for us and to take a

00:30 simple example for this let's say your

00:33 flip card and in flip read you have to

00:34 make a delivery from place a to place B

00:36 and there are a lot of steps that that

00:38 we will go through for example let's say

00:41 the video originates from Bangalore and

00:43 it has to go from Bangalore Hyderabad

00:44 and then it has to fly from Hyderabad to

00:46 Delhi and then it has to find you go

00:48 from Delhi to Chattanooga and thank to

00:49 your house so these are various steps

00:52 that it needs to follow and it has to

00:54 find the shortest path of moving from

00:55 one place to the other in all these

00:57 steps so this can be stored by a person

00:59 or the come to you are sitting on a

01:00 computer using some calculations but it

01:02 is in general faster if you write

01:04 algorithm for it and then I am going to

01:05 solve this problem and in feed as many

01:08 such questions to that particular once

01:10 we have written that down so that's the

01:13 idea behind writing an algorithm to

01:14 automate it a study you go to and to do

01:17 the task ready to be faster and I do it

01:20 for a lot of cellular problems so the

01:22 problem that we are solving today is

01:23 that we have to move from one city to

01:25 the other and then from the second to

01:27 the third city and finally end up at the

01:30 LA City you have a lot of such steps

01:32 that you have to go through and you have

01:34 to tell the shortest or the minimum

01:36 number of steps that you need to do in

01:39 order to reach the final destination so

01:41 to visualize this problem simply let's

01:44 say you have a partition plane over here

01:45 and you have to move from this initial

01:48 point to the final point and

01:50 initial point is tenure and this final

01:52 point is mangling you have to go from

01:53 here to here in the shortest number of

01:56 steps and what is the step one step is

01:58 that you move either upward or downward

02:01 left or right or at that medium a tiny

02:04 town a tiny left a tiny net so you can

02:06 move in one of eight directions and that

02:09 would count as one step and you have to

02:10 go from here to here in the minimum

02:13 number of steps okay so if you see over

02:15 here this is synchronicity to the ideal

02:20 way of doing this you know if this is N

02:21 and this is M the shortest distance

02:24 between these two is root of H square

02:25 bassam's but that's how you solve a

02:27 problem using the triangle inequality

02:28 but in this case you cannot move from

02:31 here to here technically the directly in

02:33 the dying manner because your diagonals

02:36 are discrete your diamonds have an angle

02:38 of 45 degree associate liquid you cannot

02:41 move in any direction because if you can

02:43 then you obviously choose that path but

02:44 in this case there are the there is the

02:47 side you can go to if you want to move

02:49 tightly you can move in the at the 45

02:51 degree angle and not at whatever angle

02:53 you want to keep so in this case how do

02:55 you solve this problem

02:56 the first thing that you should keep in

02:58 mind is that the minimum number of steps

03:01 let's say this is m and this is M then

03:03 the minimum number of steps that you

03:04 need to reach from here to here is M

03:06 plus that make sense because M is

03:09 greater than n that we can see or let's

03:11 say even if n was later than M then the

03:13 answer would have been n so basically

03:14 the maximum of these two is the minimum

03:16 number of steps that you need to take

03:18 why because and each time you can

03:20 decrease the distance from here to here

03:23 from here the horizontal distance and

03:25 the vertical distance at max by one you

03:27 cannot decrease it by more that much

03:29 open this what this tacit direction over

03:32 here I can only decrease the distance

03:34 the vertical distance by one and

03:36 horizontal distance by one in one step I

03:38 cannot decrease it by more than one the

03:40 horizontal or the vertical distance

03:41 hence the minimum number of steps two

03:45 each from here to here is M now can we

03:48 prove somehow that that is also the

03:50 maximum number of steps that you need

03:52 and then can we prove that M is the

03:54 total number of or the maximum of

03:56 distance and this distance is the

03:58 computer model steps that you need to

03:59 reach from here to here the answer is

04:01 yes that would be the maximum number of

04:04 steps that you need why because let's

04:06 say you start from here now you know

04:08 that this particular point the

04:10 finalization lies to my top right so

04:14 it's in the high tension in the and is

04:16 above me

04:17 so I'm if I start moving in this

04:19 diagonal direction till the time I reach

04:21 the body five degree we move I forgot

04:25 unit in the time we reach the point

04:26 where this n distance becomes zero so

04:28 you have conquered the horizontal

04:30 distance that you have to enter is only

04:31 this vertical distance that's left that

04:33 you need to conquer so let's say you

04:35 keep moving in this direction and the

04:38 distance that you covered BC cleans and

04:40 in this section and n in this section

04:41 and the distance that is left is M minus

04:44 n right and this M minus n you can cover

04:47 by going in the other direction so the

04:49 next time or the next number of steps

04:50 that you need is n plus M minus n which

04:53 is basically and so if basically if you

04:57 have to solve this problem and you know

04:58 that this is this particular point is at

05:00 x1 y1 and spread it was point is and x2

05:02 y2 find out what you listen to it in the

05:04 book which is

05:04 but the absolute value of y1 minus y2

05:07 find the horizontal distance between

05:08 these two this is nothing but the

05:10 absolute value of x1 minus x2 and then

05:13 in fact the maximum of these two

05:14 whatever this is maximal these two is

05:16 the time what is the number of steps

05:17 that you need to reach from here to here

05:19 I hope that clears of the problem and

05:21 this can be extended to my total cities

05:24 as well you have given your city one

05:25 city to an eternal city three then you

05:27 know you have to the number of steps

05:29 that you need to reach from here to here

05:30 is the absolute the maximum of the

05:32 horizontal the vertical is in between a

05:34 spoon and this nozzle can be applied

05:35 with these two points as well and you

05:37 can keep doing it for multiple points

05:38 and reach your final answer so I hope

05:41 you got a little taste of how you can

05:43 solve the problem now you will be

05:45 actually coding this problem in

05:46 JavaScript all the platform quite

05:48 interview wind so you don't have to set

05:50 up and I think on the computer and once

05:52 we go disparagement solve it you will be

05:54 able to make your first submission in

05:57 the competitive program and from there

05:59 you can think on and tackle problems and

06:02 we were also talking about attacking

06:03 both of those in the future so let's get

06:05 to the good part

06:08 hey guys so let's get down to some

06:10 coding now this is the platform by the

06:12 name of interpreter conve you can propel

06:14 which basically we do prepare for campus

06:17 interviews for internships and

06:18 placements but you can use it for I will

06:20 make questions practicing as well so

06:23 this has a bunch of sections will be

06:24 focusing on the programming section and

06:26 the question that we just solved is the

06:28 first question in the section so open

06:31 this in your computer it's called main

06:35 steps in infinite grid so if you go

06:38 through the question it's basically

06:40 similar to what we discussed you are

06:41 given a sequence of points and the order

06:43 in which you need to cover these points

06:44 given give the minimum number of cells

06:47 in which you can achieve it and you can

06:48 start from the first point you have to

06:50 start with the first point move to the

06:51 second of the second move to the third

06:53 and you have to do that in the minimum

06:54 number of steps considering it move in

06:56 da directions so just come down here

06:59 it's like the languages JavaScript

07:01 because we are doing all the computer

07:03 programming in JavaScript in this course

07:04 and if you don't understand what all of

07:07 this stuff is it's basically module dot

07:10 exports as you can see is an object it

07:13 has one attribute called couple points

07:15 and this attribute is a function so if

07:18 you don't understand this structure

07:19 right now just ignore it

07:21 what we have to do is that inside this

07:23 function we have two parameters a and B

07:25 which are the add of integers which

07:27 represent the x coordinates and the Y

07:29 coordinates of the points of the earth

07:31 index value and this in these areas is

07:35 an integer and that negative resents the

07:38 x-coordinate and the y-coordinate of the

07:39 island city other point which we have to

07:42 go to and we need to start from the

07:44 first point is to see the zeros point

07:46 and move to the n minus one point and

07:48 find the minimum distr minimum number of

07:51 steps that we need to take to cover

07:52 those and whatever that you became we

07:55 need to return from this function I am

07:57 little bit I will basically check what

07:59 this function

07:59 for a lot of cases and check if your

08:02 answers are expected so you have a a and

08:05 you have vanity if you remember from

08:09 what we discussed for each and every

08:12 point we need to go diagonally to become

08:16 compressed one of the directions and

08:19 then move vertically or horizontally to

08:20 finally cover the rest of it and what we

08:23 got down to in the egg was that if

08:27 there's a point starting with x1 comma

08:29 y1 and the second point is x2 comma Y

08:31 doing what we need to do is we need to

08:34 find the maximum of the absolute value

08:36 x1 minus x2

08:41 c'mon the absolute value of y1 minus y2

08:44 and this will give us the answer right

08:47 now we don't have just two cities like

08:49 we did in the example that we discussed

08:50 we have multiple cities so we will need

08:52 to hydrate using the forum so far away I

08:55 equal to 0 I less than n minus 1 i plus

09:00 plus why did we write n minus 1 over

09:03 here because we need to compare the

09:04 first point with the second one the

09:06 second with the third one and the second

09:09 asks with the last one we don't need to

09:11 compare the last one with anything else

09:12 so we will just hydrate order from the 0

09:15 to the N minus 2 index we leave the last

09:17 one because you don't have to

09:18 with anything now what is next one for

09:21 this loop x1 is nothing but a of I X 2

09:27 is nothing but yeah I responded Y 1 is

09:33 nothing but V of I and Y 2 is your I

09:37 plus 1 so in this particular item of

09:41 mine I'm checking the distance between

09:42 the iron and I plus 1 it's city so these

09:46 are the X's and Y's in that case we need

09:49 to return the net number of steps that

09:51 it took for all of the furniture for

09:53 hydrating through all of the cities so

09:55 we will define an answer variable whose

09:57 initial value we have taken as you so

10:00 initially if it has the value 0 over

10:03 here we modify its value to something

10:04 and finally we will return that value in

10:06 this function and what will we add to

10:10 this answer over here so in each ication

10:11 what is the distance that we have to

10:13 cover it's nothing but max of absolute

10:20 value of x 1 minus x 2 comma absolute

10:26 value of y all - right this is it your

10:31 question is done hopefully I think this

10:33 would pass so what have we done we here

10:35 again what we're doing is then I taking

10:38 from the first point to the second last

10:43 point we're comparing this point with

10:45 the next point in finding what is the

10:46 answer for this what is the number of

10:48 steps that it could take me and I'm

10:49 adding that to the answer so here is the

10:51 first one answer is equal to answer Plus

10:54 this thing so answer was in DC 0 after

10:57 the first equation we had this value to

11:00 the answer after the second iteration we

11:01 do that and we do that

11:03 so that answer that it will finally

11:05 contains the net answer the answer which

11:09 contains the summation of all the cities

11:11 or the steps taken in all the cities to

11:13 each from the first to the last point if

11:15 we try to touch this and this will

11:18 basically let us know if there are any

11:19 errors there are it says n is not

11:21 defined because and it's not a variable

11:24 defined in English so I tell you can

11:26 defined and as a dot length you can

11:30 simply write a dot length over here and

11:34 it says that this is the it gives the

11:35 correct answer for one or two cases so

11:37 when you test it just test its four

11:38 disks it for its composition and for few

11:41 basic cases when you click on submit

11:44 then it actually checks whether you're

11:46 whether or not your solution passed and

11:48 if you see in this case the solution

11:50 passed so as you can see this is just

11:52 five lines of code that you have to

11:54 write in the end and JavaScript made it

11:57 so simple I think actually this would be

11:59 very simple in any language but

12:02 functions like Max and function like

12:04 absolute which sharply defined in Zurich

12:06 helped us basically in achieving our fan

12:09 your and I hope you understand how this

12:13 particular algorithm is working if you

12:15 did not understand I go over it one more

12:17 time what we have to do is we have to

12:19 check that this steps taken from the

12:20 zeros to the first city then the let's

12:22 say there is three cities they never

12:24 good from zero to the first and the

12:25 first to the same yet there are just

12:27 three cities the value of n would be

12:28 three and a of zero and we have zero

12:33 putting the X and the y index of the

12:35 zero city which is the first city so x1

12:38 would be the x coordinate of the slow

12:39 city why I am going to be the y

12:41 coordinate of the zero city

12:42 x2 would be the x coordinate of the

12:44 first city and why it would be the y

12:48 coordinate of the first city and as we

12:50 saw this is the answer for a particular

12:52 X Y and well if there is one x one y one

12:55 away and x2 y2 over there then the

12:59 number of steps check needed is this

13:00 particular thing which

13:01 individual form which is doing this

13:04 again and again by looping through a

13:06 four loop what are the four units

13:07 looping through and because there are a

13:09 lot of CDs have you have to go to and so

13:11 that we don't have to write this code

13:12 again again we use a forward to do this

13:14 this is a little difficulty question

13:17 than what we discussed what we discussed

13:19 was for just blue cities but if they

13:21 have multiple cities then you have to

13:22 like to follow you and go through all of

13:23 these and if you submit this your books

13:25 are strongly suggest you to try this

13:27 question on your own if you have any

13:29 doubts just post them in the comments if

13:31 this is the first thing you're trying to

13:32 submit your question I know this can be

13:34 a little difficult to understand so if

13:36 you have any doubts lightning on and try

13:38 to solve this problem on into a bit

13:40 because we will be moving to little more

13:42 difficult questions in the future if you

13:43 don't understand this one you might get

13:45 confused in those in the ones that come

13:48 in the future so thank you for joining

13:50 me in this video and I hope to see you

13:53 in the next one

**Tags:**competitive, first, javascript, problem, programming, solving

## Leave a Reply