Beispiel #1
0
        public void Find_Indices_By_Array_ShouldReturn_NotNull()
        {
            var array            = new[] { 9, 4, 6, 3, 8, 5, 4 };
            var objective        = 9;
            var expectedResponse = new IndicesModel {
                Indice1 = 1, Indice2 = 5
            };

            _fixedArray.GetArray().Returns(array);

            var response = _twoSum.FindIndicesByArrayAndObjetive(_fixedArray, objective);

            Assert.AreEqual(expectedResponse.Indice1, response.Indice1);
            Assert.AreEqual(expectedResponse.Indice2, response.Indice2);
        }
Beispiel #2
0
        public IndicesModel FindIndicesByArrayAndObjetive(IFixedArray fixedArray, int objetive)
        {
            var array = fixedArray.GetArray();

            for (int i = 0; i < array.Length; i++)
            {
                var item1 = array[i];

                for (int j = 0; j < array.Length; j++)
                {
                    var item2 = array[j];

                    if (i != j && item1 + item2 == objetive)
                    {
                        return new IndicesModel {
                                   Indice1 = i, Indice2 = j
                        }
                    }
                    ;
                }
            }

            return(null);
        }
    }