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!