Solution:- Two Sum with break outerLoop

Опубликовано: 22 Октябрь 2024
на канале: BBUniversal Team
18
2

#twosumequaltotarget,
#twosumproblem,
#twosumsolution,



Problem Solution:- Two Sum with break outerLoop

Let’s check the code.
Let’s take a look in detail

1. Add Indices variable with Type Int Array
var Indices: [Int] = [Int]() (This variable store return value)

2. Adding two forloop So, we can add two numbers
for (index, value1) in nums.enumerated(){
for (index2, value2) in nums.enumerated(){
// Let’s add value1 + value2 and compare with target
}
}
3. Adding value1 + value2 and Compare with target value
for (index, value1) in nums.enumerated(){
for (index2, value2) in nums.enumerated(){
if (value1 + value2) == target{
// Add one more condition with AND(&&) index != index2
Because we don’t want same index value to match the target.
}
}
}
5.for (index, value1) in nums.enumerated(){
for (index2, value2) in nums.enumerated(){
if (value1 + value2) == target && index != index2{
// Now add Indices and once you get match with sum of value1 + value2 with target
break the outerLoop
}
}
}
6. outerLoop: for (index, value1) in nums.enumerated(){
for (index2, value2) in nums.enumerated(){
if (value1 + value2) == target && index != index2{
Indices.append(contentsOf: [index, index2])
break outerLoop
}
}
}


outerLoop Label helps you to exit from the main for-loop as we added outerLoop label to the main for-loop.

Comment below if you have any queries.
Thank you. Stay Connected Stay Subscribe.

#twosumjavascript,
#twosumleetcodesolutionpython,
#twosumjava,
#twosumleetcodesolutionjava,
#twosumalgorithm,
#twosumanswer,
#twosumallpairs,
#twosumarray,
#twosumalgorithmpython,
#twosumarrayjava,
#twosumarrayleetcode,
#twosumarrayproblem,
#twosumarrayjavascript,
#algorithmtwosum,
#arraystwosum,
#thesumoftwonumbers,
#twosumbruteforcepython,
#twosumbinarysearch,
#twosumbestsolution,
#binarysearchtwosum,
#binarysearchtreetwosum,
#bestsolutionfortwosum,
#twosumcodingproblem,
#twosumcode,
#twosumcomplexity,
#twosumdynamicprogramming,
#twosumduplicates,
#twosumdatastructuredesign,
#twosumdigit,
#whatistwosumproblem,
#twosumexample,