Dada una array arr[] de tamaño N y un número entero K , la tarea es encontrar si la suma de los elementos de la array es menor o igual que K o no.
Ejemplos :
Entrada : arr[] = {1, 2, 8}, K = 5
Salida : falso
Explicación : la suma de la array es 11, que es mayor que 5Entrada : arr[] = {2}, K = 5
Salida : verdadero
Enfoque : el problema se puede resolver encontrando la suma de la array y verificando si la suma obtenida es menor o igual a K o no.
A continuación se muestra la implementación del enfoque anterior:
C++
// C++ program for the above approach #include <bits/stdc++.h> using namespace std; // Function to check if sum of elements // is less than or equal to K or not bool check(int arr[], int N, int K) { // Stores the sum int sum = 0; for (int i = 0; i < N; i++) { sum += arr[i]; } return sum <= K; } // Driver Code int main() { int arr[3] = { 1, 2, 8 }; int N = sizeof(arr) / sizeof(arr[0]); int K = 5; if (check(arr, N, K)) cout << "true"; else cout << "false"; return 0; }
Java
// Java program for the above approach class GFG { // Function to check if sum of elements // is less than or equal to K or not static boolean check(int[] arr, int N, int K) { // Stores the sum int sum = 0; for (int i = 0; i < N; i++) { sum += arr[i]; } return sum <= K; } // Driver Code public static void main(String args[]) { int[] arr = { 1, 2, 8 }; int N = arr.length; int K = 5; if (check(arr, N, K)) System.out.println("true"); else System.out.println("false"); } } // This code is contributed by Saurabh Jaiswal
Python3
# Python code for the above approach # Function to check if sum of elements # is less than or equal to K or not def check(arr, N, K): # Stores the sum sum = 0; for i in range(N): sum += arr[i]; return sum <= K; # Driver Code arr = [1, 2, 8]; N = len(arr) K = 5 if (check(arr, N, K)): print("true"); else: print("false"); # This code is contributed by gfgking
C#
// C# program for the above approach using System; class GFG { // Function to check if sum of elements // is less than or equal to K or not static bool check(int []arr, int N, int K) { // Stores the sum int sum = 0; for (int i = 0; i < N; i++) { sum += arr[i]; } return sum <= K; } // Driver Code public static void Main() { int []arr = { 1, 2, 8 }; int N = arr.Length; int K = 5; if (check(arr, N, K)) Console.Write("true"); else Console.Write("false"); } } // This code is contributed by Samim Hossain Mondal.
Javascript
<script> // JavaScript code for the above approach // Function to check if sum of elements // is less than or equal to K or not function check(arr, N, K) { // Stores the sum let sum = 0; for (let i = 0; i < N; i++) { sum += arr[i]; } return sum <= K; } // Driver Code let arr = [1, 2, 8]; let N = arr.length; let K = 5; if (check(arr, N, K)) document.write("true"); else document.write("false"); // This code is contributed by Potta Lokesh </script>
Producción
false
Complejidad de Tiempo : O(N)
Espacio Auxiliar : O(1)
Publicación traducida automáticamente
Artículo escrito por mayank007rawa y traducido por Barcelona Geeks. The original can be accessed here. Licence: CCBY-SA