title: Efficient Search In An Array Where Difference Between Adjacent Is 1
signature: int efficientSearchInAnArrayWhereDifferenceBetweenAdjacentIs1(vector<int> arr, int n, int x) {
test_code: #include <algorithm>
  #include <assert.h>
  #include <cstdlib>
  #include <iostream>
  #include <map>
  #include <set>
  #include <string>
  #include <tuple> 
  #include <unordered_map>
  #include <vector>
  #include <math.h>
  #include <cmath>
  #include <bits/stdc++.h>
  #include <numeric>
  #include <functional>
  
  using namespace std;
  
  // SOLUTION CODE
  // ============================================
  PLACEHOLDER_CODE_BODY
  
  // TESTING CODE 
  // ============================================
  bool validateSolution(int actual, int expected){
      return actual == expected;
  }
  
  string driver(vector<int> arr, int n, int x, int expected){
      try
      {
          if (validateSolution(PLACEHOLDER_FN_NAME(arr, n, x), expected)){
              return "PASSED";
          }
          return "FAILED";
      }
      catch (const std::overflow_error& exception_obj)
      {
          return typeid(exception_obj).name();
      }
      catch (const std::runtime_error& exception_obj)
      {
          return typeid(exception_obj).name();
      }
      catch (const std::exception& exception_obj)
      {
          return typeid(exception_obj).name();
      }
      catch (...)
      {
          return "UNK_ERROR";
      }
  }
  
  int main() {
      string result = "";
  
      result = driver({5, 4, 5, 6, 5, 4, 3, 2}, 29, 6, 3); 
      cout << "TEST-" << 0 << "..."
           <<  result
           << "\n";
  
      result = driver({5, 4, 5, 6, 5, 4, 3, 2}, 19, 3, 6); 
      cout << "TEST-" << 1 << "..."
           <<  result
           << "\n";
  
      result = driver({0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1}, 37, 1, 14); 
      cout << "TEST-" << 2 << "..."
           <<  result
           << "\n";
  
      result = driver({50, 51, 52, 51, 50, 49, 48}, 17, 49, 5); 
      cout << "TEST-" << 3 << "..."
           <<  result
           << "\n";
  
      result = driver({-86, -68, -32, -6, 6, 10, 30, 34, 58, 92}, 6, 6, -1); 
      cout << "TEST-" << 4 << "..."
           <<  result
           << "\n";
  
      result = driver({1, 1, 1, 0, 0, 1, 0, 0, 0, 1, 1, 1, 0, 1, 0, 0, 1, 1, 1, 0, 1, 0, 1, 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 1, 1, 0, 1, 0, 1, 1, 1, 0, 0}, 27, 22, -1); 
      cout << "TEST-" << 5 << "..."
           <<  result
           << "\n";
  
      result = driver({58}, 0, 0, -1); 
      cout << "TEST-" << 6 << "..."
           <<  result
           << "\n";
  
      result = driver({-64, 78, 58, 36, 48, 80, -80, 74, -98, 46, -48, 24, 80, 72, 90, -46, 14, 68, 38, 58, -54, 80, 44, -62, 34, -28, 92, 84, 90, 44, -26, 88, 18, 22, -32, 54, 58, 92}, 24, 34, -1); 
      cout << "TEST-" << 7 << "..."
           <<  result
           << "\n";
  
      result = driver({0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1}, 35, 1, 21); 
      cout << "TEST-" << 8 << "..."
           <<  result
           << "\n";
  
      result = driver({5}, 0, 0, -1); 
      cout << "TEST-" << 9 << "..."
           <<  result
           << "\n";
  
      return 0;
  }
source_py: def efficient_search_in_an_array_where_difference_between_adjacent_is_1(arr, n, x):
      i = 0
      while (i <= (n - 1)):
          if (arr[i] == x):
              return i
          i += abs((arr[i] - x))
      return (- 1)
source_cpp: int efficient_search_in_an_array_where_difference_between_adjacent_is_1(vector<int> arr, int n, int x) {
    int i = 0;
    while ( i <= n - 1 ) {
      if ( arr [ i ] == x ) return i;
      i += abs ( arr [ i ] - x );
    }
    return - 1;
  }