1
00:00:00,110 --> 00:00:05,810
Let's now get into the code for the second approach that we have discussed for solving the sorted squared

2
00:00:05,810 --> 00:00:06,770
array question.

3
00:00:06,950 --> 00:00:07,400
Okay.

4
00:00:07,400 --> 00:00:10,520
So we are going to start by creating a new array.

5
00:00:10,550 --> 00:00:17,180
So let me say const new array is equal to new array.

6
00:00:18,110 --> 00:00:24,980
And this new array will have the same size as the input array which is what is given us over here.

7
00:00:25,010 --> 00:00:25,370
Okay.

8
00:00:25,370 --> 00:00:27,770
So over here and say array dot length.

9
00:00:28,690 --> 00:00:35,200
And what we're going to do is we're going to fill every cell in the new array with the value zero.

10
00:00:35,200 --> 00:00:38,500
So over here I'll say dot fill zero.

11
00:00:39,820 --> 00:00:40,420
Okay.

12
00:00:40,420 --> 00:00:49,390
Now we have seen that the highest value will either be the squared of the leftmost element or the rightmost

13
00:00:49,390 --> 00:00:51,460
element in the array, which is given to us.

14
00:00:51,460 --> 00:00:53,290
So let's have two pointers.

15
00:00:53,290 --> 00:00:56,260
So I'll say let pointer left.

16
00:00:58,680 --> 00:01:02,130
Is equal to zero, which is the left most index.

17
00:01:02,130 --> 00:01:05,010
And also I'll say let pointer right.

18
00:01:07,280 --> 00:01:12,230
Is equal to array dot length minus one.

19
00:01:13,280 --> 00:01:20,030
Okay, now I'm going to iterate over every index in the new array which we have created over here.

20
00:01:20,030 --> 00:01:24,050
But we will start at the last index and go up to zero.

21
00:01:24,290 --> 00:01:34,580
So over here I'll say for let I is equal to array dot length minus one and I greater than equal to zero.

22
00:01:34,580 --> 00:01:39,110
Because we will be going till I equal to zero and I minus minus.

23
00:01:39,110 --> 00:01:39,830
Okay.

24
00:01:39,830 --> 00:01:46,430
Now we will find the squared value of the leftmost element and the rightmost element.

25
00:01:46,430 --> 00:01:50,090
So I'll say const left squared.

26
00:01:52,430 --> 00:02:00,500
Is equal to math dot pow array at pointer left okay.

27
00:02:00,500 --> 00:02:02,360
And we are squaring it.

28
00:02:02,360 --> 00:02:03,800
So comma two over here.

29
00:02:03,800 --> 00:02:07,010
And similarly const right squared.

30
00:02:10,370 --> 00:02:14,810
Is equal to math dot pow array.

31
00:02:16,470 --> 00:02:18,030
At point or write.

32
00:02:19,650 --> 00:02:21,090
And again comma two okay.

33
00:02:21,090 --> 00:02:22,320
Because we are squaring it.

34
00:02:22,950 --> 00:02:25,470
Now we are going to compare these two values.

35
00:02:25,470 --> 00:02:32,100
So I will say if left squared greater than right squared.

36
00:02:32,790 --> 00:02:41,040
So if this is the case then we will say new array at index I is equal to left squared.

37
00:02:41,520 --> 00:02:47,850
And because we have used up the left squared or the element at which the left pointer is pointing at

38
00:02:47,850 --> 00:02:52,320
the current moment, we are going to change that pointer by incrementing it.

39
00:02:52,320 --> 00:02:57,540
So I will say pointer left plus plus okay.

40
00:02:57,540 --> 00:02:59,700
Now if this is not the case.

41
00:03:00,480 --> 00:03:08,370
What we are going to do is we'll say new array at index I is equal to write squared okay.

42
00:03:08,370 --> 00:03:13,980
And then because we have used the element at which the right pointer was pointing, we are going to

43
00:03:13,980 --> 00:03:16,890
shift the right pointer by decrementing it.

44
00:03:16,890 --> 00:03:20,820
So I will say pointer right minus minus okay.

45
00:03:20,820 --> 00:03:21,570
So that's it.

46
00:03:21,570 --> 00:03:23,580
So this should solve the question.

47
00:03:23,580 --> 00:03:26,880
Now all that remains is we'll have to return the new array.

48
00:03:26,880 --> 00:03:29,010
So here we'll say return new array.

49
00:03:29,010 --> 00:03:30,030
And that's it.

50
00:03:30,030 --> 00:03:34,260
Now let's go ahead and run this code and see if it's passing all the test cases.

51
00:03:40,130 --> 00:03:42,890
And you can see that it's passing all the test cases.

52
00:03:42,890 --> 00:03:45,170
And again do make use of the user logs.

53
00:03:45,170 --> 00:03:50,690
So for every test case you can see the input, the expected output and the actual output.

54
00:03:50,690 --> 00:03:54,650
When you take a look at the user logs for each individual test case.

55
00:03:54,650 --> 00:03:56,480
Also do make use of this.

56
00:03:56,480 --> 00:04:01,130
If you want to debug your code, you can just have console log statements and you can print various

57
00:04:01,130 --> 00:04:04,820
things which will help you debug your code in case you're stuck.
