Dada una array de N números únicos. También dados dos números a y b tales que a siempre estará antes de b en la array. La tarea es encontrar la suma de los elementos del arreglo excluyendo los elementos que se encuentran entre a y b.
Ejemplos:
Entrada : arr = [2, 1, 6, 9, 11], a = 6, b = 9
Salida : 14Entrada : arr = [1, 2, 4, 5, 6], a = 2, b = 5
Salida : 7
Enfoque: Traverse en la array, seguir agregando los elementos de la array hasta que encontremos a. Continúe la iteración hasta que se encuentre b y no agregue elementos de array a la suma. Después de encontrar b, agregue elementos de array a la suma hasta el final. Una vez completada la iteración, devuelva la suma.
A continuación se muestra la implementación del enfoque anterior:
C++
// CPP implementation of above approach #include<bits/stdc++.h> using namespace std; // Function to find sum // of array excluding 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
Java
// Java implementation of above approach // Function to find sum // of array excluding 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.
Python3
# Python3 implementation of above approach # Function to find sum # of array excluding 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)
C#
// C# implementation of above approach // Function to find sum // of array excluding 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); } }
Javascript
<script> // JavaScript implementation of above approach // Function to find sum // of array excluding the // range which has [a, b] function sumexcludingrange(li, a, b) { let sum = 0; let add = true; // Loop in li for(let 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 document.write(sum); } // Driver Code let lis = [ 1, 2, 4, 5, 6 ]; let a = 2; let b = 5; sumexcludingrange(lis, a, b); // This code is contributed by sravan kumar </script>
7
Publicación traducida automáticamente
Artículo escrito por PranshuAgarwal1 y traducido por Barcelona Geeks. The original can be accessed here. Licence: CCBY-SA