Dada una array arr[] y el entero K , nuestra tarea es determinar si la suma de cada elemento de la array y K es mayor o igual que el elemento máximo que está presente en la array que es arr[i] + k > = maxElement de la array . Imprime el recuento total de todos esos elementos.
Ejemplos:
Entrada : arr = [2, 3, 5, 1, 3], k = 3
Salida : 4
Explicaciones :
En el arreglo dado los elementos 2, 3, 5, 3 cumplen la condición porque todos ellos al sumar 3( =K) produce un valor mayor que el elemento máximo de la array, que es 5.
Entrada: arr = [4, 2, 1, 1, 2], k = 1
Salida: 1
Explicaciones:
en la array dada, el elemento 4 cumplen la condición porque al sumar 4 con 1(=K) obtenemos un valor mayor que el elemento máximo del arreglo que es el mismo 4.
Enfoque:
para resolver el problema mencionado anteriormente, primero debemos almacenar ese elemento máximo que tiene la array. Luego, para cada elemento, verifique si la suma del elemento y K da un valor mayor que el elemento máximo, luego incremente el conteo; de lo contrario, vaya al siguiente elemento.
A continuación se muestra la implementación del enfoque anterior:
C++
// C++ implementation to Count of all the elements // in the array whose summation with integer K returns // a value that is greater than or equal to the // maximum value present in the array #include <bits/stdc++.h> using namespace std; // Function to count all the elements int countNum(int arr[], int K, int n) { int maxi = INT_MIN; // Store the maximum array element for (int i = 0; i < n; i++) { if (arr[i] > maxi) maxi = arr[i]; } int cnt = 0; // Iterate in array for (int i = 0; i < n; i++) { // Check if current element and k gives // a greater sum than max element if (arr[i] + K >= maxi) // Increment the count cnt++; else continue; } // Return the final result return cnt; } // Driver code int main() { int arr[] = { 4, 2, 1, 1, 2 }; int k = 1; int n = sizeof(arr) / sizeof(arr[0]); cout << countNum(arr, k, n) << endl; return 0; }
Java
// Java implementation to count of all the elements // in the array whose summation with integer K returns // a value that is greater than or equal to the // maximum value present in the array class GFG{ // Function to count all the elements public static int countNum(int arr[], int K, int n) { int maxi = 0; // Store the maximum array element for(int i = 0; i < n; i++) { if (arr[i] > maxi) maxi = arr[i]; } int cnt = 0; // Iterate in array for(int i = 0; i < n; i++) { // Check if current element and k gives // a greater sum than max element if (arr[i] + K >= maxi) // Increment the count cnt++; else continue; } // Return the final result return cnt; } // Driver code public static void main(String[] args) { int arr[] = { 4, 2, 1, 1, 2 }; int k = 1; int n = arr.length; System.out.println(countNum(arr, k, n)); } } // This code is contributed by divyeshrabadiya07
Python3
# Python3 implementation to Count of all the elements # in the array whose summation with integer K returns # a value that is greater than or equal to the # maximum value present in the array import sys # Function to count all the elements def countNum(arr, K, n): maxi = -sys.maxsize # Store the maximum array element for i in range(n) : if arr[i] > maxi: maxi = arr[i] cnt = 0 # Iterate in array for i in range(n): # Check if current element and k gives # a greater sum than max element if (arr[i] + K) >= maxi: # Increment the count cnt += 1 else : continue # Return the final result return cnt # Driver code if __name__=='__main__': arr = [ 4, 2, 1, 1, 2 ] k = 1 n = len(arr) print(countNum(arr, k, n)) # This code is contributed by rutvik_56
C#
// C# implementation to count of all // the elements in the array whose // summation with integer K returns // a value that is greater than or // equal to the maximum value present // in the array using System; class GFG{ // Function to count all the elements public static int countNum(int[] arr, int K, int n) { int maxi = 0; // Store the maximum array element for(int i = 0; i < n; i++) { if (arr[i] > maxi) maxi = arr[i]; } int cnt = 0; // Iterate in array for(int i = 0; i < n; i++) { // Check if current element and k // gives a greater sum than max // element if (arr[i] + K >= maxi) // Increment the count cnt++; else continue; } // Return the final result return cnt; } // Driver code public static void Main() { int[] arr = { 4, 2, 1, 1, 2 }; int k = 1; int n = arr.Length; Console.Write(countNum(arr, k, n)); } } // This code is contributed by chitranayal
Javascript
<script> // Javascript implementation to Count of all the elements // in the array whose summation with integer K returns // a value that is greater than or equal to the // maximum value present in the array // Function to count all the elements function countNum(arr, K, n) { var maxi = -1000000000; // Store the maximum array element for (var i = 0; i < n; i++) { if (arr[i] > maxi) maxi = arr[i]; } var cnt = 0; // Iterate in array for (var i = 0; i < n; i++) { // Check if current element and k gives // a greater sum than max element if (arr[i] + K >= maxi) // Increment the count cnt++; else continue; } // Return the final result return cnt; } // Driver code var arr = [4, 2, 1, 1, 2]; var k = 1; var n = arr.length; document.write( countNum(arr, k, n)); </script>
1
Complejidad temporal: O(n)
Espacio auxiliar: O(1)