title: Maximum Sum Iarri Among Rotations Given Array 1
signature: maximum_sum_iarri_among_rotations_given_array_1 <- function(arr, n) {
test_code: 
  
  
  # SOLUTION CODE
  # ============================================
  PLACEHOLDER_CODE_BODY
  
  # TESTING CODE 
  # ============================================
  validate_solution <- function(actual, expected) {
      toString(actual)==toString(expected)
  }
  
  driver <- function(arr, n, expected){
      if (validate_solution(PLACEHOLDER_FN_NAME(arr, n), expected)){
          "PASSED"
      } else{
          "FAILED"
      }
  }
  
  main <- function() {
      cat(sprintf("TEST-0...%s\n",driver(
          list(6L, 6L, 13L, 14L, 16L, 20L, 24L, 24L, 24L, 27L, 28L, 36L, 49L, 51L, 55L, 56L, 62L, 69L, 74L, 74L, 76L, 85L, 86L, 90L, 92L, 98L), 13L, 
          2249L
      )))
  
      cat(sprintf("TEST-1...%s\n",driver(
          list(-42L, 96L, 68L, 64L, 14L, -74L, 76L, 42L, 34L, -92L, -20L, 28L, -80L, -34L, -22L, 96L, -46L, 96L, 10L, -82L, 82L, 50L, -24L, 48L, 56L, 72L, -40L, -86L, 84L, 66L, -62L, 50L, -76L, 34L), 27L, 
          9152L
      )))
  
      cat(sprintf("TEST-2...%s\n",driver(
          list(0L, 0L, 0L, 0L, 0L, 1L, 1L, 1L, 1L, 1L, 1L), 10L, 
          35L
      )))
  
      cat(sprintf("TEST-3...%s\n",driver(
          list(37L, 88L, 70L, 86L, 24L, 62L, 34L, 44L, 37L, 42L, 46L, 34L, 23L, 32L, 55L, 2L, 5L, 70L, 30L, 46L, 40L, 65L, 91L, 4L, 7L, 74L, 46L, 12L, 30L, 22L, 1L, 91L, 89L, 88L, 97L, 6L, 6L, 11L, 33L, 14L, 68L, 24L), 39L, 
          34887L
      )))
  
      cat(sprintf("TEST-4...%s\n",driver(
          list(-92L, -90L, -70L, -70L, -10L, 2L, 10L, 12L, 14L, 40L, 44L, 46L, 64L, 68L, 68L, 96L), 11L, 
          586L
      )))
  
      cat(sprintf("TEST-5...%s\n",driver(
          list(1L, 0L, 1L, 0L, 0L, 0L, 1L, 1L, 0L, 1L, 0L, 1L, 0L, 0L, 1L, 0L, 1L, 1L, 1L, 1L), 15L, 
          58L
      )))
  
      cat(sprintf("TEST-6...%s\n",driver(
          list(9L, 15L, 15L, 17L, 19L, 20L, 21L, 23L, 25L, 25L, 25L, 32L, 32L, 33L, 45L, 51L, 54L, 59L, 68L, 71L, 71L, 71L, 72L, 75L, 78L, 80L, 82L, 82L, 88L, 89L, 92L, 93L, 94L, 97L), 22L, 
          11145L
      )))
  
      cat(sprintf("TEST-7...%s\n",driver(
          list(52L, -78L, -80L, 32L, -56L, -98L, -36L, 86L, 34L, -36L, 42L, 46L, 50L, 0L, 34L, -46L, -2L, -18L, -96L, 12L, -42L, 62L, 32L, 78L, 66L, -8L, 50L, 60L, 10L, -18L, 66L, 80L, -24L, -98L, 8L, 48L, 34L, 44L, -80L, -34L, 72L, 0L, -60L, 52L, 40L, 20L), 45L, 
          14776L
      )))
  
      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, 0L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L), 33L, 
          297L
      )))
  
      cat(sprintf("TEST-9...%s\n",driver(
          list(45L, 35L, 25L, 7L, 24L, 73L, 25L, 86L, 48L, 70L, 47L, 91L, 96L, 15L, 39L, 9L), 8L, 
          1359L
      )))
  
  }
  
  main()
source_py: def maximum_sum_iarri_among_rotations_given_array_1(arr, n):
      cum_sum = 0
      for i in range(0, n):
          cum_sum += arr[i]
      curr_val = 0
      for i in range(0, n):
          curr_val += (i * arr[i])
      res = curr_val
      for i in range(1, n):
          next_val = ((curr_val - (cum_sum - arr[(i - 1)])) + (arr[(i - 1)] * (n - 1)))
          curr_val = next_val
          res = max(res, next_val)
      return res
source_cpp: int maximum_sum_iarri_among_rotations_given_array_1(vector<int> arr, int n) {
    int cum_sum = 0;
    for ( int i = 0;
    i < n;
    i ++ ) cum_sum += arr [ i ];
    int curr_val = 0;
    for ( int i = 0;
    i < n;
    i ++ ) curr_val += i * arr [ i ];
    int res = curr_val;
    for ( int i = 1;
    i < n;
    i ++ ) {
      int next_val = curr_val - ( cum_sum - arr [ i - 1 ] ) + arr [ i - 1 ] * ( n - 1 );
      curr_val = next_val;
      res = max ( res, next_val );
    }
    return res;
  }