1

I'm looking for a single recursive structure algorithm to find both maximum and minimum values of an array. I've found the following pseudocode on here:

FindMaxAndMin(A, max, min)
    if (|A| == 1)
        if (max < A[1])
            max = A[1]
        if (min > A[1])
            min = A[1]
        return (min, max)
    cen = |A| /2
    l = A[:cen-1]
    h = A[cen:]
    (min, max) = FindMaxAndMin(l, min, max)
    (min, max) = FindMaxAndMin(h, min, max)
    return (min, max)

So I was wondering firstly if this counts as a single recursive structure as it all takes place under the first if. If this is a single recursive structure, I was firstly wondering what |A| represents, can't find it anywhere online, and how would it work call by call when A = (3,2,4,1) for example?

4
  • 2
    |A| is the cardinality (quantity of elements) of A. Commented Dec 12, 2018 at 0:09
  • 3
    "if this counts as a single recursive structure " - you should ask whoever give you the assignment... Commented Dec 12, 2018 at 0:11
  • @AlexeiLevenkov it is an online task, I've googled the meaning and I cannot find it. I'm assuming it means something quite trivial though perhaps I've never heard it been called that. Commented Dec 12, 2018 at 1:39
  • @bemzoo It's not a commonly used term with a fixed meaning. Would you mind providing a link to the original problem? Commented Dec 12, 2018 at 5:39

1 Answer 1

1

|A| is just the length of the array

you can debug and follow the steps here

(becuse i use js i couldnt return 2 values thats way i changes it to an array

keep in mind that minMax[0] = min

and minMax[1] = max

i initilized minMax[0] (min) with MAX_SAFE_INTEGER

and minMax[0] (max) with MIN_SAFE_INTEGER)

const FindMaxAndMin = (A, minMax)=>{
    if (A.length === 1){
        if (minMax[1] < A[0])
            minMax[1] = A[0]
        if (minMax[0] > A[0])
            minMax[0] = A[0]
        return minMax
    }
    let cen = A.length /2
    let l = A.slice(0,cen)
    let h = A.slice(cen,A.length)
    minMax = FindMaxAndMin(l, minMax)
    minMax = FindMaxAndMin(h, minMax)
    return minMax
  }
  
  console.log(FindMaxAndMin([3,4,1,2],[Number.MAX_SAFE_INTEGER , Number.MIN_SAFE_INTEGER]))

Sign up to request clarification or add additional context in comments.

Comments

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.