title: Given A Sorted And Rotated Array Find If There Is A Pair With A Given Sum 1
signature: function given_a_sorted_and_rotated_array_find_if_there_is_a_pair_with_a_given_sum_1(arr, n, x)
test_code: 
  
  -- SOLUTION CODE
  -- ============================================
  PLACEHOLDER_CODE_BODY
  
  -- TESTING CODE 
  -- ============================================
  function isCorrect(actual, expected)
      if type(expected) ~= type(actual) then
          return false
      end
      
      if type(expected) == 'table' then
          for k,v_expected in pairs(expected) do
              
              if actual[k] == nil then
                  return false
              end
              local v_actual = actual[k]
              if not isCorrect(v_actual, v_expected) then
                  return false
              end
              actual[k]=nil
          end
      
          if next(actual) then 
              return false
          end
          return true
      else
          return actual == expected
      end
  end
  
  function validateSolution(actual, expected)
      return isCorrect(actual,expected)
  end
  
  function driver(arr, n, x, expected)
      local exec_result;
      local status, error_msg = pcall(function () 
          exec_result=PLACEHOLDER_FN_NAME(arr, n, x) 
      end)
      if status then
          if validateSolution(exec_result,expected) then
              return "PASSED"
          end
          return "FAILED"
      else
          return string.format("ERROR=%s",string.gsub(error_msg,".*:[%d]+: ",""))
      end
  end
      
  function main()
      result = driver({24, 54}, 1, 1, 0)
      print(string.format("TEST-0...%s",result))
  
      result = driver({68, -30, -18, -6, 70, -40, 86, 98, -24, -48}, 8, 8, 0)
      print(string.format("TEST-1...%s",result))
  
      result = driver({0, 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, 1}, 33, 28, 0)
      print(string.format("TEST-2...%s",result))
  
      result = driver({84, 44, 40, 45, 2, 41, 52, 17, 50, 41, 5, 52, 48, 90, 13, 55, 34, 55, 94, 44, 41, 2}, 18, 16, 0)
      print(string.format("TEST-3...%s",result))
  
      result = driver({-92, -76, -74, -72, -68, -64, -58, -44, -44, -38, -26, -24, -20, -12, -8, -8, -4, 10, 10, 10, 20, 20, 26, 26, 28, 50, 52, 54, 60, 66, 72, 74, 78, 78, 78, 80, 86, 88}, 29, 30, 4)
      print(string.format("TEST-4...%s",result))
  
      result = driver({1, 1, 1, 1, 0, 1, 0, 0, 1, 0, 0, 0, 0, 1, 0, 1, 0, 1, 1, 1}, 19, 10, 0)
      print(string.format("TEST-5...%s",result))
  
      result = driver({5, 5, 15, 19, 22, 24, 26, 27, 28, 32, 37, 39, 40, 43, 49, 52, 55, 56, 58, 58, 59, 62, 67, 68, 77, 79, 79, 80, 81, 87, 95, 95, 96, 98, 98}, 28, 34, 1)
      print(string.format("TEST-6...%s",result))
  
      result = driver({-98, 28, 54, 44, -98, -70, 48, -98, 56, 4, -18, 26, -8, -58, 30, 82, 4, -38, 42, 64, -28}, 17, 14, 0)
      print(string.format("TEST-7...%s",result))
  
      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}, 24, 24, 0)
      print(string.format("TEST-8...%s",result))
  
      result = driver({26, 72, 74, 86, 98, 86, 22, 6, 95, 36, 11, 82, 34, 3, 50, 36, 81, 94, 55, 30, 62, 53, 50, 95, 32, 83, 9, 16}, 19, 16, 0)
      print(string.format("TEST-9...%s",result))
  
  end
  
  main()
source_py: def given_a_sorted_and_rotated_array_find_if_there_is_a_pair_with_a_given_sum_1(arr, n, x):
      for i in range(n):
          if (arr[i] > arr[(i + 1)]):
              break
      l = ((i + 1) % n)
      r = i
      cnt = 0
      while (l != r):
          if ((arr[l] + arr[r]) == x):
              cnt += 1
              if (l == (((r - 1) + n) % n)):
                  return cnt
              l = ((l + 1) % n)
              r = (((r - 1) + n) % n)
          elif ((arr[l] + arr[r]) < x):
              l = ((l + 1) % n)
          else:
              r = (((n + r) - 1) % n)
      return cnt
source_cpp: int given_a_sorted_and_rotated_array_find_if_there_is_a_pair_with_a_given_sum_1(vector<int> arr, int n, int x) {
    int i;
    for ( i = 0;
    i < n - 1;
    i ++ ) if ( arr [ i ] > arr [ i + 1 ] ) break;
    int l = ( i + 1 ) % n;
    int r = i;
    int cnt = 0;
    while ( l != r ) {
      if ( arr [ l ] + arr [ r ] == x ) {
        cnt ++;
        if ( l == ( r - 1 + n ) % n ) {
          return cnt;
        }
        l = ( l + 1 ) % n;
        r = ( r - 1 + n ) % n;
      }
      else if ( arr [ l ] + arr [ r ] < x ) l = ( l + 1 ) % n;
      else r = ( n + r - 1 ) % n;
    }
    return cnt;
  }