Given two integers and and an array arr every element of which at index is calculated as arr[i] = i * (-1)i. The task is to find the sum of these elements of the array within the index range .
Input: L = 1 , R = 5
Sum = (-1) + 2 + (-3) + 4 + (-5) = -3
Input: L = 5 , R = 100000000
Naive Approach: According to the definition of array elements, each odd element of the array is negative and even element is positive. So, to find the sum run a for loop from (L to R) and maintain the sum of all the odd(negative) and even(positive) numbers. Finally, return the sum.
Efficient Approach: It can be noted that the sum of all the odd elements of this series will always be equal to (totalOdd)2 where totalOdd = total number of odd elements and sum of the even numbers will be totalEven * (totalEven + 1). Now, all we have to do is to find the sum of all the odd elements upto L and upto R. Store the difference of both to get the sum of all the odd elements between L and R. Do this same for even numbers and finally return the difference of even and odd sums.
Below is the implementation of the above approach:
- Rearrange array such that even index elements are smaller and odd index elements are greater
- Queries for number of distinct elements from a given index till last index in an array
- Construct an array from XOR of all elements of array except element at same index
- Find the index of the element in an array which divides most elements before it
- Count of index pairs with equal elements in an array
- Minimum index i such that all the elements from index i to given index are equal
- Number of permutations such that sum of elements at odd index and even index are equal
- Check if every index i has an index j such that sum of elements in both directions are equal
- Minimum elements to change so that for an index i all elements on the left are -ve and all elements on the right are +ve
- Index of the elements which are equal to the sum of all succeeding elements
- Count smaller elements on right side and greater elements on left side using Binary Index Tree
- Minimum cost to reach end of array array when a maximum jump of K index is allowed
- Longest alternating sub-array starting from every index in a Binary Array
- Find starting index for every occurrence of given array B in array A using Z-Algorithm
- Sum of elements till the smallest index such that there are no even numbers to its right
- Check if Array elements can be maximized upto M by adding all elements from another array
- Find an index such that difference between product of elements before and after it is minimum
- Maximum subsequence sum with adjacent elements having atleast K difference in index
- Count of elements having odd number of divisors in index range [L, R] for Q queries
- Queries for elements greater than K in the given index range using Segment Tree
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to email@example.com. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.