Please help me with this breadth first search algorithm in C++. Thanks in advanc
ID: 3821764 • Letter: P
Question
Please help me with this breadth first search algorithm in C++. Thanks in advance.
Difference of Pairs on a BST: Write a method/function that outputs two pointers pointing to two nodes, x and y. in a given BST where the key values of x and y differ by a given integer k. Recall that we had given the following algorithm ¡n lecture that on an sorted input array A and given value k returns two indices i and j. where A[i] - A[j] = k. or report error if such i and j do not exist: i leftarrow 1; j leftarrow 2; n leftarrow length[A]//assuming indices of A start with 1 while (j lessthanorequalto n and i lessthanorequalto n) d leftarrow A[i] - A[j] if (d==k) return (i, j) if (dExplanation / Answer
Here is the implementation of the above algorithm for you:
#include <iostream>
void differenceOfPairs(int A[], int n, int k, int& i, int& j)
{
i = 1;
j = 2;
bool found = false;
while(j <= n && i <= n)
{
int dist = A[i] - A[j];
if(dist == k)
{
found = true;
break;
}
if(dist < k)
i++;
else
j++;
}
if(!found)
i = -1;
j = -1;
}
Related Questions
Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.