Given a number N, create an array such the first half of the array is filled with odd numbers till N, and the second half of the array is filled with even numbers. Also given are L and R indices, the task is to print the sum of elements in the array in the range [L, R].Â
Examples:Â
Input: N = 12, L = 1, R = 11Â
Output: 66Â
The array formed thus is {1, 3, 5, 7, 9, 11, 2, 4, 6, 8, 10, 12}Â
The sum between index 1 and index 11 is 66 {1+3+5+7+9+11+2+4+6+8+10}Input: N = 11, L = 3 and R = 7Â
Output: 34Â
The array formed is {1, 3, 5, 7, 9, 11, 2, 4, 6, 8, 10}Â
The sum between index 3 and index 7 is 34 {5+7+9+11+2}
A naive approach will be to form the array of size N and iterate from L to R and return the sum. Â
C++
// C++ program to find the sum between L-R // range by creating the array // Naive Approach #include<bits/stdc++.h> 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
<?php // PHP program to find the sum between // L-R range by creating the array // Naive Approach Â
// Function to find the sum between L and R function rangesum( $n , $l , $r ) {     // array created     $arr = array_fill (0, $n , 0); Â
    // fill the first half of array     $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;     }     $sum = 0; Â
    // find the sum between range     for ( $i = $l - 1; $i < $r ; $i ++)     {         $sum += $arr [ $i ];     } Â
    return $sum ; } Â
// Driver Code $n = 12; $l = 1; $r = 11; echo (rangesum( $n , $l , $r )); Â Â Â Â Â // This code is contributed by mits ?> |
Javascript
<script> Â
// Javascript program to find the sum between L-R // range by creating the array // Naive Approach Â
    // Function to find the sum between L and R     function rangesum(n, l, r)     {         // array created         let arr = new Array(n); Â
        // fill the first half of array         let 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;         }         let sum = 0; Â
        // find the sum between range         for (i = l - 1; i < r; i++) {             sum += arr[i];         } Â
        return sum;     } Â
    // Driver Code     let n = 12;     let l = 1, r = 11;     document.write(rangesum(n, l, r));          // This code is contributed by rishavmahato348. </script> |
66
Â
Time complexity: O(N)Â
Auxiliary Space: O(N)
Efficient Approach: Without building the array the problem can be solved in O(1) complexity. Consider the middle point of the sequence thus formed. Then there can be 3 possibilities for L and R. Â
- l and r both are on the right of the middle point.
- l and r both are on the left of the middle point.
- l is on the left of the middle point and r is on the left of the middle point.
For the 1st and 2nd case, just find the number which corresponds to the l-th position from the start or middle and the number which corresponds to the r-th position from start or middle. The below formula can be used to find out the sum:Â Â
sum = (no. of terms in the range)*(first term + last term)/2
For the third case, consider it as [L-mid] and [mid-R] and then apply case 1 and case 2 formulae as mentioned above.
Below is the implementation of the above approach:Â Â
C++
// C++ program to find the sum between L-R // range by creating the array // Naive Approach #include<bits/stdc++.h> 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)     { Â
        // first and last term,         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) { Â
            // first and last term,             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):              # first and last term,         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)         { Â
            // first and last term,             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. |
Javascript
<script> // Javascript program to find the sum between L-R // range by creating the array // Efficient Approach           // Function to calculate the sum if n is even     function sumeven(n,l,r)     {         let sum = 0;         let mid = Math.floor(n / 2);           // both l and r are to the left of mid         if (r <= mid) {             // first and last element             let first = (2 * l - 1);             let last = (2 * r - 1);               // Total number of terms in             // the sequence is r-l+1             let 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             let first = (2 * (l - n / 2));             let last = (2 * (r - n / 2));               let 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             let sumleft = 0, sumright = 0;               // first and last element             let first_term1 = (2 * l - 1);             let last_term1 = (2 * (n / 2) - 1);               // total terms             let 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             let first_term2 = 2;               // The last element is given by 2*(r-n/2)             let last_term2 = (2 * (r - n / 2));             let 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     function sumodd(n,l,r)     {         // take ceil value if n is odd         let mid = Math.floor(n / 2) + 1;         let sum = 0;           // // both l and r are to the left of mid         if (r <= mid) {             // first and last element             let first = (2 * l - 1);             let last = (2 * r - 1);               // number of terms             let 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) {               // first and last term ,             let first = (2 * (l - mid));             let last = (2 * (r - mid));               // no of terms             let 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             let sumleft = 0, sumright = 0;               // first half             let first_term1 = (2 * l - 1);             let last_term1 = (2 * mid - 1);               // calculate terms             let no_of_terms1 = mid - l + 1;             sumleft = ((no_of_terms1) * ((first_term1 + last_term1))) / 2;               // second half             let first_term2 = 2;             let last_term2 = (2 * (r - mid));             let 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     function rangesum(n,l,r)     {         let 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     let n = 12;     let l = 1, r = 11;     document.write(rangesum(n, l, r));      Â
Â
// This code is contributed by rag2127 </script> |
66
Â
Time complexity: O(1)Â
Auxiliary Space: O(1)
Ready to dive in? Explore our Free Demo Content and join our DSA course, trusted by over 100,000 neveropen!