title: Efficient Search In An Array Where Difference Between Adjacent Is 1
signature: efficient_search_in_an_array_where_difference_between_adjacent_is_1 <- function(arr, n, x) {
test_code: 
  
  
  # SOLUTION CODE
  # ============================================
  PLACEHOLDER_CODE_BODY
  
  # TESTING CODE 
  # ============================================
  validate_solution <- function(actual, expected) {
      toString(actual)==toString(expected)
  }
  
  driver <- function(arr, n, x, expected){
      if (validate_solution(PLACEHOLDER_FN_NAME(arr, n, x), expected)){
          "PASSED"
      } else{
          "FAILED"
      }
  }
  
  main <- function() {
      cat(sprintf("TEST-0...%s\n",driver(
          list(5L, 4L, 5L, 6L, 5L, 4L, 3L, 2L), 29L, 6L, 
          3L
      )))
  
      cat(sprintf("TEST-1...%s\n",driver(
          list(5L, 4L, 5L, 6L, 5L, 4L, 3L, 2L), 19L, 3L, 
          6L
      )))
  
      cat(sprintf("TEST-2...%s\n",driver(
          list(0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L), 37L, 1L, 
          14L
      )))
  
      cat(sprintf("TEST-3...%s\n",driver(
          list(50L, 51L, 52L, 51L, 50L, 49L, 48L), 17L, 49L, 
          5L
      )))
  
      cat(sprintf("TEST-4...%s\n",driver(
          list(-86L, -68L, -32L, -6L, 6L, 10L, 30L, 34L, 58L, 92L), 6L, 6L, 
          -1L
      )))
  
      cat(sprintf("TEST-5...%s\n",driver(
          list(1L, 1L, 1L, 0L, 0L, 1L, 0L, 0L, 0L, 1L, 1L, 1L, 0L, 1L, 0L, 0L, 1L, 1L, 1L, 0L, 1L, 0L, 1L, 0L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 0L, 1L, 1L, 0L, 1L, 0L, 1L, 1L, 1L, 0L, 0L), 27L, 22L, 
          -1L
      )))
  
      cat(sprintf("TEST-6...%s\n",driver(
          list(58L), 0L, 0L, 
          -1L
      )))
  
      cat(sprintf("TEST-7...%s\n",driver(
          list(-64L, 78L, 58L, 36L, 48L, 80L, -80L, 74L, -98L, 46L, -48L, 24L, 80L, 72L, 90L, -46L, 14L, 68L, 38L, 58L, -54L, 80L, 44L, -62L, 34L, -28L, 92L, 84L, 90L, 44L, -26L, 88L, 18L, 22L, -32L, 54L, 58L, 92L), 24L, 34L, 
          -1L
      )))
  
      cat(sprintf("TEST-8...%s\n",driver(
          list(0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L), 35L, 1L, 
          21L
      )))
  
      cat(sprintf("TEST-9...%s\n",driver(
          list(5L), 0L, 0L, 
          -1L
      )))
  
  }
  
  main()
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;
  }