title: Given A Sorted And Rotated Array Find If There Is A Pair With A Given Sum 1
signature: given_a_sorted_and_rotated_array_find_if_there_is_a_pair_with_a_given_sum_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(24L, 54L), 1L, 1L, 
          0L
      )))
  
      cat(sprintf("TEST-1...%s\n",driver(
          list(68L, -30L, -18L, -6L, 70L, -40L, 86L, 98L, -24L, -48L), 8L, 8L, 
          0L
      )))
  
      cat(sprintf("TEST-2...%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, 0L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L), 33L, 28L, 
          0L
      )))
  
      cat(sprintf("TEST-3...%s\n",driver(
          list(84L, 44L, 40L, 45L, 2L, 41L, 52L, 17L, 50L, 41L, 5L, 52L, 48L, 90L, 13L, 55L, 34L, 55L, 94L, 44L, 41L, 2L), 18L, 16L, 
          0L
      )))
  
      cat(sprintf("TEST-4...%s\n",driver(
          list(-92L, -76L, -74L, -72L, -68L, -64L, -58L, -44L, -44L, -38L, -26L, -24L, -20L, -12L, -8L, -8L, -4L, 10L, 10L, 10L, 20L, 20L, 26L, 26L, 28L, 50L, 52L, 54L, 60L, 66L, 72L, 74L, 78L, 78L, 78L, 80L, 86L, 88L), 29L, 30L, 
          4L
      )))
  
      cat(sprintf("TEST-5...%s\n",driver(
          list(1L, 1L, 1L, 1L, 0L, 1L, 0L, 0L, 1L, 0L, 0L, 0L, 0L, 1L, 0L, 1L, 0L, 1L, 1L, 1L), 19L, 10L, 
          0L
      )))
  
      cat(sprintf("TEST-6...%s\n",driver(
          list(5L, 5L, 15L, 19L, 22L, 24L, 26L, 27L, 28L, 32L, 37L, 39L, 40L, 43L, 49L, 52L, 55L, 56L, 58L, 58L, 59L, 62L, 67L, 68L, 77L, 79L, 79L, 80L, 81L, 87L, 95L, 95L, 96L, 98L, 98L), 28L, 34L, 
          1L
      )))
  
      cat(sprintf("TEST-7...%s\n",driver(
          list(-98L, 28L, 54L, 44L, -98L, -70L, 48L, -98L, 56L, 4L, -18L, 26L, -8L, -58L, 30L, 82L, 4L, -38L, 42L, 64L, -28L), 17L, 14L, 
          0L
      )))
  
      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), 24L, 24L, 
          0L
      )))
  
      cat(sprintf("TEST-9...%s\n",driver(
          list(26L, 72L, 74L, 86L, 98L, 86L, 22L, 6L, 95L, 36L, 11L, 82L, 34L, 3L, 50L, 36L, 81L, 94L, 55L, 30L, 62L, 53L, 50L, 95L, 32L, 83L, 9L, 16L), 19L, 16L, 
          0L
      )))
  
  }
  
  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;
  }