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

af Afrikaans
ak Akan
sq Albanian
am Amharic
ar Arabic
hy Armenian
az Azerbaijani
eu Basque
be Belarusian
bem Bemba
bn Bengali
bh Bihari
bs Bosnian
br Breton
bg Bulgarian
km Cambodian
ca Catalan
ceb Cebuano
chr Cherokee
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
ee Ewe
fo Faroese
tl Filipino
fi Finnish
fr French
fy Frisian
gaa Ga
gl Galician
ka Georgian
de German
el Greek
gn Guarani
gu Gujarati
ht Haitian Creole
ha Hausa
haw Hawaiian
iw Hebrew
hi Hindi
hmn Hmong
is Icelandic
ig Igbo
id Indonesian
ia Interlingua
ga Irish
it Italian
ja Japanese
jw Javanese
kn Kannada
kk Kazakh
rw Kinyarwanda
rn Kirundi
kg Kongo
ko Korean
kri Krio (Sierra Leone)
ku Kurdish
ckb Kurdish (Soranรฎ)
ky Kyrgyz
lo Laothian
la Latin
lv Latvian
ln Lingala
lt Lithuanian
loz Lozi
lg Luganda
ach Luo
lb Luxembourgish
mk Macedonian
mg Malagasy
ms Malay
ml Malayalam
mt Maltese
mi Maori
mr Marathi
mfe Mauritian Creole
mo Moldavian
mn Mongolian
my Myanmar (Burmese)
sr-ME Montenegrin
ne Nepali
pcm Nigerian Pidgin
nso Northern Sotho
no Norwegian
nn Norwegian (Nynorsk)
oc Occitan
or Oriya
om Oromo
ps Pashto
fa Persian
pl Polish
pt-BR Portuguese (Brazil)
pt Portuguese (Portugal)
pa Punjabi
qu Quechua
ro Romanian
rm Romansh
nyn Runyakitara
ru Russian
sm Samoan
gd Scots Gaelic
sr Serbian
sh Serbo-Croatian
st Sesotho
tn Setswana
crs Seychellois Creole
sn Shona
sd Sindhi
si Sinhalese
sk Slovak
sl Slovenian
so Somali
es Spanish
es-419 Spanish (Latin American)
su Sundanese
sw Swahili
sv Swedish
tg Tajik
ta Tamil
tt Tatar
te Telugu
th Thai
ti Tigrinya
to Tonga
lua Tshiluba
tum Tumbuka
tr Turkish
tk Turkmen
tw Twi
ug Uighur
uk Ukrainian
ur Urdu
uz Uzbek
vi Vietnamese
cy Welsh
wo Wolof
xh Xhosa
yi Yiddish
yo Yoruba
zu Zulu

Original subtitles

All right.

So now let's solve this exercise together.

Let's say we have some array of size for, okay, this is not necessary and we will create, first of

all, the actual array that we are going to work with.

Let's create array of values.

OK, so values of size and we'll put here, I don't know, like minus one to three and I don't know,

minus six.

OK.

Awesome.

So, so far, so good, we created the array.

You can also read these values from the user doesn't really matter.

And we also assume that the array is going to be at least with two elements so that we can find out

and calculate the sum or at least one pair of these elements.

So what we will do, we will create two.

Two integers that will use as a spice things for the indexes, since we want to iterate over all the

elements and for any pair, let's take it like this.

OK, so for this value, we would like to find out its sound with all the values on.

It's right for these value.

We would like to find out the sum of these value with all the values on its right.

So we may use here some nested for a loop.

And how these for a loop is going to look like these for a loop is going to look like this.

So the outer loop is going to iterate and every time take one element.

Right?

Starting from the element at index equal to zero.

As long as I use less than Sice I + + + inside of the inner loop, what we are going to do is start

j from I +1 right, because we every time want to start from the element on its right.

OK.

A little bit improving it and doing that as long as J is less then size J + +.

OK, so we have two loops, one in sight of the other.

And what we need to do now.

OK.

We need to say, OK, let's say we have created some ain't mean, some OK or closest to zero with this

will be the minimum sum and we will associated with the value of zero.

So the question is whether this will be correct.

OK.

And the answer is no, I wouldn't go with this way because we will not be able to every time compare

mean sum of zero with some other sum that will be calculated during each iteration because means some

will always be the lowest.

So we will start means some and we will see that it will be equal to values at index zero plus value

set index one.

OK, so just for simplicity, we will say that we assume that this array will have at least two elements.

OK, we can also check this out.

We can add some condition to check out if there are more than just one elements in disarray.

Otherwise, just bring some error message.

But we will start it like this and we can also start I from one or not.

Yeah, we can start it from Jay, from I.

But let's leave it as is OK, because I think it can make more problems than it needs because we maximum

adding one operation.

That's it.

So minimum sum equals do the sum of these two and now on every iteration, we are going to see if there

is at least one pair of elements that there are, some will be lower than the minimum sum.

So we will create additional variable called its current some.

And on every iteration, what we will do is we will create current sum equal to what?

Two values at which index at index, I place values at Index J.

So far, so good.

So we are doing for a loop inside of a for a loop, and that's how we calculate fanged out the current

sum.

And now a simple question should be asked.

We need to ask whether right, we need to ask whether the this current sum is less or greater, meaning

it's absolute value is less than the minimum sum.

And the way we need to ask it is whether you can use the ABS function, the absolute function.

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.

So we can ask a simple question if what should we do now?

Yeah, basically, let's use the ABS function, shall we?

OK, we will use the ABS function.

I think there is no problem in it.

We can always include the associated libraries for that.

OK, so let's use abs.

OK.

We can also add here Where is it?

Let's use here.

OK, so a B.S., we included mathematical functions, so abs of what abs of let's do it.

If abs of current some.

If these abs is less, then the ABS of minimum some.

Minimum sum up, come on, come on, come on, come on, come on here.

Here it is.

If these abs is less than this one, then what should we do?

What should we do?

We should say that minimum sum is now being updated and it should be equal to the current sum.

So far, so good.

So that's how we do it.

If the absolute value of current is less than the absolute value of the minimum sum, then minimum sum

will be equal to currency.

And basically inside of these for a loop.

That's it.

That's how we find out.

Both of the options to calculate we take into account just the absolute value because we care how close

we are.

To the zero value, and we are interested not even knowing if we are only in this section.

We are also interested in this section, so the absolute value is the distance from zero up to here,

OK, to the values themselves.

And finally, what we should do.

We can basically print these value, OK?

We can use some printing operation.

We can print the value of the absence of the minimum sum.

We can also hear if we would like to add.

OK, I'm just kind of expanding these exercise so we can also create two additional variables called

these variables.

Let's say, let's see index one and Index two, and these two indexes are going to be associated.

Let's start it like that.

So these two index are going to be associated with the actual indexes of the pair that has the minimum

sum.

And we can update these indexes right here.

And there are these safe condition to make you B index one equal to AI and index two equal to J, and

then also to print the actual indexes and the actual values that are at these indexes, as well as the

sum itself.

And basically, they is how you can extend your working with this program.

So, yeah, I hope this is clear so far, guys.

This is very interesting.

VIDEO With some logic involved with working with absolute values in taking into account also both negative

and positive values.

So, yeah, once again, thank you so much for watching.

Keep on practicing, keep on moving forward.

And if you have any questions, feel free to ask me and gladly we will be able to answer all of your

questions.

Thank you so much and enjoy.

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