1
00:00:00,940 --> 00:00:05,110
Hi everyone, I hope you have thought about how to solve this on your own.

2
00:00:05,110 --> 00:00:07,900
Now let's look at how to solve this together.

3
00:00:07,900 --> 00:00:10,750
The first method is the brute force method.

4
00:00:10,780 --> 00:00:13,420
All right so let's look at this method over here.

5
00:00:13,420 --> 00:00:15,790
For this let's take a sample input.

6
00:00:15,790 --> 00:00:19,390
Let's say the input is minus three one two and seven.

7
00:00:19,390 --> 00:00:21,400
So we have this array as input.

8
00:00:21,400 --> 00:00:23,050
Notice that this is sorted.

9
00:00:23,050 --> 00:00:23,470
All right.

10
00:00:23,470 --> 00:00:29,170
Now the brute force method is that we just square each element and put it in a new array.

11
00:00:29,170 --> 00:00:29,500
Right.

12
00:00:29,500 --> 00:00:32,770
So that uh after doing that what would be the array.

13
00:00:32,770 --> 00:00:37,030
The array would be 914 and 49.

14
00:00:37,030 --> 00:00:37,270
Right.

15
00:00:37,270 --> 00:00:40,390
We are just squaring each element and putting it in a new array.

16
00:00:40,390 --> 00:00:46,120
So minus three square is nine, one square is one, two square is four and seven square is 49.

17
00:00:46,120 --> 00:00:49,300
Now notice that this array over here is not sorted now.

18
00:00:49,300 --> 00:00:52,270
So the next step would be that we have to sort this array.

19
00:00:52,720 --> 00:00:59,020
And once we sort this array we will get the required output which is 149 and 49.

20
00:00:59,020 --> 00:01:03,580
Now this is very simple but it does not have the best space and time complexity.

21
00:01:03,580 --> 00:01:07,240
Now let's look at the space and time complexity of this solution.

22
00:01:07,630 --> 00:01:11,710
Now over here notice that we have traversed the array.

23
00:01:11,710 --> 00:01:14,980
And we have squared each element and put it in a new array.

24
00:01:14,980 --> 00:01:15,310
Right.

25
00:01:15,310 --> 00:01:17,260
So we have traversed it one time.

26
00:01:17,260 --> 00:01:19,240
Let's say this array has n elements.

27
00:01:19,240 --> 00:01:23,140
So the time complexity of this operation is O of n right.

28
00:01:23,140 --> 00:01:24,880
We are just traversing it one time.

29
00:01:24,880 --> 00:01:27,430
Now after this we have to sort right.

30
00:01:27,430 --> 00:01:31,300
We have to sort the given array to get from here to here right now.

31
00:01:31,300 --> 00:01:37,780
Any good sort is has a time complexity of o of n log n for example, quicksort, etc..

32
00:01:37,780 --> 00:01:38,200
Right.

33
00:01:38,200 --> 00:01:45,130
So the total time complexity of this algorithm therefore is equal to o of n log n.

34
00:01:45,130 --> 00:01:50,920
Now you could say that it is O of n log n plus n right o of n log n plus n.

35
00:01:50,920 --> 00:01:52,300
Yes, we can say that.

36
00:01:52,300 --> 00:01:54,760
But again this simplifies to n log n.

37
00:01:54,760 --> 00:01:55,750
Why is that so?

38
00:01:55,750 --> 00:01:59,230
Because n log n is greater than n right in.

39
00:01:59,230 --> 00:02:03,190
In the logarithm section we have seen that already n log n is greater than n.

40
00:02:03,190 --> 00:02:09,160
Now if n log n is greater than n, you can either just remove this and you're just left with n log n,

41
00:02:09,160 --> 00:02:14,080
or you can think of it in this way instead of n, let's replace it with n log n.

42
00:02:14,080 --> 00:02:17,050
So when we add these two, we get two n log n.

43
00:02:17,050 --> 00:02:18,730
And then this is a constant.

44
00:02:18,730 --> 00:02:20,290
So we can remove the constant.

45
00:02:20,290 --> 00:02:22,570
And again we are getting n log n.

46
00:02:22,570 --> 00:02:28,240
So the time complexity of the brute force method is n log n.

47
00:02:29,010 --> 00:02:32,940
Now what about the space complexity of this solution over here?

48
00:02:32,940 --> 00:02:36,000
Notice that we have made a new array with n elements.

49
00:02:36,000 --> 00:02:38,400
So we have consumed some space.

50
00:02:38,400 --> 00:02:42,870
Therefore the space complexity of this algorithm is O of n right.

51
00:02:42,990 --> 00:02:44,490
This will have n elements.

52
00:02:44,490 --> 00:02:48,780
If the input array has n elements, the new array also will have n elements.

53
00:02:48,780 --> 00:02:51,330
So the space complexity is O of n.

54
00:02:51,330 --> 00:02:51,750
All right.

55
00:02:51,750 --> 00:02:57,810
So we have seen that the time complexity is O of n log n and the space complexity is O of n.

56
00:02:57,810 --> 00:02:59,910
Now this is the brute force method.

57
00:02:59,910 --> 00:03:03,690
We will see in the next video how we can do better than this.
