Optomized solutions for Day1

This commit is contained in:
2020-12-03 12:10:28 -05:00
parent ddddf63071
commit 73c4fb9984
2 changed files with 21 additions and 7 deletions

View File

@@ -7,6 +7,7 @@
#include <iostream>
#include <vector>
#include <algorithm>
#include "Stopwatch.hpp"
@@ -32,10 +33,16 @@ int main(){
//Start the timer
timer.start();
std::sort(numbersList.begin(), numbersList.end());
//Find the two numbers in the array that make the correct sum
for(int cnt1 = 0;(cnt1 < numbersList.size()) && (!found);++cnt1){
for(int cnt2 = cnt1 + 1;(cnt2 < numbersList.size()) && (!found);++cnt2){
if((numbersList[cnt1] + numbersList[cnt2]) == NEEDED_SUM){
int sum = numbersList[cnt1] + numbersList[cnt2];
if(sum > NEEDED_SUM){
break;
}
else if(sum == NEEDED_SUM){
firstNum = numbersList[cnt1];
secondNum = numbersList[cnt2];
found = true;