1
00:00:00,080 --> 00:00:04,340
Let's now get into the code for the approach that we have discussed in the previous video.

2
00:00:04,340 --> 00:00:07,100
So first we are going to create a new array.

3
00:00:07,100 --> 00:00:10,010
So let me write let new array.

4
00:00:10,580 --> 00:00:12,650
So let's just call it new array itself.

5
00:00:12,650 --> 00:00:18,950
And this new array will have the same size as the input array which is given to us over here okay.

6
00:00:18,950 --> 00:00:22,310
So let new array equal to new.

7
00:00:23,000 --> 00:00:27,620
Array and the length of this is going to be array dot length.

8
00:00:29,220 --> 00:00:32,040
Where array is the input array which is given to us over here.

9
00:00:32,070 --> 00:00:32,610
Okay.

10
00:00:32,610 --> 00:00:39,120
And what we are going to do is over here, we are going to fill the value zero at every index in this

11
00:00:39,120 --> 00:00:39,690
new array.

12
00:00:39,690 --> 00:00:44,670
So I can say dot fill zero okay.

13
00:00:44,670 --> 00:00:48,990
So at this point new array has the same size as the input array.

14
00:00:48,990 --> 00:00:52,560
But every cell in the new array has the value zero.

15
00:00:52,560 --> 00:00:57,870
Now what we are going to do is we are going to iterate through every index in the new array.

16
00:00:57,870 --> 00:01:03,900
And then let's go ahead and square the element in the input array and place it at that particular index.

17
00:01:03,900 --> 00:01:09,060
So I can say for let I is equal to zero.

18
00:01:10,170 --> 00:01:16,020
I less than array dot length I plus plus okay.

19
00:01:16,560 --> 00:01:23,730
And over here I'm going to say new array at index I is equal to array.

20
00:01:23,730 --> 00:01:31,410
At index I into array at index I okay so that's it.

21
00:01:31,410 --> 00:01:38,940
So we have filled the new array with values such that at every index which is squaring the value in

22
00:01:38,940 --> 00:01:41,910
the given input array and placing it in the new array.

23
00:01:41,910 --> 00:01:48,090
But we have seen that this will not work because if the input array has negative and positive numbers,

24
00:01:48,090 --> 00:01:54,630
like for example, if the input array is something like minus four and then you have one, two and three

25
00:01:54,630 --> 00:01:54,960
okay.

26
00:01:54,960 --> 00:01:58,860
So clearly minus four square is greater than one square.

27
00:01:58,860 --> 00:02:00,270
So this will not work.

28
00:02:00,270 --> 00:02:02,940
So what we have to do is after this okay.

29
00:02:02,940 --> 00:02:04,530
So let's have three over here.

30
00:02:04,530 --> 00:02:08,730
So after this we have to go ahead and sort the new array.

31
00:02:08,730 --> 00:02:09,630
So let's do that.

32
00:02:09,630 --> 00:02:12,960
So again say new array dot sort.

33
00:02:13,080 --> 00:02:16,950
And we are going to sort the array in ascending order.

34
00:02:16,950 --> 00:02:21,240
So I can say a comma b and the criteria is a minus b.

35
00:02:21,390 --> 00:02:24,900
So we are sorting the new array in ascending order.

36
00:02:24,900 --> 00:02:27,450
And finally we just need to return the new array.

37
00:02:27,450 --> 00:02:31,230
So I can say return new array okay.

38
00:02:31,230 --> 00:02:32,370
So this should work.

39
00:02:32,370 --> 00:02:36,150
Now let's go ahead and run this code and see if it's passing all the test cases.

40
00:02:41,380 --> 00:02:44,050
And you can see that it's passing all the test cases.

41
00:02:44,050 --> 00:02:46,690
Now you can make use of the user logs okay.

42
00:02:46,690 --> 00:02:50,680
So in case your stack somewhere do make use of it to debug your code.

43
00:02:50,680 --> 00:02:56,200
So you can just add console log statements and you can print what is happening at that particular stage

44
00:02:56,200 --> 00:02:56,800
of your code.

45
00:02:56,800 --> 00:02:58,990
And that will help you debug your code.

46
00:02:58,990 --> 00:03:05,080
But over here you can see what is the input for various test cases and what is the expected output and

47
00:03:05,080 --> 00:03:06,340
what is the actual output.

48
00:03:06,370 --> 00:03:07,810
Now you will get over here.

49
00:03:07,810 --> 00:03:12,850
When you go to user logs, you will get these statements even if your code is passing the test case

50
00:03:12,850 --> 00:03:14,560
or it's failing the test case.
