Given an array of N unique numbers. Also given two numbers a and b such that a will always be before b in the array. The task is to find the sum of the array elements excluding the elements which lie between a and b.
Input : arr = [2, 1, 6, 9, 11], a = 6, b = 9
Output : 14
Input : arr = [1, 2, 4, 5, 6], a = 2, b = 5
Output : 7
Approach: Traverse in the array, keep adding the array elements until we find a. Continue iteration until b is found and do not add array elements to the sum. After b is found, add array elements to the sum till the end. Once the iteration is completed, return the sum.
Below is the implementation of the above approach:
- Maximum Subarray Sum Excluding Certain Elements
- Average of remaining elements after removing K largest and K smallest elements from array
- Sum of elements in 1st array such that number of elements less than or equal to them in 2nd array is maximum
- Elements to be added so that all elements of a range are present in array
- Find elements larger than half of the elements in an array
- Maximize the sum of X+Y elements by picking X and Y elements from 1st and 2nd array
- Count array elements that divide the sum of all other elements
- Find elements of array using XOR of consecutive elements
- Replace array elements by sum of next two consecutive elements
- Find all elements in array which have at-least two greater elements
- Generate an array of K elements such that sum of elements is N and the condition a[i] < a[i+1] <= 2*a[i] is met | Set 2
- Count number of elements between two given elements in array
- 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
- Rearrange array such that even index elements are smaller and odd index elements are greater
- Increment odd positioned elements by 1 and decrement even positioned elements by 1 in an Array
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.