1

I'm told to implement two pointers which will run through the first and last index of my array and try to find a pair that returns 20. If the pair's sum is larger than 20 then lower the last index by 1 and if the pair's sum is smaller than 20 then add the first index by 1. If the pair returns 20 find the smallest number's index.

This is my checkSum method:

public static int checkSum(int[] array){
        // This function will inspect the input to find any pair of values that add up to 20
        // if it finds such a pair, it will return the *index* of the smallest value
        // if it does not find such as pair, it will return -1;
        
        int twenty = 20;
        int zero = 0;
        int checkIndex = array.length;
        

        for (int i = 0; i < array.length - 1; i++){
            for (int k = array.length - 1; k < array.length; k++){
                if (array[i] + array[k] == twenty){
                    // 0 + 1
                    System.out.println("this print out 20");
                    System.out.println(array [k] + " + " + array[i]);
                    if (array [k] >= array [zero]){
                        //need to print out the index of the minimum value
                        checkIndex = zero;
                    }
                }
                else if (array[i] + array[k] > twenty){
                    array[k]--;
                    checkIndex = array[k];
                }
                else if (array[i] + array[k] < twenty){
                 //tried a different method to see if it would increment the index rather than the value
                    array[i+1] = array[i];
                    checkIndex = array[i];
                }
                
                }
        }// remove the following line after you are done writing the function
        System.out.println(checkIndex);
        return checkIndex;

    }

and this is the main method that is provided:

public static void main(String[] args) {
        int[] array1 = new int[]{5, 7, 8, 9, 10, 15, 16};
        if (checkSum(array1) != 0){
            System.err.println("TEST1 FAILED");
        }
        int[] array2 = new int[]{3, 5, 8, 9, 10, 15, 16};
        if (checkSum(array2) != 1){
            System.err.println("TEST2 FAILED");
        }
        int[] array3 = new int[]{3, 4, 6, 9, 10, 14, 15};
        if (checkSum(array3) != 2){
            System.err.println("TEST3 FAILED");
        }
        int[] array4 = new int[]{6, 7, 8, 9, 10, 15, 16};
        if (checkSum(array4) != -1){
            System.err.println("TEST4 FAILED");
        }
        System.out.println("Done!!!");
    }

My error is that it's doing: Lets say array[i] + array[k] > twenty:

expected output:

array[0] + array[6] = 5 + 16 > 20
so do array[0] + array[5] = 5 + 15 = 20
than notice that 5 < 15 so the index is 0.

current output:

array[6] + array [6] -  1 = 16 + 15 > 20
so array[6] - 1 + array [6] -  1 - 1 = 15 + 14 > 20
and so forth...
1
  • This code doesn't use pointers and Java doesn't have pointers, at least in the sense of ones you can do pointer arithmetic on, apart from references. These are indices (or, indexes) into the arrays, whereas pointers are actual memory addresses such as you would find in C or C++, among other languages. Commented Sep 5, 2021 at 21:20

1 Answer 1

1

You don't need a nested loop to do the task. Assuming your input array is always sorted, you can declare two variables in one for loop one starting at the first element of your array and the second at the last element. Check at each step if the sum equals 20 if yes find the index and break the loop, if not increment your first variable or decrement your second variable depending on whether the sum was greater or less than 20.

public static int checkSum(int[] array) {
    int checkIndex = -1;
    int first = 0;
    int last = array.length -1;

    for (int i = first, k = last; i < k; ) {
        if (array[i] + array[k] == 20){
            System.out.println("Found a pair which adds up to 20");
            System.out.println(array [i] + " + " + array[k]);
            //find index of smallest value
            if (array[i] < array[k]){
                checkIndex = i;
            }
            else {
                checkIndex = k;
            }
            //break out of loop if found a valid pair
            break;
        }
        else {
            //you will get here if the sum was not 20. Increment i or decrement k according to sum > 20 0r sum < 20
            if (array[i] + array[k] > 20){
                k--;
            }
            else {
                i++;
            }
        }
    }
    System.out.println("index to return" + checkIndex);
    return  checkIndex;
}
Sign up to request clarification or add additional context in comments.

1 Comment

Wow thank you. For some reason I thought when you’re working with array you default to nested for loops. I see now.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.