Dada una array arr[] de tamaño N y una string S , la tarea es modificar la array dada eliminando todos los caracteres de sus representaciones hexadecimales que están presentes en S y luego reemplazando el elemento decimal equivalente nuevamente en la array.
Ejemplos:
Entrada: arr[] = {74, 91, 31, 122}, S = “1AB”
Salida: {4, 5, 15, 7}
Explicación:
74 -> (4A) 16 -> (4)16 -> 4
91 -> (5B)16 -> (5)16 -> 5
31 -> (1F)16 -> (F)16 -> 15
122 -> (7A)16 -> (7)16 -> 7Entrada: arr[] = {1450, 1716, 284, 843}, S = “ABFE3”
Salida: {5, 100, 284, 4}
Enfoque: siga los pasos a continuación para resolver el problema:
- Atraviesa la array arr[]:
- Convierta cada elemento de la array a su valor hexadecimal equivalente .
- Elimine los caracteres del número hexadecimal que están presentes en la string S.
- Convierta el número hexadecimal modificado de nuevo a su representación decimal .
- Reemplace el elemento de array con él.
- Imprime la array modificada.
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 convert a decimal number // to its equivalent hexadecimal number string decHex(int n) { char alpha[] = { 'A', 'B', 'C', 'D', 'E', 'F' }; string ans; while (n > 0) { if (n % 16 < 10) { ans += to_string(n % 16); } else { ans += alpha[n % 16 - 10]; } n /= 16; } reverse(ans.begin(), ans.end()); return ans; } // Function to convert hexadecimal number // to its equavalent decimal number int hexDec(string convertedHex) { // Stores characters with their // respective hexadecimal values char mp[] = { 10, 11, 12, 13, 14, 15 }; // Stores answer int ans = 0; int pos = 0; // Traverse the string reverse(convertedHex.begin(), convertedHex.end()); for (char ch : convertedHex) { // If digit if (isdigit(ch)) { ans += ((int)pow(16, pos)) * (ch - '0'); } // If character else { ans += ((int)pow(16, pos)) * mp[ch - 'A']; } pos += 1; } // Return the answer return ans; } // Function to move all the // alphabets to front string removeChars(string hexaVal, string S) { set<char> setk; for (char ch : S) { setk.insert(ch); } string ans = ""; for (char ch : hexaVal) { if (setk.find(ch) != setk.end()) { continue; } ans += ch; } return ans; } // Function to modify each array // element by removing characters // from their hexadecimal representation // which are present in a given string void convertArr(int arr[], int N, string S) { // Traverse the array for (int i = 0; i < N; i++) { // Stores hexadecimal value string hexaVal = decHex(arr[i]); // Remove the characters from hexadecimal // representation present in string S string convertedHex = removeChars(hexaVal, S); // Stores decimal value int decVal = hexDec(convertedHex); // Replace array element arr[i] = decVal; } // Print the modified array for (int i = 0; i < N; i++) { cout << arr[i] << " "; } } // Driven Program int main() { // Given array int arr[] = { 74, 91, 31, 122 }; int N = sizeof(arr) / sizeof(arr[0]); // Given string string S = "1AB"; // Function call to modify // array by given operations convertArr(arr, N, S); return 0; } // This code is contributed by Kingash.
Java
// java program for the above approach import java.io.*; import java.lang.*; import java.util.*; class GFG { // Function to convert a decimal number // to its equivalent hexadecimal number static String decHex(int n) { char alpha[] = { 'A', 'B', 'C', 'D', 'E', 'F' }; StringBuilder ans = new StringBuilder(""); while (n > 0) { if (n % 16 < 10) { ans.append(Integer.toString(n % 16)); } else { ans.append(alpha[n % 16 - 10]); } n /= 16; } ans = ans.reverse(); return ans.toString(); } // Function to convert hexadecimal number // to its equavalent decimal number static int hexDec(String convertedHex) { // Stores characters with their // respective hexadecimal values char mp[] = { 10, 11, 12, 13, 14, 15 }; // Stores answer int ans = 0; int pos = 0; // Traverse the string StringBuilder s = new StringBuilder(convertedHex); convertedHex = s.reverse().toString(); for (char ch : convertedHex.toCharArray()) { // If digit if (Character.isDigit(ch)) { ans += ((int)Math.pow(16, pos)) * (ch - '0'); } // If character else { ans += ((int)Math.pow(16, pos)) * mp[ch - 'A']; } pos += 1; } // Return the answer return ans; } // Function to move all the // alphabets to front static String removeChars(String hexaVal, String S) { HashSet<Character> setk = new HashSet<>(); for (char ch : S.toCharArray()) { setk.add(ch); } String ans = ""; for (char ch : hexaVal.toCharArray()) { if (setk.contains(ch)) { continue; } ans += ch; } return ans; } // Function to modify each array // element by removing characters // from their hexadecimal representation // which are present in a given string static void convertArr(int arr[], String S) { // Traverse the array for (int i = 0; i < arr.length; i++) { // Stores hexadecimal value String hexaVal = decHex(arr[i]); // Remove the characters from hexadecimal // representation present in string S String convertedHex = removeChars(hexaVal, S); // Stores decimal value int decVal = hexDec(convertedHex); // Replace array element arr[i] = decVal; } // Print the modified array for (int val : arr) { System.out.print(val + " "); } } // Driver Code public static void main(String[] args) { // Given array int arr[] = { 74, 91, 31, 122 }; // Given string String S = "1AB"; // Function call to modify // array by given operations convertArr(arr, S); } } // This code is contributed by Kingash.
Python3
# Python3 program for the above approach # Function to convert a decimal number # to its equivalent hexadecimal number def decHex(n): alpha = ['A', 'B', 'C', 'D', 'E', 'F'] ans = '' while n: if n % 16 < 10: ans += str(n % 16) else: ans += alpha[n % 16 - 10] n //= 16 ans = ans[::-1] return ans # Function to convert hexadecimal number # to its equavalent decimal number def hexDec(convertedHex): # Stores characters with their # respective hexadecimal values mp = {"A": 10, "B": 11, "C": 12, "D": 13, "E": 14, "F": 15} # Stores answer ans = 0 pos = 0 # Traverse the string for i in convertedHex[::-1]: # If digit if i.isdigit(): ans += (16**pos)*int(i) # If character else: ans += (16**pos)*mp[i] pos += 1 # Return the answer return ans # Function to move all the # alphabets to front def removeChars(hexaVal, S): setk = set() for i in S: setk.add(i) ans = '' for i in hexaVal: if i in setk: continue ans += i return ans # Function to modify each array # element by removing characters # from their hexadecimal representation # which are present in a given string def convertArr(arr, S): # Traverse the array for i in range(len(arr)): # Stores hexadecimal value hexaVal = decHex(arr[i]) # Remove the characters from hexadecimal # representation present in string S convertedHex = removeChars(hexaVal, S) # Stores decimal value decVal = hexDec(convertedHex) # Replace array element arr[i] = decVal # Print the modified array print(arr) # Driver Code # Given array arr = [74, 91, 31, 122] # Given string S = "1AB" # Function call to modify # array by given operations convertArr(arr, S)
Javascript
<script> // JavaScript program for the above approach // Function to convert a decimal number // to its equivalent hexadecimal number function decHex(n){ let alpha = ['A', 'B', 'C', 'D', 'E', 'F'] let ans = '' while(n){ if(n % 16 < 10) ans += (n % 16).toString() else ans += alpha[n % 16 - 10] n = Math.floor(n/16) } ans = ans.split("").reverse().join("") return ans } // Function to convert hexadecimal number // to its equavalent decimal {number function hexDec(convertedHex){ // Stores characters with their // respective hexadecimal values let mp = {"A": 10, "B": 11, "C": 12, "D": 13, "E": 14, "F": 15} // Stores answer let ans = 0 let pos = 0 // Traverse the string for(let i of convertedHex.split("").reverse().join("")){ // If digit if(i.charCodeAt(0)>=48 && i.charCodeAt(0)<=57) ans += Math.pow(16,pos)*parseInt(i) // If character else ans += Math.pow(16,pos)*mp[i] pos += 1 } // Return the answer return ans } // Function to move all the // alphabets to front function removeChars(hexaVal, S){ let setk = new Set() for(let i of S) setk.add(i) let ans = '' for(let i of hexaVal){ if(setk.has(i)) continue ans += i } return ans } // Function to modify each array // element by removing characters // from their hexadecimal representation // which are present in a given string function convertArr(arr, S){ // Traverse the array for(let i=0;i<arr.length;i++){ // Stores hexadecimal value let hexaVal = decHex(arr[i]) // Remove the characters from hexadecimal // representation present in string S let convertedHex = removeChars(hexaVal, S) // Stores decimal value let decVal = hexDec(convertedHex) // Replace array element arr[i] = decVal } // Print the modified array document.write(arr,"</br>") } // Driver Code // Given array let arr = [74, 91, 31, 122] // Given string let S = "1AB" // Function call to modify // array by given operations convertArr(arr, S) // This code is contributed by shinjanpatra </script>
[4, 5, 15, 7]
Complejidad de tiempo: O(N * |S|)
Espacio auxiliar: O(|S|)
Publicación traducida automáticamente
Artículo escrito por rohitsingh07052 y traducido por Barcelona Geeks. The original can be accessed here. Licence: CCBY-SA