Modify array by removing characters from their Hexadecimal representations which are present in a given string


We have given an array of positive integers and need to modify each element of the array by removing the characters given the ‘hex’ string from the hexadecimal representation of the current element.

To solve the problem, we can convert the current number to a hexadecimal number. After that, we can remove the characters from the hexadecimal string, which are common in ‘hex’ and the current hexadecimal string. After modifying the hexadecimal string, we can convert it back to decimal.

Problem statement – We have given an array containing positive integers, and the array's length is N. Also, we have given the ‘hex’ string containing 0 to 9 digits and ‘A’ to ‘F’ characters. We need to convert each array value to hexadecimal, remove the characters from the hexadecimal representation of the number which is present in the ‘hex’ string, and convert it back to the decimal number again.

Sample examples

Input– array[] = {92, 90, 75, 129}, hex = ‘1BC’

Output– [5, 90, 4, 8]

Explanation

  • The hexadecimal representation of 92 is ‘5C’. As ‘C’ is present in the ‘hex’ string, we must remove it. So, the resultant string will be ‘5’; after converting it to decimal, we get 5.

  • The hexadecimal value of ‘90’ is ‘5A’. As ‘5’ or ‘A’ is not present in the ‘hex’ string, it will remain as it is.

  • The hexadecimal representation of 75 is ‘4B’, and after removing the ‘B’ from it string becomes ‘4’. When we convert ‘4’ to decimal, we get 4.

  • The hexadecimal value of 129 is ‘81’. After removing ‘1’ from the modified hexadecimal string is ‘8’, and its decimal representation is 8.

Input– array[] = {54, 43,56,9999,1234, 32}, hex = "1BCFE234"

Output– [6, 0, 8, 112, 13, 0]

Explanation

  • Actual number -> hexadecimal -> modified hexadecimal -> final decimal

  • 54 -> 36 -> 6 -> 6

  • 43 -> 2B -> 0 -> 0

  • 56 -> 38 -> 8 -> 8

  • 9999 -> 270F -> 70 -> 112

  • 1234 -> 4D2 -> D -> 13

  • 32 -> 20 -> 0 -> 0

Approach 1

In this approach, we will first convert decimal value to hexadecimal value. After that, we will modify the hexadecimal value. Next, we will convert the modified hexadecimal value to decimal and replace the array element with the resultant value. Also, we will use different approaches to remove characters from the hexadecimal string, convert decimal to hexadecimal, and hexadecimal to decimal numbers.

Algorithm

  • Start traversing each string character in the changeArr() function.

  • Use the decimalToHexa() function to convert the array value to a hexadecimal value and store the result in the ‘hexa’ string variable.

    • In the decimalToHexa() function, return 0 if the number is 0.

    • Define the alpha[] array and store the characters. Also, Define the ‘hexa’ string.

    • Make iterations until the number is greater than zero.

    • If num % 16 <10 is true, append num % 16 to the ‘hexa’ string. Else, append the alpha[num % 16 - 10] to the ‘hexa’ string as we need to append characters for 11 to 16 values.

    • Divide num by 16.

    • When iterations of the while loop are completed, use the reverse() method to reverse the ‘hexa’ string and return its value.

  • Now, use the removeChars() function to remove the characters from the ‘hexa’ string, which are common between ‘hexa’ and the given ‘hex’ string.

    • In the removeChars() function, define the ‘charSet’ named set and insert all characters of the ‘hex’ string into that.

    • Now, traverse the ‘hexVal’ string. If the current character of the ‘hexVal’ string is present in the charset, continue iteration. Otherwise, append the current character to the ‘res’ string, which is temporary.

    • Return the ‘res’ string.

  • Now, use the decimalToHexa() function to convert the modified hexadecimal string to decimal.

    • In the decimalToHexa() function, define the ‘mp’ array containing numbers from 10 to 15.

    • Reverse the hexadecimal string.

    • Traverse the hexadecimal string. Multiply the character value with 16pos, and add it to the ‘res’ value.

    • – Add 1 to the ‘pos’ value.

  • Replace the array element with the ‘dec’ value.

Example

#include <bits/stdc++.h>
using namespace std;
string decimalToHexa(int num){
   if (num == 0) {
      return "0";
   }
   // char array to store hexadecimal number
   char alpha[] = {'A', 'B', 'C', 'D', 'E', 'F'};
   string Hexa;
   // Traverse the number until it becomes zero
   while (num > 0) {
      // if the remainder is less than 10, store it as it is
      if (num % 16 < 10) {
          Hexa += to_string(num % 16);
      } else {
          // else store the character
          Hexa += alpha[num % 16 - 10];
      }
      // divide the number by 16
      num /= 16;
   }
   // reverse the string
   reverse(Hexa.begin(), Hexa.end());
   return Hexa;
}
int hexaToDecimal(string modifiedHex) {
   // stores hexadecimal to decimal
   char mp[] = {10, 11, 12, 13, 14, 15};
   // Stores result
   int res = 0;
   int pos = 0;
   // reverse the string
   reverse(modifiedHex.begin(), modifiedHex.end());
   // Traverse the string
   for (char ch : modifiedHex) {
      // If a digit, multiply it with 16^pos
      if (isdigit(ch)) {
          res += ((int)pow(16, pos)) * (ch - '0');
      }
      // If character, multiply it with 16^pos
      else {
          res += ((int)pow(16, pos)) * mp[ch - 'A'];
      }
      // Increment the position
      pos += 1;
   }
   // Return the answer
   return res;
}
string removeChars(string hexaVal, string hex) {
   // set to store the characters of the given string
   set<char> charSet;
   // insert the characters of the given string in the set
   for (char ch : hex) {
      charSet.insert(ch);
   }
   // string to store the final hexadecimal number
   string res = "";
   // traverse the hexadecimal number string
   for (char ch : hexaVal) {
      // if the character is present in the set, then continue
      if (charSet.find(ch) != charSet.end()) {
          continue;
      }
      // else add the character to the final string
      res += ch;
   }
   // return the final string
   return res;
}
// function to modify the array as per the given condition
void changeArr(int array[], int N, string hex) {
   // Traverse the array
   for (int i = 0; i < N; i++) {
      // covnert the number to hexadecimal
      string hexa = decimalToHexa(array[i]);
      // remove the characters from the hexadecimal number string which are present in the given string
      string modifiedHex = removeChars(hexa, hex);
      // change the hexadecimal number to decimal
      int decVal = hexaToDecimal(modifiedHex);
      // replace the number with the new decimal value
      array[i] = decVal;
   }
   cout << "The final array after converting a number to hexadecimal, removing characters which are present in the string, and converting back to decimal is " << endl;
   // Print the modified array
   for (int i = 0; i < N; i++) {
      cout << array[i] << " ";
   }
}
int main() {
   // Given array
   int array[] = {54, 43,56,9999,1234, 32};
   int N = sizeof(array) / sizeof(array[0]);
   // Given string
   string hex = "1BCFE234";
   changeArr(array, N, hex);
   return 0;
}

Output

The final array after converting a number to hexadecimal, removing characters which are present in the string, and converting back to decimal is 
6 0 8 112 13 0 

Time complexity – O(N*S), Where N is the array length, and S is the string length.

Space complexity – O(S), as we need to store the string after modifying the string.

Updated on: 18-Aug-2023

42 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements