Dada una array a[] de tamaño N y un número entero K, la tarea es encontrar un valor X en el rango [0, K] que pueda maximizar el valor de la función dada
- Xor-sum(X) = (X XOR A[0]) + (X Xor A[1]) + (X Xor A[2]) + __________+ (X Xor A[N-1]).
Ejemplos:
Entrada: a[] = {1, 2, 3, 4, 5, 6}, N=6, K=10
Salida: 8
Explicación: El valor de X es 1 para el cual la suma requerida se vuelve máxima.Entrada: a[] = {1, 6}, N=2, K=7
Salida: 0
Enfoque: La idea es considerar todos y cada uno de los valores de K y luego encontrar el valor de X que satisfaga la condición anterior. Siga los pasos a continuación para resolver el problema:
- Inicialice las variables max y X como 0 y -1 para almacenar la suma máxima definida y el valor de X para el que está sucediendo.
- Itere sobre el rango [0, K] usando la variable j y realice las siguientes tareas:
- Inicialice la suma variable como 0 para almacenar la suma definida.
- Itere sobre el rango [0, N) usando la variable i y realice las siguientes tareas:
- Establece el valor de sum como sum + j^a[i].
- Si sum es mayor que max, establezca el valor de max como sum y X como j.
- Después de realizar los pasos anteriores, imprima el valor de X como respuesta.
A continuación se muestra la implementación del enfoque anterior.
C++14
// C++ program for the above approach #include <bits/stdc++.h> using namespace std; // Function to find the value of X for which // the given sum maximises void findValue(vector<int>& a, int N, int K) { // Variables to store the maximum // sum and that particular value // of X. long long int max = 0, X = -1; // Check every value of K for (int j = 0; j <= K; j++) { // Variable to store the desired sum long long int sum = 0; for (int i = 0; i < N; i++) { (sum = sum + (j ^ a[i])); } // Check the condition if (sum > max) { max = sum; X = j; } } // Print the result cout << X; } // Driver Code int main() { vector<int> a = { 1, 2, 3, 4, 5, 6 }; int N = 6, K = 10; findValue(a, N, K); return 0; }
C
// C program for the above approach #include <stdio.h> // Function to find the value of X for which // the given sum maximises void findValue(int *a, int N, int K) { // Variables to store the maximum // sum and that particular value // of X. long long int max = 0, X = -1; // Check every value of K for (int j = 0; j <= K; j++) { // Variable to store the desired sum long long int sum = 0; for (int i = 0; i < N; i++) { (sum = sum + (j ^ a[i])); } // Check the condition if (sum > max) { max = sum; X = j; } } // Print the result printf("%lli", X); } // Driver Code int main() { int a[] = { 1, 2, 3, 4, 5, 6 }; int N = 6, K = 10; findValue(a, N, K); return 0; } // This code is contributed by palashi.
Java
// Java program for the above approach class GFG { // Function to find the value of X for which // the given sum maximises public static void findValue(int[] a, int N, int K) { // Variables to store the maximum // sum and that particular value // of X. int max = 0, X = -1; // Check every value of K for (int j = 0; j <= K; j++) { // Variable to store the desired sum int sum = 0; for (int i = 0; i < N; i++) { sum = sum + (j ^ a[i]); } // Check the condition if (sum > max) { max = sum; X = j; } } // Print the result System.out.println(X); } // Driver Code public static void main(String args[]) { int[] a = { 1, 2, 3, 4, 5, 6 }; int N = 6, K = 10; findValue(a, N, K); } } // This code is contributed by saurabh_jaiswal.
Python3
# Python3 program for the above approach # Function to find the value of X for which # the given sum maximises def findValue(a, N, K) : # Variables to store the maximum # sum and that particular value # of X. max = 0; X = -1; # Check every value of K for j in range( K + 1) : # Variable to store the desired sum sum = 0; for i in range(N) : sum = sum + (j ^ a[i]); # Check the condition if (sum > max) : max = sum; X = j; # Print the result print(X); # Driver Code if __name__ == "__main__" : a = [ 1, 2, 3, 4, 5, 6 ]; N = 6; K = 10; findValue(a, N, K); # This code is contributed by AnkThon
C#
// C# program for the above approach using System; public class GFG { // Function to find the value of X for which // the given sum maximises public static void findValue(int[] a, int N, int K) { // Variables to store the maximum // sum and that particular value // of X. int max = 0, X = -1; // Check every value of K for (int j = 0; j <= K; j++) { // Variable to store the desired sum int sum = 0; for (int i = 0; i < N; i++) { sum = sum + (j ^ a[i]); } // Check the condition if (sum > max) { max = sum; X = j; } } // Print the result Console.WriteLine(X); } // Driver Code public static void Main(string []args) { int[] a = { 1, 2, 3, 4, 5, 6 }; int N = 6, K = 10; findValue(a, N, K); } } // This code is contributed by AnkThon
Javascript
<script> // JavaScript Program to implement // the above approach // Function to find the value of X for which // the given sum maximises function findValue(a, N, K) { // Variables to store the maximum // sum and that particular value // of X. let max = 0, X = -1; // Check every value of K for (let j = 0; j <= K; j++) { // Variable to store the desired sum let sum = 0; for (let i = 0; i < N; i++) { (sum = sum + (j ^ a[i])); } // Check the condition if (sum > max) { max = sum; X = j; } } // Print the result document.write(X); } // Driver Code let a = [1, 2, 3, 4, 5, 6]; let N = 6, K = 10; findValue(a, N, K); // This code is contributed by Potta Lokesh </script>
8
Complejidad temporal: O(K*N)
Espacio auxiliar: O(1)
Publicación traducida automáticamente
Artículo escrito por pavankumarb289 y traducido por Barcelona Geeks. The original can be accessed here. Licence: CCBY-SA