📌  相关文章
📜  LR范围中的元素之和,其中上半部分和后半部分用奇数和偶数填充

📅  最后修改于: 2021-04-28 00:13:45             🧑  作者: Mango

给定数字N,创建一个数组,使该数组的前半部分填充为奇数,直到N,该数组的后半部分填充为偶数。还给出了L和R索引,任务是在[L,R]范围内打印数组中元素的总和。
例子:

一个简单的方法是形成大小为N的数组,并从L迭代到R,然后返回和。

C++
// C++ program to find the sum between L-R
// range by creating the array
// Naive Approach
#include
using namespace std;
 
    // Function to find the sum between L and R
    int rangesum(int n, int l, int r)
    {
        // array created
        int arr[n];
 
        // fill the first half of array
        int c = 1, i = 0;
        while (c <= n) {
            arr[i++] = c;
            c += 2;
        }
 
        // fill the second half of array
        c = 2;
        while (c <= n) {
            arr[i++] = c;
            c += 2;
        }
        int sum = 0;
 
        // find the sum between range
        for (i = l - 1; i < r; i++) {
            sum += arr[i];
        }
 
        return sum;
    }
 
    // Driver Code
    int  main()
    {
        int n = 12;
        int l = 1, r = 11;
        cout<<(rangesum(n, l, r));
    }
// This code is contributed by
// Sanjit_Prasad


Java
// Java program to find the sum between L-R
// range by creating the array
// Naive Approach
import java.io.*;
public class GFG {
 
    // Function to find the sum between L and R
    static int rangesum(int n, int l, int r)
    {
        // array created
        int[] arr = new int[n];
 
        // fill the first half of array
        int c = 1, i = 0;
        while (c <= n) {
            arr[i++] = c;
            c += 2;
        }
 
        // fill the second half of array
        c = 2;
        while (c <= n) {
            arr[i++] = c;
            c += 2;
        }
        int sum = 0;
 
        // find the sum between range
        for (i = l - 1; i < r; i++) {
            sum += arr[i];
        }
 
        return sum;
    }
 
    // Driver Code
    public static void main(String[] args)
    {
        int n = 12;
        int l = 1, r = 11;
        System.out.println(rangesum(n, l, r));
    }
}


Python3
# Python3 program to find the sum between L-R
# range by creating the array
# Naive Approach
 
# Function to find the sum between L and R
def rangesum(n, l, r):
 
    # array created
    arr = [0] * n;
 
    # fill the first half of array
    c = 1; i = 0;
    while (c <= n):
        arr[i] = c;
        i += 1;
        c += 2;
     
    # fill the second half of array
    c = 2;
    while (c <= n):
        arr[i] = c;
        i += 1;
        c += 2;
 
    sum = 0;
 
    # find the sum between range
    for i in range(l - 1, r, 1):
        sum += arr[i];
     
    return sum;
 
# Driver Code
if __name__ == '__main__':
 
    n = 12;
    l, r = 1, 11;
    print(rangesum(n, l, r));
     
# This code contributed by PrinciRaj1992


C#
// C# program to find the sum
// between L-R range by creating
// the array Naive Approach
using System;
 
class GFG
{
 
    // Function to find the
    // sum between L and R
    static int rangesum(int n,
                        int l, int r)
    {
        // array created
        int[] arr = new int[n];
 
        // fill the first
        // half of array
        int c = 1, i = 0;
        while (c <= n)
        {
            arr[i++] = c;
            c += 2;
        }
 
        // fill the second
        // half of array
        c = 2;
        while (c <= n)
        {
            arr[i++] = c;
            c += 2;
        }
        int sum = 0;
 
        // find the sum
        // between range
        for (i = l - 1; i < r; i++)
        {
            sum += arr[i];
        }
 
        return sum;
    }
 
    // Driver Code
    public static void Main()
    {
        int n = 12;
        int l = 1, r = 11;
        Console.WriteLine(rangesum(n, l, r));
    }
}
 
// This code is contributed
// by inder_verma.


PHP


Javascript


C++
// C++ program to find the sum between L-R
// range by creating the array
// Naive Approach
#include
using namespace std;
 
// // Function to calculate the sum if n is even
int sumeven(int n, int l, int r)
{
    int sum = 0;
    int mid = n / 2;
 
    // both l and r are to the left of mid
    if (r <= mid)
    {
        // first and last element
        int first = (2 * l - 1);
        int last = (2 * r - 1);
 
        // Total number of terms in
        // the sequence is r-l+1
        int no_of_terms = r - l + 1;
 
        // use of formula derived
        sum = ((no_of_terms) * ((first + last))) / 2;
    }
     
    // both l and r are to the right of mid
    else if (l >= mid)
    {
        // first and last element
        int first = (2 * (l - n / 2));
        int last = (2 * (r - n / 2));
 
        int no_of_terms = r - l + 1;
 
        // Use of formula derived
        sum = ((no_of_terms) * ((first + last))) / 2;
    }
 
    // left is to the left of mid and
    // right is to the right of mid
    else
    {
         
        // Take two sums i.e left and
        // right differently and add
        int sumleft = 0, sumright = 0;
 
        // first and last element
        int first_term1 = (2 * l - 1);
        int last_term1 = (2 * (n / 2) - 1);
 
        // total terms
        int no_of_terms1 = n / 2 - l + 1;
 
        // no of terms
        sumleft = ((no_of_terms1) *
                  ((first_term1 + last_term1))) / 2;
 
        // The first even number is 2
        int first_term2 = 2;
 
        // The last element is given by 2*(r-n/2)
        int last_term2 = (2 * (r - n / 2));
        int no_of_terms2 = r - mid;
 
        // formula applied
        sumright = ((no_of_terms2) *
                   ((first_term2 + last_term2))) / 2;
        sum = (sumleft + sumright);
    }
    return sum;
}
 
// Function to calculate the sum if n is odd
int sumodd(int n, int l, int r)
{
 
    // take ceil value if n is odd
    int mid = n / 2 + 1;
    int sum = 0;
 
    // // both l and r are to the left of mid
    if (r <= mid)
    {
        // first and last element
        int first = (2 * l - 1);
        int last = (2 * r - 1);
 
        // number of terms
        int no_of_terms = r - l + 1;
 
        // formula
        sum = ((no_of_terms) *
             ((first + last))) / 2;
    }
 
    // // both l and r are to the right of mid
    else if (l > mid)
    {
 
        // firts and last ter,
        int first = (2 * (l - mid));
        int last = (2 * (r - mid));
 
        // no of terms
        int no_of_terms = r - l + 1;
 
        // formula used
        sum = ((no_of_terms) *
              ((first + last))) / 2;
    }
 
    // If l is on left and r on right
    else
    {
 
        // calculate separate sums
        int sumleft = 0, sumright = 0;
 
        // first half
        int first_term1 = (2 * l - 1);
        int last_term1 = (2 * mid - 1);
 
        // calculate terms
        int no_of_terms1 = mid - l + 1;
        sumleft = ((no_of_terms1) *
                  ((first_term1 + last_term1))) / 2;
 
        // second half
        int first_term2 = 2;
        int last_term2 = (2 * (r - mid));
        int no_of_terms2 = r - mid;
        sumright = ((no_of_terms2) *
                   ((first_term2 + last_term2))) / 2;
 
        // add both halves
        sum = (sumleft + sumright);
    }
    return sum;
}
 
// Function to find the sum between L and R
int rangesum(int n, int l, int r)
{
    int sum = 0;
 
    // If n is even
    if (n % 2 == 0)
        return sumeven(n, l, r);
 
    // If n is odd
    else
        return sumodd(n, l, r);
}
 
// Driver Code
int main()
{
    int n = 12;
    int l = 1, r = 11;
    cout << (rangesum(n, l, r));
}
 
// This code is contributed by 29AjayKumar


Java
// Java program to find the sum between L-R
// range by creating the array
// Efficient Approach
import java.io.*;
public class GFG {
 
    // // Function to calculate the sum if n is even
    static int sumeven(int n, int l, int r)
    {
        int sum = 0;
        int mid = n / 2;
 
        // both l and r are to the left of mid
        if (r <= mid) {
            // first and last element
            int first = (2 * l - 1);
            int last = (2 * r - 1);
 
            // Total number of terms in
            // the sequence is r-l+1
            int no_of_terms = r - l + 1;
 
            // use of formula derived
            sum = ((no_of_terms) * ((first + last))) / 2;
        }
        // both l and r are to the right of mid
        else if (l >= mid) {
            // // first and last element
            int first = (2 * (l - n / 2));
            int last = (2 * (r - n / 2));
 
            int no_of_terms = r - l + 1;
 
            // Use of formula derived
            sum = ((no_of_terms) * ((first + last))) / 2;
        }
 
        // left is to the left of mid and
        // right is to the right of mid
        else {
            // Take two sums i.e left and
            // right differently and add
            int sumleft = 0, sumright = 0;
 
            // first and last element
            int first_term1 = (2 * l - 1);
            int last_term1 = (2 * (n / 2) - 1);
 
            // total terms
            int no_of_terms1 = n / 2 - l + 1;
 
            // no of terms
            sumleft = ((no_of_terms1) * ((first_term1 + last_term1))) / 2;
 
            // The first even number is 2
            int first_term2 = 2;
 
            // The last element is given by 2*(r-n/2)
            int last_term2 = (2 * (r - n / 2));
            int no_of_terms2 = r - mid;
 
            // formula applied
            sumright = ((no_of_terms2) * ((first_term2 + last_term2))) / 2;
            sum = (sumleft + sumright);
        }
 
        return sum;
    }
 
    // Function to calculate the sum if n is odd
    static int sumodd(int n, int l, int r)
    {
 
        // take ceil value if n is odd
        int mid = n / 2 + 1;
        int sum = 0;
 
        // // both l and r are to the left of mid
        if (r <= mid) {
            // first and last element
            int first = (2 * l - 1);
            int last = (2 * r - 1);
 
            // number of terms
            int no_of_terms = r - l + 1;
 
            // formula
            sum = ((no_of_terms) * ((first + last))) / 2;
        }
 
        // // both l and r are to the right of mid
        else if (l > mid) {
 
            // firts and last ter,
            int first = (2 * (l - mid));
            int last = (2 * (r - mid));
 
            // no of terms
            int no_of_terms = r - l + 1;
 
            // formula used
            sum = ((no_of_terms) * ((first + last))) / 2;
        }
 
        // If l is on left and r on right
        else {
 
            // calculate separate sums
            int sumleft = 0, sumright = 0;
 
            // first half
            int first_term1 = (2 * l - 1);
            int last_term1 = (2 * mid - 1);
 
            // calculate terms
            int no_of_terms1 = mid - l + 1;
            sumleft = ((no_of_terms1) * ((first_term1 + last_term1))) / 2;
 
            // second half
            int first_term2 = 2;
            int last_term2 = (2 * (r - mid));
            int no_of_terms2 = r - mid;
            sumright = ((no_of_terms2) * ((first_term2 + last_term2))) / 2;
 
            // add both halves
            sum = (sumleft + sumright);
        }
 
        return sum;
    }
    // Function to find the sum between L and R
    static int rangesum(int n, int l, int r)
    {
        int sum = 0;
 
        // If n is even
        if (n % 2 == 0)
            return sumeven(n, l, r);
 
        // If n is odd
        else
            return sumodd(n, l, r);
    }
 
    // Driver Code
    public static void main(String[] args)
    {
        int n = 12;
        int l = 1, r = 11;
        System.out.println(rangesum(n, l, r));
    }
}


Python3
# Python3 program to find
# the sum between L-R range
# by creating the array
# Naive Approach
 
# Function to calculate
# the sum if n is even
def sumeven(n, l, r):
 
    sum = 0
    mid = n // 2
 
    # Both l and r are
    # to the left of mid
    if (r <= mid):
     
        # First and last element
        first = (2 * l - 1)
        last = (2 * r - 1)
 
        # Total number of terms in
        # the sequence is r-l+1
        no_of_terms = r - l + 1
 
        # Use of formula derived
        sum = ((no_of_terms) *
              ((first + last))) // 2
    
    # Both l and r are to the right of mid
    elif (l >= mid):
     
        # First and last element
        first = (2 * (l - n // 2))
        last = (2 * (r - n // 2))
 
        no_of_terms = r - l + 1
 
        # Use of formula derived
        sum = ((no_of_terms) *
              ((first + last))) // 2
 
    # Left is to the left of mid and
    # right is to the right of mid
    else :
       
        # Take two sums i.e left and
        # right differently and add
        sumleft, sumright = 0, 0
 
        # First and last element
        first_term1 = (2 * l - 1)
        last_term1 = (2 * (n // 2) - 1)
 
        # total terms
        no_of_terms1 = n // 2 - l + 1
 
        # no of terms
        sumleft = (((no_of_terms1) *
                  ((first_term1 +
                    last_term1))) // 2)
 
        # The first even number is 2
        first_term2 = 2
 
        # The last element is
        #
        last_term2 = (2 * (r - n // 2))
        no_of_terms2 = r - mid
 
        # formula applied
        sumright = (((no_of_terms2) *
                   ((first_term2 +
                     last_term2))) // 2)
        sum = (sumleft + sumright);
   
    return sum
 
# Function to calculate
# the sum if n is odd
def sumodd(n, l, r):
   
    # Take ceil value if n is odd
    mid = n // 2 + 1;
    sum = 0
 
    # Both l and r are to
    # the left of mid
    if (r <= mid):
     
        # First and last element
        first = (2 * l - 1)
        last = (2 * r - 1)
 
        # number of terms
        no_of_terms = r - l + 1
 
        # formula
        sum = (((no_of_terms) *
              ((first + last))) // 2)
    
    # both l and r are to the right of mid
    elif (l > mid):
     
        # firts and last ter,
        first = (2 * (l - mid))
        last = (2 * (r - mid))
 
        # no of terms
        no_of_terms = r - l + 1
 
        # formula used
        sum = (((no_of_terms) *
              ((first + last))) // 2)
     
    # If l is on left and r on right
    else :
 
        # calculate separate sums
        sumleft, sumright = 0, 0
 
        # first half
        first_term1 = (2 * l - 1)
        last_term1 = (2 * mid - 1)
 
        # calculate terms
        no_of_terms1 = mid - l + 1
        sumleft = (((no_of_terms1) *
                  ((first_term1 +
                    last_term1))) // 2)
 
        # second half
        first_term2 = 2
        last_term2 = (2 * (r - mid))
        no_of_terms2 = r - mid
        sumright = (((no_of_terms2) *
                   ((first_term2 +
                     last_term2))) // 2)
 
        # add both halves
        sum = (sumleft + sumright)
     
    return sum
 
# Function to find the
# sum between L and R
def rangesum(n, l, r):
 
    sum = 0
 
    # If n is even
    if (n % 2 == 0):
        return sumeven(n, l, r);
 
    # If n is odd
    else:
        return sumodd(n, l, r)
 
# Driver Code
if __name__ == "__main__":
    n = 12
    l = 1
    r = 11;
    print (rangesum(n, l, r))
 
# This code is contributed by Chitranayal


C#
// C# program to find the sum
// between L-R range by creating
// the array Efficient Approach
using System;
 
class GFG
{
 
    // Function to calculate
    // the sum if n is even
    static int sumeven(int n,
                       int l, int r)
    {
        int sum = 0;
        int mid = n / 2;
 
        // both l and r are
        // to the left of mid
        if (r <= mid)
        {
            // first and last element
            int first = (2 * l - 1);
            int last = (2 * r - 1);
 
            // Total number of terms in
            // the sequence is r-l+1
            int no_of_terms = r - l + 1;
 
            // use of formula derived
            sum = ((no_of_terms) *
                  ((first + last))) / 2;
        }
         
        // both l and r are
        // to the right of mid
        else if (l >= mid)
        {
            // first and last element
            int first = (2 * (l - n / 2));
            int last = (2 * (r - n / 2));
 
            int no_of_terms = r - l + 1;
 
            // Use of formula derived
            sum = ((no_of_terms) *
                  ((first + last))) / 2;
        }
 
        // left is to the left of
        // mid and right is to the
        // right of mid
        else
        {
            // Take two sums i.e left and
            // right differently and add
            int sumleft = 0, sumright = 0;
 
            // first and last element
            int first_term1 = (2 * l - 1);
            int last_term1 = (2 * (n / 2) - 1);
 
            // total terms
            int no_of_terms1 = n / 2 - l + 1;
 
            // no of terms
            sumleft = ((no_of_terms1) *
                      ((first_term1 +
                        last_term1))) / 2;
 
            // The first even
            // number is 2
            int first_term2 = 2;
 
            // The last element is
            // given by 2*(r-n/2)
            int last_term2 = (2 * (r - n / 2));
            int no_of_terms2 = r - mid;
 
            // formula applied
            sumright = ((no_of_terms2) *
                       ((first_term2 +
                         last_term2))) / 2;
            sum = (sumleft + sumright);
        }
 
        return sum;
    }
 
    // Function to calculate
    // the sum if n is odd
    static int sumodd(int n,
                      int l, int r)
    {
 
        // take ceil value
        // if n is odd
        int mid = n / 2 + 1;
        int sum = 0;
 
        // both l and r are
        // to the left of mid
        if (r <= mid)
        {
            // first and last element
            int first = (2 * l - 1);
            int last = (2 * r - 1);
 
            // number of terms
            int no_of_terms = r - l + 1;
 
            // formula
            sum = ((no_of_terms) *
                  ((first + last))) / 2;
        }
 
        // both l and r are
        // to the right of mid
        else if (l > mid)
        {
 
            // firts and last ter,
            int first = (2 * (l - mid));
            int last = (2 * (r - mid));
 
            // no of terms
            int no_of_terms = r - l + 1;
 
            // formula used
            sum = ((no_of_terms) *
                  ((first + last))) / 2;
        }
 
        // If l is on left
        // and r on right
        else
        {
 
            // calculate separate sums
            int sumleft = 0, sumright = 0;
 
            // first half
            int first_term1 = (2 * l - 1);
            int last_term1 = (2 * mid - 1);
 
            // calculate terms
            int no_of_terms1 = mid - l + 1;
            sumleft = ((no_of_terms1) *
                      ((first_term1 +
                        last_term1))) / 2;
 
            // second half
            int first_term2 = 2;
            int last_term2 = (2 * (r - mid));
            int no_of_terms2 = r - mid;
            sumright = ((no_of_terms2) *
                       ((first_term2 +
                         last_term2))) / 2;
 
            // add both halves
            sum = (sumleft + sumright);
        }
 
        return sum;
    }
     
    // Function to find the
    // sum between L and R
    static int rangesum(int n,
                        int l, int r)
    {
         
        // If n is even
        if (n % 2 == 0)
            return sumeven(n, l, r);
 
        // If n is odd
        else
            return sumodd(n, l, r);
    }
 
    // Driver Code
    public static void Main()
    {
        int n = 12;
        int l = 1, r = 11;
        Console.WriteLine(rangesum(n, l, r));
    }
}
 
// This code is contributed
// by chandan_jnu.


输出:
66

时间复杂度: O(N)
辅助空间: O(N)
高效的方法:无需构建数组即可解决O(1)复杂性的问题。考虑这样形成的序列的中间点。那么L和R可能有3种可能性。

  • l和r都在中间点的右侧。
  • l和r都在中间点的左侧。
  • l在中间点的左侧,r在中间点的左侧。

对于第一种情况和第二种情况,只需找到与从起始位置或中间位置开始的第l个位置相对应的数字和与从起始位置或中间位置开始的第r个位置相对应的数字即可。以下公式可用于求和:

sum = (no. of terms in the range)*(first term + last term)/2

对于第三种情况,将其视为[L-mid]和[mid-R],然后应用上述情况1和情况2的公式。
下面是上述方法的实现:

C++

// C++ program to find the sum between L-R
// range by creating the array
// Naive Approach
#include
using namespace std;
 
// // Function to calculate the sum if n is even
int sumeven(int n, int l, int r)
{
    int sum = 0;
    int mid = n / 2;
 
    // both l and r are to the left of mid
    if (r <= mid)
    {
        // first and last element
        int first = (2 * l - 1);
        int last = (2 * r - 1);
 
        // Total number of terms in
        // the sequence is r-l+1
        int no_of_terms = r - l + 1;
 
        // use of formula derived
        sum = ((no_of_terms) * ((first + last))) / 2;
    }
     
    // both l and r are to the right of mid
    else if (l >= mid)
    {
        // first and last element
        int first = (2 * (l - n / 2));
        int last = (2 * (r - n / 2));
 
        int no_of_terms = r - l + 1;
 
        // Use of formula derived
        sum = ((no_of_terms) * ((first + last))) / 2;
    }
 
    // left is to the left of mid and
    // right is to the right of mid
    else
    {
         
        // Take two sums i.e left and
        // right differently and add
        int sumleft = 0, sumright = 0;
 
        // first and last element
        int first_term1 = (2 * l - 1);
        int last_term1 = (2 * (n / 2) - 1);
 
        // total terms
        int no_of_terms1 = n / 2 - l + 1;
 
        // no of terms
        sumleft = ((no_of_terms1) *
                  ((first_term1 + last_term1))) / 2;
 
        // The first even number is 2
        int first_term2 = 2;
 
        // The last element is given by 2*(r-n/2)
        int last_term2 = (2 * (r - n / 2));
        int no_of_terms2 = r - mid;
 
        // formula applied
        sumright = ((no_of_terms2) *
                   ((first_term2 + last_term2))) / 2;
        sum = (sumleft + sumright);
    }
    return sum;
}
 
// Function to calculate the sum if n is odd
int sumodd(int n, int l, int r)
{
 
    // take ceil value if n is odd
    int mid = n / 2 + 1;
    int sum = 0;
 
    // // both l and r are to the left of mid
    if (r <= mid)
    {
        // first and last element
        int first = (2 * l - 1);
        int last = (2 * r - 1);
 
        // number of terms
        int no_of_terms = r - l + 1;
 
        // formula
        sum = ((no_of_terms) *
             ((first + last))) / 2;
    }
 
    // // both l and r are to the right of mid
    else if (l > mid)
    {
 
        // firts and last ter,
        int first = (2 * (l - mid));
        int last = (2 * (r - mid));
 
        // no of terms
        int no_of_terms = r - l + 1;
 
        // formula used
        sum = ((no_of_terms) *
              ((first + last))) / 2;
    }
 
    // If l is on left and r on right
    else
    {
 
        // calculate separate sums
        int sumleft = 0, sumright = 0;
 
        // first half
        int first_term1 = (2 * l - 1);
        int last_term1 = (2 * mid - 1);
 
        // calculate terms
        int no_of_terms1 = mid - l + 1;
        sumleft = ((no_of_terms1) *
                  ((first_term1 + last_term1))) / 2;
 
        // second half
        int first_term2 = 2;
        int last_term2 = (2 * (r - mid));
        int no_of_terms2 = r - mid;
        sumright = ((no_of_terms2) *
                   ((first_term2 + last_term2))) / 2;
 
        // add both halves
        sum = (sumleft + sumright);
    }
    return sum;
}
 
// Function to find the sum between L and R
int rangesum(int n, int l, int r)
{
    int sum = 0;
 
    // If n is even
    if (n % 2 == 0)
        return sumeven(n, l, r);
 
    // If n is odd
    else
        return sumodd(n, l, r);
}
 
// Driver Code
int main()
{
    int n = 12;
    int l = 1, r = 11;
    cout << (rangesum(n, l, r));
}
 
// This code is contributed by 29AjayKumar

Java

// Java program to find the sum between L-R
// range by creating the array
// Efficient Approach
import java.io.*;
public class GFG {
 
    // // Function to calculate the sum if n is even
    static int sumeven(int n, int l, int r)
    {
        int sum = 0;
        int mid = n / 2;
 
        // both l and r are to the left of mid
        if (r <= mid) {
            // first and last element
            int first = (2 * l - 1);
            int last = (2 * r - 1);
 
            // Total number of terms in
            // the sequence is r-l+1
            int no_of_terms = r - l + 1;
 
            // use of formula derived
            sum = ((no_of_terms) * ((first + last))) / 2;
        }
        // both l and r are to the right of mid
        else if (l >= mid) {
            // // first and last element
            int first = (2 * (l - n / 2));
            int last = (2 * (r - n / 2));
 
            int no_of_terms = r - l + 1;
 
            // Use of formula derived
            sum = ((no_of_terms) * ((first + last))) / 2;
        }
 
        // left is to the left of mid and
        // right is to the right of mid
        else {
            // Take two sums i.e left and
            // right differently and add
            int sumleft = 0, sumright = 0;
 
            // first and last element
            int first_term1 = (2 * l - 1);
            int last_term1 = (2 * (n / 2) - 1);
 
            // total terms
            int no_of_terms1 = n / 2 - l + 1;
 
            // no of terms
            sumleft = ((no_of_terms1) * ((first_term1 + last_term1))) / 2;
 
            // The first even number is 2
            int first_term2 = 2;
 
            // The last element is given by 2*(r-n/2)
            int last_term2 = (2 * (r - n / 2));
            int no_of_terms2 = r - mid;
 
            // formula applied
            sumright = ((no_of_terms2) * ((first_term2 + last_term2))) / 2;
            sum = (sumleft + sumright);
        }
 
        return sum;
    }
 
    // Function to calculate the sum if n is odd
    static int sumodd(int n, int l, int r)
    {
 
        // take ceil value if n is odd
        int mid = n / 2 + 1;
        int sum = 0;
 
        // // both l and r are to the left of mid
        if (r <= mid) {
            // first and last element
            int first = (2 * l - 1);
            int last = (2 * r - 1);
 
            // number of terms
            int no_of_terms = r - l + 1;
 
            // formula
            sum = ((no_of_terms) * ((first + last))) / 2;
        }
 
        // // both l and r are to the right of mid
        else if (l > mid) {
 
            // firts and last ter,
            int first = (2 * (l - mid));
            int last = (2 * (r - mid));
 
            // no of terms
            int no_of_terms = r - l + 1;
 
            // formula used
            sum = ((no_of_terms) * ((first + last))) / 2;
        }
 
        // If l is on left and r on right
        else {
 
            // calculate separate sums
            int sumleft = 0, sumright = 0;
 
            // first half
            int first_term1 = (2 * l - 1);
            int last_term1 = (2 * mid - 1);
 
            // calculate terms
            int no_of_terms1 = mid - l + 1;
            sumleft = ((no_of_terms1) * ((first_term1 + last_term1))) / 2;
 
            // second half
            int first_term2 = 2;
            int last_term2 = (2 * (r - mid));
            int no_of_terms2 = r - mid;
            sumright = ((no_of_terms2) * ((first_term2 + last_term2))) / 2;
 
            // add both halves
            sum = (sumleft + sumright);
        }
 
        return sum;
    }
    // Function to find the sum between L and R
    static int rangesum(int n, int l, int r)
    {
        int sum = 0;
 
        // If n is even
        if (n % 2 == 0)
            return sumeven(n, l, r);
 
        // If n is odd
        else
            return sumodd(n, l, r);
    }
 
    // Driver Code
    public static void main(String[] args)
    {
        int n = 12;
        int l = 1, r = 11;
        System.out.println(rangesum(n, l, r));
    }
}

Python3

# Python3 program to find
# the sum between L-R range
# by creating the array
# Naive Approach
 
# Function to calculate
# the sum if n is even
def sumeven(n, l, r):
 
    sum = 0
    mid = n // 2
 
    # Both l and r are
    # to the left of mid
    if (r <= mid):
     
        # First and last element
        first = (2 * l - 1)
        last = (2 * r - 1)
 
        # Total number of terms in
        # the sequence is r-l+1
        no_of_terms = r - l + 1
 
        # Use of formula derived
        sum = ((no_of_terms) *
              ((first + last))) // 2
    
    # Both l and r are to the right of mid
    elif (l >= mid):
     
        # First and last element
        first = (2 * (l - n // 2))
        last = (2 * (r - n // 2))
 
        no_of_terms = r - l + 1
 
        # Use of formula derived
        sum = ((no_of_terms) *
              ((first + last))) // 2
 
    # Left is to the left of mid and
    # right is to the right of mid
    else :
       
        # Take two sums i.e left and
        # right differently and add
        sumleft, sumright = 0, 0
 
        # First and last element
        first_term1 = (2 * l - 1)
        last_term1 = (2 * (n // 2) - 1)
 
        # total terms
        no_of_terms1 = n // 2 - l + 1
 
        # no of terms
        sumleft = (((no_of_terms1) *
                  ((first_term1 +
                    last_term1))) // 2)
 
        # The first even number is 2
        first_term2 = 2
 
        # The last element is
        #
        last_term2 = (2 * (r - n // 2))
        no_of_terms2 = r - mid
 
        # formula applied
        sumright = (((no_of_terms2) *
                   ((first_term2 +
                     last_term2))) // 2)
        sum = (sumleft + sumright);
   
    return sum
 
# Function to calculate
# the sum if n is odd
def sumodd(n, l, r):
   
    # Take ceil value if n is odd
    mid = n // 2 + 1;
    sum = 0
 
    # Both l and r are to
    # the left of mid
    if (r <= mid):
     
        # First and last element
        first = (2 * l - 1)
        last = (2 * r - 1)
 
        # number of terms
        no_of_terms = r - l + 1
 
        # formula
        sum = (((no_of_terms) *
              ((first + last))) // 2)
    
    # both l and r are to the right of mid
    elif (l > mid):
     
        # firts and last ter,
        first = (2 * (l - mid))
        last = (2 * (r - mid))
 
        # no of terms
        no_of_terms = r - l + 1
 
        # formula used
        sum = (((no_of_terms) *
              ((first + last))) // 2)
     
    # If l is on left and r on right
    else :
 
        # calculate separate sums
        sumleft, sumright = 0, 0
 
        # first half
        first_term1 = (2 * l - 1)
        last_term1 = (2 * mid - 1)
 
        # calculate terms
        no_of_terms1 = mid - l + 1
        sumleft = (((no_of_terms1) *
                  ((first_term1 +
                    last_term1))) // 2)
 
        # second half
        first_term2 = 2
        last_term2 = (2 * (r - mid))
        no_of_terms2 = r - mid
        sumright = (((no_of_terms2) *
                   ((first_term2 +
                     last_term2))) // 2)
 
        # add both halves
        sum = (sumleft + sumright)
     
    return sum
 
# Function to find the
# sum between L and R
def rangesum(n, l, r):
 
    sum = 0
 
    # If n is even
    if (n % 2 == 0):
        return sumeven(n, l, r);
 
    # If n is odd
    else:
        return sumodd(n, l, r)
 
# Driver Code
if __name__ == "__main__":
    n = 12
    l = 1
    r = 11;
    print (rangesum(n, l, r))
 
# This code is contributed by Chitranayal

C#

// C# program to find the sum
// between L-R range by creating
// the array Efficient Approach
using System;
 
class GFG
{
 
    // Function to calculate
    // the sum if n is even
    static int sumeven(int n,
                       int l, int r)
    {
        int sum = 0;
        int mid = n / 2;
 
        // both l and r are
        // to the left of mid
        if (r <= mid)
        {
            // first and last element
            int first = (2 * l - 1);
            int last = (2 * r - 1);
 
            // Total number of terms in
            // the sequence is r-l+1
            int no_of_terms = r - l + 1;
 
            // use of formula derived
            sum = ((no_of_terms) *
                  ((first + last))) / 2;
        }
         
        // both l and r are
        // to the right of mid
        else if (l >= mid)
        {
            // first and last element
            int first = (2 * (l - n / 2));
            int last = (2 * (r - n / 2));
 
            int no_of_terms = r - l + 1;
 
            // Use of formula derived
            sum = ((no_of_terms) *
                  ((first + last))) / 2;
        }
 
        // left is to the left of
        // mid and right is to the
        // right of mid
        else
        {
            // Take two sums i.e left and
            // right differently and add
            int sumleft = 0, sumright = 0;
 
            // first and last element
            int first_term1 = (2 * l - 1);
            int last_term1 = (2 * (n / 2) - 1);
 
            // total terms
            int no_of_terms1 = n / 2 - l + 1;
 
            // no of terms
            sumleft = ((no_of_terms1) *
                      ((first_term1 +
                        last_term1))) / 2;
 
            // The first even
            // number is 2
            int first_term2 = 2;
 
            // The last element is
            // given by 2*(r-n/2)
            int last_term2 = (2 * (r - n / 2));
            int no_of_terms2 = r - mid;
 
            // formula applied
            sumright = ((no_of_terms2) *
                       ((first_term2 +
                         last_term2))) / 2;
            sum = (sumleft + sumright);
        }
 
        return sum;
    }
 
    // Function to calculate
    // the sum if n is odd
    static int sumodd(int n,
                      int l, int r)
    {
 
        // take ceil value
        // if n is odd
        int mid = n / 2 + 1;
        int sum = 0;
 
        // both l and r are
        // to the left of mid
        if (r <= mid)
        {
            // first and last element
            int first = (2 * l - 1);
            int last = (2 * r - 1);
 
            // number of terms
            int no_of_terms = r - l + 1;
 
            // formula
            sum = ((no_of_terms) *
                  ((first + last))) / 2;
        }
 
        // both l and r are
        // to the right of mid
        else if (l > mid)
        {
 
            // firts and last ter,
            int first = (2 * (l - mid));
            int last = (2 * (r - mid));
 
            // no of terms
            int no_of_terms = r - l + 1;
 
            // formula used
            sum = ((no_of_terms) *
                  ((first + last))) / 2;
        }
 
        // If l is on left
        // and r on right
        else
        {
 
            // calculate separate sums
            int sumleft = 0, sumright = 0;
 
            // first half
            int first_term1 = (2 * l - 1);
            int last_term1 = (2 * mid - 1);
 
            // calculate terms
            int no_of_terms1 = mid - l + 1;
            sumleft = ((no_of_terms1) *
                      ((first_term1 +
                        last_term1))) / 2;
 
            // second half
            int first_term2 = 2;
            int last_term2 = (2 * (r - mid));
            int no_of_terms2 = r - mid;
            sumright = ((no_of_terms2) *
                       ((first_term2 +
                         last_term2))) / 2;
 
            // add both halves
            sum = (sumleft + sumright);
        }
 
        return sum;
    }
     
    // Function to find the
    // sum between L and R
    static int rangesum(int n,
                        int l, int r)
    {
         
        // If n is even
        if (n % 2 == 0)
            return sumeven(n, l, r);
 
        // If n is odd
        else
            return sumodd(n, l, r);
    }
 
    // Driver Code
    public static void Main()
    {
        int n = 12;
        int l = 1, r = 11;
        Console.WriteLine(rangesum(n, l, r));
    }
}
 
// This code is contributed
// by chandan_jnu.
输出:
66

时间复杂度: O(1)
辅助空间: O(1)