Sum of array elements excluding the elements which lie between a and b

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.

Examples:

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:

C++

filter_none

edit
close

play_arrow

link
brightness_4
code

// CPP implementation of above approach
#include<bits/stdc++.h>
using namespace std;
  
// Function to find sum 
// of array exlcuding the 
// range which has [a, b] 
void sumexcludingrange(vector<int>li, int a, int b)
{
    int sum = 0;
    bool add = true;
      
    // loop in li
    int n = li.size();
    for (int i = 0;i < n; i++)
    {
          
        // if no != a then add
        if (li[i] != a && 
            add == true)
            sum = sum + li[i];
              
        // mark when a 
        // and b are found 
        else if (li[i] == a)
            add = false;
        else if( li[i] == b)
            add = true;
    }
      
    // print sum
cout<<(sum);
}
  
// Driver Code
int main()
{
    vector<int>lis{1, 2, 4, 5, 6};
    int a = 2;
    int b = 5;
      
    sumexcludingrange(lis, a, b);
}
// This code is contributed by
// Sahil_Shelangia

chevron_right


Java

filter_none

edit
close

play_arrow

link
brightness_4
code

// Java implementation of above approach
  
// Function to find sum 
// of array exlcuding the 
// range which has [a, b] 
import java .io.*;
  
class GFG
{
static void sumexcludingrange(int li[],
                              int a, int b)
{
    int sum = 0;
    boolean add = true;
      
    // loop in li
    for (int i = 0
             i < li.length; i++)
    {
          
        // if no != a then add
        if (li[i] != a && 
             add == true)
            sum = sum + li[i];
              
        // mark when a 
        // and b are found 
        else if (li[i] == a)
            add = false;
        else if( li[i] == b)
            add = true;
    }
      
    // print sum
    System.out.print(sum);
}
  
// Driver Code
public static void main(String[] args)
{
    int lis[] = {1, 2, 4, 5, 6};
    int a = 2;
    int b = 5;
      
    sumexcludingrange(lis, a, b);
}
}
  
// This code is contributed
// by anuj_67.

chevron_right


Python3

filter_none

edit
close

play_arrow

link
brightness_4
code

# Python3 implementation of above approach
  
# Function to find sum 
# of array exlcuding the 
# range which has [a, b] 
def sumexcludingrange(li, a, b):
      
    sum = 0 
    add = True
      
    # loop in li
    for no in li:
          
         # if no != a then add
        if no != a and add == True:
            sum = sum + no
              
        # mark when a and b are found 
        elif no == a:
            add = False
        elif no == b:
            add = True
              
    # print sum
    print (sum)
      
      
  
lis = [1, 2, 4, 5, 6]
a = 2
b = 5
  
sumexcludingrange(lis, a, b)

chevron_right


C#

filter_none

edit
close

play_arrow

link
brightness_4
code

// C# implementation of above approach
  
// Function to find sum 
// of array exlcuding the 
// range which has [a, b] 
  
using System;
   
class GFG
{
static void sumexcludingrange(int[] li,
                              int a, int b)
{
    int sum = 0;
    bool add = true;
       
    // loop in li
    for (int i = 0; 
             i < li.Length; i++)
    {
           
        // if no != a then add
        if (li[i] != a && 
             add == true)
            sum = sum + li[i];
               
        // mark when a 
        // and b are found 
        else if (li[i] == a)
            add = false;
        else if( li[i] == b)
            add = true;
    }
       
    // print sum
    Console.Write(sum);
}
   
// Driver Code
public static void Main()
{
    int[] lis = {1, 2, 4, 5, 6};
    int a = 2;
    int b = 5;
       
    sumexcludingrange(lis, a, b);
}
}

chevron_right


Output:

7


My Personal Notes arrow_drop_up

Check out this Author's contributed articles.

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. 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.



Improved By : vt_m, Ita_c, sahilshelangia



Article Tags :
Practice Tags :


Be the First to upvote.


Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.