All language subtitles for 18. A Program to Find Two Elements whose Sum is Closest to Zero - Solution

af Afrikaans
sq Albanian
am Amharic
ar Arabic
hy Armenian
az Azerbaijani
eu Basque
be Belarusian
bn Bengali
bs Bosnian
bg Bulgarian
ca Catalan
ceb Cebuano
ny Chichewa
zh-CN Chinese (Simplified)
zh-TW Chinese (Traditional)
co Corsican
hr Croatian
cs Czech
da Danish
nl Dutch
en English
eo Esperanto
et Estonian
tl Filipino
fi Finnish
fr French
fy Frisian
gl Galician
ka Georgian
de German
el Greek
gu Gujarati
ht Haitian Creole
ha Hausa
haw Hawaiian
iw Hebrew
hi Hindi
hmn Hmong
hu Hungarian Download
is Icelandic
ig Igbo
id Indonesian
ga Irish
it Italian
ja Japanese
jw Javanese
kn Kannada
kk Kazakh
km Khmer
ko Korean
ku Kurdish (Kurmanji)
ky Kyrgyz
lo Lao
la Latin
lv Latvian
lt Lithuanian
lb Luxembourgish
mk Macedonian
mg Malagasy
ms Malay
ml Malayalam
mt Maltese
mi Maori
mr Marathi
mn Mongolian
my Myanmar (Burmese)
ne Nepali
no Norwegian
ps Pashto
fa Persian
pl Polish
pt Portuguese
pa Punjabi
ro Romanian
ru Russian
sm Samoan
gd Scots Gaelic
sr Serbian
st Sesotho
sn Shona
sd Sindhi
si Sinhala
sk Slovak
sl Slovenian
so Somali
es Spanish
su Sundanese
sw Swahili
sv Swedish
tg Tajik
ta Tamil
te Telugu
th Thai
tr Turkish
uk Ukrainian
ur Urdu
uz Uzbek
vi Vietnamese
cy Welsh
xh Xhosa
yi Yiddish
yo Yoruba
zu Zulu
or Odia (Oriya)
rw Kinyarwanda
tk Turkmen
tt Tatar
ug Uyghur
Would you like to inspect the original subtitles? These are the user uploaded subtitles that are being translated: 1 00:00:00,790 --> 00:00:01,510 All right. 2 00:00:01,600 --> 00:00:04,420 So now let's solve this exercise together. 3 00:00:05,140 --> 00:00:12,190 Let's say we have some array of size for, okay, this is not necessary and we will create, first of 4 00:00:12,190 --> 00:00:15,040 all, the actual array that we are going to work with. 5 00:00:15,760 --> 00:00:17,920 Let's create array of values. 6 00:00:18,250 --> 00:00:28,180 OK, so values of size and we'll put here, I don't know, like minus one to three and I don't know, 7 00:00:29,410 --> 00:00:31,000 minus six. 8 00:00:32,350 --> 00:00:32,680 OK. 9 00:00:34,370 --> 00:00:34,880 Awesome. 10 00:00:35,570 --> 00:00:38,270 So, so far, so good, we created the array. 11 00:00:38,300 --> 00:00:42,360 You can also read these values from the user doesn't really matter. 12 00:00:43,190 --> 00:00:49,040 And we also assume that the array is going to be at least with two elements so that we can find out 13 00:00:49,040 --> 00:00:54,380 and calculate the sum or at least one pair of these elements. 14 00:00:55,580 --> 00:00:58,880 So what we will do, we will create two. 15 00:01:00,710 --> 00:01:08,030 Two integers that will use as a spice things for the indexes, since we want to iterate over all the 16 00:01:08,030 --> 00:01:11,540 elements and for any pair, let's take it like this. 17 00:01:12,080 --> 00:01:17,300 OK, so for this value, we would like to find out its sound with all the values on. 18 00:01:17,300 --> 00:01:19,280 It's right for these value. 19 00:01:19,290 --> 00:01:24,560 We would like to find out the sum of these value with all the values on its right. 20 00:01:25,070 --> 00:01:29,330 So we may use here some nested for a loop. 21 00:01:30,840 --> 00:01:35,130 And how these for a loop is going to look like these for a loop is going to look like this. 22 00:01:35,310 --> 00:01:39,330 So the outer loop is going to iterate and every time take one element. 23 00:01:39,720 --> 00:01:40,080 Right? 24 00:01:40,110 --> 00:01:43,560 Starting from the element at index equal to zero. 25 00:01:43,800 --> 00:01:53,160 As long as I use less than Sice I + + + inside of the inner loop, what we are going to do is start 26 00:01:53,160 --> 00:02:00,120 j from I +1 right, because we every time want to start from the element on its right. 27 00:02:00,420 --> 00:02:00,780 OK. 28 00:02:00,810 --> 00:02:07,140 A little bit improving it and doing that as long as J is less then size J + +. 29 00:02:08,820 --> 00:02:12,690 OK, so we have two loops, one in sight of the other. 30 00:02:13,710 --> 00:02:15,270 And what we need to do now. 31 00:02:15,540 --> 00:02:15,850 OK. 32 00:02:15,900 --> 00:02:24,990 We need to say, OK, let's say we have created some ain't mean, some OK or closest to zero with this 33 00:02:24,990 --> 00:02:30,580 will be the minimum sum and we will associated with the value of zero. 34 00:02:30,660 --> 00:02:35,130 So the question is whether this will be correct. 35 00:02:35,850 --> 00:02:36,270 OK. 36 00:02:36,720 --> 00:02:43,800 And the answer is no, I wouldn't go with this way because we will not be able to every time compare 37 00:02:43,830 --> 00:02:51,150 mean sum of zero with some other sum that will be calculated during each iteration because means some 38 00:02:51,150 --> 00:02:52,410 will always be the lowest. 39 00:02:53,040 --> 00:03:02,400 So we will start means some and we will see that it will be equal to values at index zero plus value 40 00:03:02,400 --> 00:03:03,300 set index one. 41 00:03:04,490 --> 00:03:10,670 OK, so just for simplicity, we will say that we assume that this array will have at least two elements. 42 00:03:10,880 --> 00:03:12,500 OK, we can also check this out. 43 00:03:12,500 --> 00:03:19,350 We can add some condition to check out if there are more than just one elements in disarray. 44 00:03:19,370 --> 00:03:21,770 Otherwise, just bring some error message. 45 00:03:22,040 --> 00:03:27,500 But we will start it like this and we can also start I from one or not. 46 00:03:28,040 --> 00:03:31,700 Yeah, we can start it from Jay, from I. 47 00:03:31,710 --> 00:03:39,290 But let's leave it as is OK, because I think it can make more problems than it needs because we maximum 48 00:03:39,290 --> 00:03:40,740 adding one operation. 49 00:03:40,760 --> 00:03:41,180 That's it. 50 00:03:42,640 --> 00:03:49,120 So minimum sum equals do the sum of these two and now on every iteration, we are going to see if there 51 00:03:49,120 --> 00:03:56,170 is at least one pair of elements that there are, some will be lower than the minimum sum. 52 00:03:57,930 --> 00:04:01,980 So we will create additional variable called its current some. 53 00:04:03,430 --> 00:04:10,270 And on every iteration, what we will do is we will create current sum equal to what? 54 00:04:11,180 --> 00:04:19,000 Two values at which index at index, I place values at Index J. 55 00:04:21,830 --> 00:04:22,880 So far, so good. 56 00:04:23,720 --> 00:04:29,870 So we are doing for a loop inside of a for a loop, and that's how we calculate fanged out the current 57 00:04:29,870 --> 00:04:30,290 sum. 58 00:04:31,190 --> 00:04:33,230 And now a simple question should be asked. 59 00:04:34,160 --> 00:04:46,160 We need to ask whether right, we need to ask whether the this current sum is less or greater, meaning 60 00:04:46,520 --> 00:04:49,520 it's absolute value is less than the minimum sum. 61 00:04:50,390 --> 00:04:55,850 And the way we need to ask it is whether you can use the ABS function, the absolute function. 62 00:04:55,850 --> 00:05:00,950 But I'm going to do it even assuming that we don't have these function and we don't know how to do it. 63 00:05:01,400 --> 00:05:06,160 So we can ask a simple question if what should we do now? 64 00:05:06,950 --> 00:05:09,710 Yeah, basically, let's use the ABS function, shall we? 65 00:05:10,400 --> 00:05:12,050 OK, we will use the ABS function. 66 00:05:12,260 --> 00:05:13,880 I think there is no problem in it. 67 00:05:13,880 --> 00:05:18,080 We can always include the associated libraries for that. 68 00:05:18,320 --> 00:05:20,900 OK, so let's use abs. 69 00:05:21,020 --> 00:05:21,330 OK. 70 00:05:21,350 --> 00:05:23,690 We can also add here Where is it? 71 00:05:23,960 --> 00:05:24,950 Let's use here. 72 00:05:33,340 --> 00:05:41,950 OK, so a B.S., we included mathematical functions, so abs of what abs of let's do it. 73 00:05:42,100 --> 00:05:44,680 If abs of current some. 74 00:05:46,430 --> 00:05:52,580 If these abs is less, then the ABS of minimum some. 75 00:05:54,310 --> 00:05:57,880 Minimum sum up, come on, come on, come on, come on, come on here. 76 00:05:58,250 --> 00:05:59,230 Here it is. 77 00:05:59,980 --> 00:06:03,640 If these abs is less than this one, then what should we do? 78 00:06:04,550 --> 00:06:05,250 What should we do? 79 00:06:05,270 --> 00:06:12,590 We should say that minimum sum is now being updated and it should be equal to the current sum. 80 00:06:14,670 --> 00:06:15,630 So far, so good. 81 00:06:17,250 --> 00:06:18,270 So that's how we do it. 82 00:06:18,540 --> 00:06:24,390 If the absolute value of current is less than the absolute value of the minimum sum, then minimum sum 83 00:06:24,390 --> 00:06:26,180 will be equal to currency. 84 00:06:27,420 --> 00:06:30,150 And basically inside of these for a loop. 85 00:06:30,540 --> 00:06:31,140 That's it. 86 00:06:31,920 --> 00:06:33,660 That's how we find out. 87 00:06:34,230 --> 00:06:42,720 Both of the options to calculate we take into account just the absolute value because we care how close 88 00:06:42,720 --> 00:06:43,560 we are. 89 00:06:45,330 --> 00:06:50,730 To the zero value, and we are interested not even knowing if we are only in this section. 90 00:06:51,180 --> 00:06:58,260 We are also interested in this section, so the absolute value is the distance from zero up to here, 91 00:06:59,100 --> 00:07:00,900 OK, to the values themselves. 92 00:07:01,290 --> 00:07:02,610 And finally, what we should do. 93 00:07:02,760 --> 00:07:06,330 We can basically print these value, OK? 94 00:07:06,360 --> 00:07:08,490 We can use some printing operation. 95 00:07:08,490 --> 00:07:12,750 We can print the value of the absence of the minimum sum. 96 00:07:13,110 --> 00:07:16,110 We can also hear if we would like to add. 97 00:07:16,320 --> 00:07:23,970 OK, I'm just kind of expanding these exercise so we can also create two additional variables called 98 00:07:23,970 --> 00:07:24,850 these variables. 99 00:07:24,870 --> 00:07:36,240 Let's say, let's see index one and Index two, and these two indexes are going to be associated. 100 00:07:37,020 --> 00:07:38,580 Let's start it like that. 101 00:07:39,510 --> 00:07:47,340 So these two index are going to be associated with the actual indexes of the pair that has the minimum 102 00:07:47,340 --> 00:07:47,670 sum. 103 00:07:47,940 --> 00:07:50,280 And we can update these indexes right here. 104 00:07:50,280 --> 00:07:55,890 And there are these safe condition to make you B index one equal to AI and index two equal to J, and 105 00:07:55,890 --> 00:08:02,610 then also to print the actual indexes and the actual values that are at these indexes, as well as the 106 00:08:02,610 --> 00:08:03,330 sum itself. 107 00:08:03,720 --> 00:08:11,100 And basically, they is how you can extend your working with this program. 108 00:08:12,600 --> 00:08:15,280 So, yeah, I hope this is clear so far, guys. 109 00:08:16,380 --> 00:08:17,910 This is very interesting. 110 00:08:17,910 --> 00:08:25,350 VIDEO With some logic involved with working with absolute values in taking into account also both negative 111 00:08:25,350 --> 00:08:26,550 and positive values. 112 00:08:26,820 --> 00:08:30,300 So, yeah, once again, thank you so much for watching. 113 00:08:30,330 --> 00:08:32,720 Keep on practicing, keep on moving forward. 114 00:08:32,730 --> 00:08:41,820 And if you have any questions, feel free to ask me and gladly we will be able to answer all of your 115 00:08:41,820 --> 00:08:42,510 questions. 116 00:08:43,050 --> 00:08:45,060 Thank you so much and enjoy. 10690

Can't find what you're looking for?
Get subtitles in any language from opensubtitles.com, and translate them here.