I was messing around with possible "twoSum" solutions because I'm pretty bored... Anyways I'm pretty new to "Big O Time Complexity", and I was trying to figure out the time complexity of my program.
Now I don't assume my solution is at all polished or better, I was just wanting some help finding out the time complexity of it. I am very new to BIG(O) so don't dog on my skills... you can roast my code if you wish.
public static void main(String[] args) {
int[] array = {5, 3, 4, 1, 5, 2, 1, 6, 3, 2};
System.out.println(Arrays.toString(twoSum(array, 6)));
}
public static int[] twoSum(int[] nums, int target) {
int oldValue = 0;
int newValue;
int currentValue;
for (int i = 0; i < nums.length; i++) {
currentValue = i;
if (currentValue != 0) { // so oldValue never causes an exception
oldValue = i - 1;
}
newValue = i + 1;
if ((nums[currentValue] + nums[oldValue]) == target) {
return new int[]{currentValue, oldValue};
}
if ((nums[currentValue] + nums[newValue]) == target) {
return new int[]{currentValue, newValue};
}
if ((nums[newValue] + nums[oldValue]) == target) {
return new int[]{newValue, oldValue};
}
}
return new int[]{0, 0}; // we print out 0 0 if we can't find a solution
}
This really won't have an ANSWER, as I don't know how time complexity works fully (as of right now) all I am asking for is someone to help me with an explanation