简体   繁体   中英

How would I write an algorithm to find the greatest jump from number to number in a sequence?

If your sequence is 4 2 1 , the largest jump is from 4 to 2. If your sequence is 3 10 5 16 8 4 2 1 , the largest jump is from 5 to 16.

I've made an algorithm however I'm not completely sure what I have done wrong (whever I haven't made the loop properly, set my variables correctly, or something else). I'm not sure what I need to set my index, BiggestDiff, or CurrentDiff too. I tried using a while loop to compare each number in my vector but I get zero (I'm assuming because I set BiggestDiff to zero)

If anyone can point me in the right direction, show me an example, or something else, that will be greatly appreciated.

Here is my code below

int findBiggestDiff(std::vector<int> sequence)
{
  int index = 0;
  int BiggestDiff = 0 ;
  int CurrentDiff = BiggestDiff;
  CurrentDiff = std::abs(sequence[index] - sequence[index + 1]);
  while (index < sequence.size())
  {
    if (CurrentDiff > BiggestDiff)
    {
      BiggestDiff = CurrentDiff;
    }

    return index;
  }
}

Try this:

{
    int indexOfBiggestJump = 0;
    int BiggestDiff = 0 ;
    int CurrentDiff = BiggestDiff;

    for(int i = 0; i < sequence.size() - 1; i++) {
        CurrentDiff = std::abs(sequence[i] - sequence[i + 1]);
        if (CurrentDiff > BiggestDiff)
        {
            BiggestDiff = CurrentDiff;
            indexOfBiggestJump = i;
        }
    }
    return indexOfBiggestJump;
}

There are several errors in your code.

  1. your return index literally does nothing, only returns index (which will be 0) always.

  2. you are not saving the index of the biggest jump anywhere.

  3. if you are looking positions i and i + 1 , you must go until sequence.size() - 1 , otherwise you will look out of the bounds of sequence .

You aren't recalculating CurrentDiff at all. Also, your return statement in the in the wrong spot. You can do something like this (not tested)

int findLargest( const std::vector<int> &sequence ) {
  if ( sequence.size() < 2 ) return -1; // if there's not at least two elements, there's nothing valid.
  int index = 0;
  int biggestIndex = -1;
  int biggestDiff = -1;

  while (index < sequence.size() - 1) // -1 so that the +1 below doesn't go out of range
  {
    // get the current difference
    int currentDiff = std::abs(sequence[index] - sequence[index + 1]);
    if (currentDiff > biggestDiff)
    {
       // update stats
       biggestIndex = index;
       biggestDiff = currentDiff;
    }
    ++index;
  }
  return biggestIndex
}

int main() {
   //…
   int index = findLargest( sequence );
   if ( index != -1 ) {
       std::cout << "Biggest difference was between " << sequence[index] << " and " << sequence[index+1];
   }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM