Dada una array, la tarea es encontrar los elementos mínimo y máximo en la array que son divisibles por un número dado k.
Ejemplos:
Input: arr[] = {12, 1235, 45, 67, 1}, k=5 Output: Minimum = 45, Maximum = 1235 Input: arr[] = {10, 1230, 45, 67, 1}, k=10 Output: Minimum = 10, Maximum = 1230
Acercarse:
- Tome una variable mínima que almacene el elemento mínimo e inicialícela con INT_MAX y compárela con cada elemento de la array y actualice el siguiente elemento mínimo que es divisible por k.
- Tome una variable máxima que almacene el elemento máximo e inicialícela con INT_MIN y compárela con cada elemento de la array y actualice el siguiente elemento máximo que es divisible por k.
A continuación se muestra la implementación del enfoque anterior:
C++
// C++ implementation of the above approach #include <bits/stdc++.h> using namespace std; // Function to find the minimum element int getMin(int arr[], int n, int k) { int res = INT_MAX; for (int i = 0; i < n; i++) { if (arr[i] % k == 0) res = min(res, arr[i]); } return res; } // Function to find the maximum element int getMax(int arr[], int n, int k) { int res = INT_MIN; for (int i = 1; i < n; i++) { if (arr[i] % k == 0) res = max(res, arr[i]); } return res; } // Driver code int main() { int arr[] = { 10, 1230, 45, 67, 1 }; int k = 10; int n = sizeof(arr) / sizeof(arr[0]); cout << "Minimum element of array which is divisible by k: " << getMin(arr, n, k) << "\n"; cout << "Maximum element of array which is divisible by k: " << getMax(arr, n, k); return 0; }
C
// C implementation of the above approach #include <stdio.h> #include <limits.h> int max(int a, int b) { int max = a; if(max < b) max = b; return max; } int min(int a,int b) { int min = a; if(min > b) min = b; return min; } // Function to find the minimum element int getMin(int arr[], int n, int k) { int res = INT_MAX; for (int i = 0; i < n; i++) { if (arr[i] % k == 0) res = min(res, arr[i]); } return res; } // Function to find the maximum element int getMax(int arr[], int n, int k) { int res = INT_MIN; for (int i = 1; i < n; i++) { if (arr[i] % k == 0) res = max(res, arr[i]); } return res; } // Driver code int main() { int arr[] = { 10, 1230, 45, 67, 1 }; int k = 10; int n = sizeof(arr) / sizeof(arr[0]); printf("Minimum element of array which is divisible by k: %d\n",getMin(arr, n, k)); printf("Maximum element of array which is divisible by k: %d\n",getMax(arr, n, k)); return 0; } // This code is contributed by kothavvsaakash.
Java
//Java implementation of the above approach class GFG { // Function to find the minimum element static int getMin(int arr[], int n, int k) { int res = Integer.MAX_VALUE; for (int i = 0; i < n; i++) { if (arr[i] % k == 0) { res = Math.min(res, arr[i]); } } return res; } // Function to find the maximum element static int getMax(int arr[], int n, int k) { int res = Integer.MIN_VALUE; for (int i = 1; i < n; i++) { if (arr[i] % k == 0) { res = Math.max(res, arr[i]); } } return res; } // Driver code public static void main(String[] args) { int arr[] = {10, 1230, 45, 67, 1}; int k = 10; int n = arr.length; System.out.println("Minimum element of array which is divisible by k: " + getMin(arr, n, k)); System.out.println("Maximum element of array which is divisible by k: " + getMax(arr, n, k)); } } //This code contribute by Shikha Singh
Python 3
# Python 3 implementation of the # above approach import sys # Function to find the minimum element def getMin(arr, n, k): res = sys.maxsize for i in range(n): if (arr[i] % k == 0): res = min(res, arr[i]) return res # Function to find the maximum element def getMax(arr, n, k): res = 0 for i in range(1, n): if (arr[i] % k == 0): res = max(res, arr[i]) return res # Driver code if __name__ == "__main__": arr = [ 10, 1230, 45, 67, 1 ] k = 10 n = len(arr) print("Minimum element of array which", "is divisible by k: ", getMin(arr, n, k)) print( "Maximum element of array which", "is divisible by k: ", getMax(arr, n, k)) # This code is contributed # by ChitraNayal
C#
// C# implementation of the above approach using System; class GFG { // Function to find the minimum element static int getMin(int []arr, int n, int k) { int res = int.MaxValue; for (int i = 0; i < n; i++) { if (arr[i] % k == 0) { res = Math.Min(res, arr[i]); } } return res; } // Function to find the maximum element static int getMax(int []arr, int n, int k) { int res = int.MinValue; for (int i = 1; i < n; i++) { if (arr[i] % k == 0) { res = Math.Max(res, arr[i]); } } return res; } // Driver code static public void Main () { int []arr = {10, 1230, 45, 67, 1}; int k = 10; int n = arr.Length; Console.WriteLine("Minimum element of array " + "which is divisible by k: " + getMin(arr, n, k)); Console.WriteLine("Maximum element of array " + "which is divisible by k: " + getMax(arr, n, k)); } } // This code is contributes by ajit
PHP
<?php // PHP implementation of the above approach // Function to find the minimum element function getMin($arr, $n, $k) { $res = PHP_INT_MAX; for ($i = 0; $i < $n; $i++) { if ($arr[$i] % $k == 0) $res = min($res, $arr[$i]); } return $res; } // Function to find the maximum element function getMax($arr, $n, $k) { $res = PHP_INT_MIN; for ($i = 1; $i < $n; $i++) { if ($arr[$i] % $k == 0) $res = max($res, $arr[$i]); } return $res; } // Driver code $arr = array( 10, 1230, 45, 67, 1 ); $k = 10; $n = sizeof($arr); echo "Minimum element of array which is " . "divisible by k: ", getMin($arr, $n, $k) , "\n"; echo "Maximum element of array which is " . "divisible by k: ", getMax($arr, $n, $k); // This code is contributed by akt_mit ?>
Javascript
<script> // Javascript implementation of the above approach // Function to find the minimum element function getMin(arr, n, k) { let res = Number.MAX_VALUE; for (let i = 0; i < n; i++) { if (arr[i] % k == 0) res = Math.min(res, arr[i]); } return res; } // Function to find the maximum element function getMax(arr, n, k) { let res = Number.MIN_VALUE; for (let i = 1; i < n; i++) { if (arr[i] % k == 0) res = Math.max(res, arr[i]); } return res; } // Driver code let arr = [ 10, 1230, 45, 67, 1 ]; let k = 10; let n = arr.length; document.write("Minimum element of array which is divisible by k: " + getMin(arr, n, k) + "<br>"); document.write("Maximum element of array which is divisible by k: " + getMax(arr, n, k)); </script>
Producción:
Minimum element of array which is divisible by k: 10 Maximum element of array which is divisible by k: 1230
Publicación traducida automáticamente
Artículo escrito por VishalBachchas y traducido por Barcelona Geeks. The original can be accessed here. Licence: CCBY-SA