Coding for Scrubs: Using Multiple Pointers to Find the Average Pair (JS)

Aymes S.
Aymes S.
Mar 2 · 4 min read

Multiple Pointers:

Welcome back to another series of Coding for Scrubs! Today we are going to be talking about solving algorithms. This is a huge thing to know, especially when prepping for technical interviews. We are going to be learning about multiple pointers. This means we will be creating pointers or values that correspond to an index or position and move towards the beginning, end, or middle based on a certain condition.

Average Pair:

Given a sorted array of integers and a target average, determine if there is a pair of values in the array where the average of the pair equals the target average. There may be more than one pair that matches the average target, but we just want the first pair.

Pseudocode:

Time to figure out the problem in plain English first!

1. Declare two variables/pointers (left and right), one starting in the beginning of the array and one starting at the end 
2. Iterate through the array with a while loop
3. Compare index of the left and right and get the average
4. Create a conditional for if the average is equal to the target average, return true
5. Else return false at the end of the loop

Writing the Function:

First, create the function called averagePair and pass in the two arguments, array and target.

function averagePair(array, target) {
}
console.log(averagePair([1,2,3], 2))
function averagePair(array, target) {
let left = 0
let right = array.length - 1
}
console.log(averagePair([1,2,3], 2))
function averagePair(array, target) {
let left = 0
let right = array.length - 1
while (left < right) {
}
}
console.log(averagePair([1,2,3], 2))
function averagePair(array, target) {
let left = 0
let right = array.length - 1
while (left < right) {
let average = (array[left] + array[right]) / 2
}
}
console.log(averagePair([1,2,3], 2))
function averagePair(array, target) {
let left = 0
let right = array.length - 1
while (left < right) {
let average = (array[left] + array[right]) / 2
if (average === target) {
return true
} else if (average > target) {
right--
} else {
left++
}
}
}
console.log(averagePair([1,2,3], 2))
function averagePair(array, target) {
let left = 0
let right = array.length - 1
while (left < right) {
let average = (array[left] + array[right]) / 2
if (average === target) {
return true
} else if (average > target) {
right--
} else {
left++
}
}
return false
};
console.log(averagePair([1,2,3], 2))

Conclusion:

Thank you guys once again for tuning into another series. I hope this blog post helped you guys learn about multiple pointers and see their purpose!

Nerd For Tech

From Confusion to Clarification

Medium is an open platform where 170 million readers come to find insightful and dynamic thinking. Here, expert and undiscovered voices alike dive into the heart of any topic and bring new ideas to the surface. Learn more

Follow the writers, publications, and topics that matter to you, and you’ll see them on your homepage and in your inbox. Explore

If you have a story to tell, knowledge to share, or a perspective to offer — welcome home. It’s easy and free to post your thinking on any topic. Write on Medium

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store