user10660670
user10660670

Reputation:

find values on parallel array recursively

I need to write a recursive method that takes two parallel arrays and the word to find, look for the word specified and sum the values of each time the index matches on the other array. For example:

array1 = "Toyota", "Honda", "Honda", "Toyota", "Toyota", ......n

array2 = 22500, 18000, 29000, 22500, 32000, ....... n

If I say that I need to look for the word "Toyota", it should sum the values on the second array anytime it finds the index. In this case, it should sum, 22500+22500+32000.

How can I make my recursion method so it takes the appropriate parameters and makes the calculations recursively. I will be using hard coded values.

This is what I have so far. I'm pretty sure my recursive method needs more parameters, but i'll see if somebody can help me

static int printRecursively(int A[], int N) {
        if(N <= 0) {

            return 0;

        }

        return (printRecursively(A, N - 1) + A[N -1]);
    }

}

Upvotes: 0

Views: 311

Answers (3)

Tim Biegeleisen
Tim Biegeleisen

Reputation: 522284

I don't think your current data structures are optimal for this problem. Instead, I recommend using a hashmap of cars to values:

Map<String, List<Integer>> map = new HashMap<>();
List<Integer> values = Arrays.asList(22500, 22500, 32000);
map.put("Toyota", values);
values = Arrays.asList(18000, 29000);
map.put("Honda", values);

Then, to get the sum of values for a given car, we can easily use a stream:

int sum = map.get("Toyota").stream().reduce(0, (a, b) -> a + b);

Generally speaking, a good way to approach this is to represent the data where the car is a key, and the values are to what that key is pointing.

Upvotes: 1

Kartik
Kartik

Reputation: 7917

Start with a "cursor" at position 0. Then return the sum of the number at that position and whatever sum is returned from the same method call with cursor value as cursor+1. If there is nothing at cursor+1, that means you have reached the end of the array, in which case just return the number at that position.

public static void main(String[] args) {
    String arr1[] = new String[]{"Toyota", "Honda", "Honda", "Toyota", "Toyota"};
    int arr2[] = new int[]{22500, 18000, 29000, 22500, 32000};

    System.out.println(getSum(arr1, arr2, "Toyota", 0));
}

private static int getSum(String arr1[], int arr2[], String word, int cursor) {
    if (cursor == arr1.length - 1) return arr1[arr1.length - 1].equals(word) ? arr2[arr2.length - 1] : 0;
    return arr1[cursor].equals(word)
            ? arr2[cursor] + getSum(arr1, arr2, word, cursor + 1)
            : getSum(arr1, arr2, word, cursor + 1);
}

Output

77000

Upvotes: 2

T-Fowl
T-Fowl

Reputation: 704

Something alike the following may suit your needs

public static int recursiveSum(String search, String[] names, int[] values, int start) {
    // Check that the two arrays are of the same length
    // And that start does not exceed the bounds of either
    if((names.length != values.length) || start > names.length)
        return 0;

    // If the value at the 'start' of the array is the one we're looking for
    if(names[start].equals(search)) {
        return values[start] + recursiveSum(search, names, values, start + 1);
    } else {
        // Otherwise just skip onto the next value of the arrays
        return recursiveSum(search, names, values, start + 1);
    }
}


recursiveSum("Toyota", names, values, 0)

Upvotes: 0

Related Questions