-1

convert method "FINAL" to divide and conquer algorithm

the task sounded like this: The buyer has n coins of

H1,...,Hn.

The seller has m coins in denominations of

B1,...,Bm.

Can the buyer purchase the item the cost S so that the seller has an exact change (if necessary).

fun Final(H: ArrayList<Int>, B: ArrayList<Int>, S: Int): Boolean {
    var Clon_Price = false;
    var Temp: Int;

    for (i in H) {
        if (i == S)
            return true;
    }

    for (i in H.withIndex()) {
        Temp = i.value - S;
        for (j in B) {
            if (j == Temp)
                Clon_Price = true;
        }
    }

    return Clon_Price;
}


fun main(args: Array<String>) {


    val H:ArrayList<Int> = ArrayList();
    val B:ArrayList<Int> = ArrayList();

    println("Enter the number of coins the buyer has:");
    var n: Int = readln().toInt();

    println("Enter their nominal value:")
    while (n > 0){
        H.add(readln().toInt());
        n--
    }

    println("Enter the number of coins the seller has:");
    var m: Int = readln().toInt();

    println("Enter their nominal value:")
    while (m > 0){
        B.add(readln().toInt());
        m--
    }

    println("Enter the product price:");
    val S = readln().toInt();

    if(Final(H,B,S)){
        println("YES");
    }
    else{
        println("No!");
    }
Lajos Arpad
  • 64,414
  • 37
  • 100
  • 175
  • Please revise your post title to ask a clear, specific question. Don't add tags. See [ask]. – isherwood Oct 20 '22 at 15:50
  • @isherwood the question was clear and specific to me. The only fault of the asker was that he did not have a demonstrable try. But maybe he just didn't know where to start. – Lajos Arpad Oct 20 '22 at 16:00
  • It's not at all specific. It's quite broad. And it's not a question. And it contains tags. Please see [ask]. – isherwood Oct 20 '22 at 16:58
  • @isherwood You are right that no question is being asked. The implicit question is: "how can I achieve that?". It contains tags, true. But I had no difficulty understanding the exact problem, so it does not appear to be vague to me. – Lajos Arpad Oct 20 '22 at 19:44

1 Answers1

0

Introduction

Since this is an assignment, I will only give you insights to solve this problem and you will need to do the coding yourself.

The algorithm

  • Receives two ArrayList<Int> and an Int parameter
  • if the searched (S) element can be found in H, then the result is true
  • Otherwise it loops H
    • Computes the difference between the current element and S
    • Searches for a match in B and if it's found, then true is being returned
  • If the method has not returned yet, then return false;

Divide et impera (Divide and conquer)

Divide and conquer is the process of breaking down a complicated task into similar, but simpler subtasks, repeating this breaking down until the subtasks become trivial (this was the divide part) and then, using the results of the trivial subtasks we can solve the slightly more complicated subtasks and go upwards in our layers of unsolved complexities until the problem is solved (this is the conquer part).

A very handy data-structure to use is the Stack. You can use the stack of your memory, which are fancy words for recursion, or, you can solve it iteratively, by managing such a stack yourself.

This specific problem

This algorithm does not seem to necessitate divide and conquer, given the fact that you only have two array lists that can be iterated, so, I guess, this is an early assignment.

To make sure this is divide and conquer, you can add two parameters to your method (which are 0 and length - 1 at the start) that reflect the current problem-space. And upon each call, check whether the starting and ending index (the two new parameters) are equal. If they are, you already have a trivial, simplified subtask and you just iterate the second ArrayList.

If they are not equal, then you still need to divide. You can simply

//... Some code here
return Final(H, B, S, start, end / 2) || Final(H, B, S, end / 2 + 1, end);

(there you go, I couldn't resist writing code, after all)

for your nontrivial cases. This automatically breaks down the problem into sub-problems.

Self-criticism

The idea above is a simplistic solution for you to get the gist. But, in reality, programmers dislike recursion, as it can lead to trouble. So, once you complete the implementation of the above, you are well-advised to convert your algorithm to make sure it's iterative, which should be fairly easy once you succeeded implementing the recursive version.

Lajos Arpad
  • 64,414
  • 37
  • 100
  • 175