Given a range [L, R]. The task is to find the total number of Hexadecimal alphabets that are required to write every number in the range.
Hexadecimal alphabets are the alphabets in the range [A, F] that are required to represent decimal numbers from the range [10, 15]
Examples:
Input: L = 10, R = 15
Output: 6
All the numbers from 10 to 15 contain a hexadecimal alphabet.
Input: L = 15, R = 16
Output: 1
15 and 16 are represented in hexadecimal as F and 10 respectively.
Approach:
- First of all, check if num ? 10 and num ? 15. If yes then increment the count as decimal numbers from 10 to 15 containing a hexadecimal alphabet.
- If num > 15 then update the number as num = num % 16. If it is greater than 10 then increment the count.
- Repeat 2nd step till the number (for every number) is greater than 0.
Below is the implementation of the above approach:
C++
// C++ implementation of the approach
#include <bits/stdc++.h>
using namespace std;
// Function that will count
// total hexadecimal alphabet
int countHexadecimal(int L, int R)
{
int count = 0;
for (int i = L; i <= R; i++) {
// All the numbers from 10 to 15
// contain a hexadecimal alphabet
if (i >= 10 && i <= 15)
count++;
// If i > 15 then perform mod by 16 repeatedly
// till the number is > 0
// If number % 16 > 10 then increase count
else if (i > 15) {
int k = i;
while (k != 0) {
if (k % 16 >= 10)
count++;
k = k / 16;
}
}
}
return count;
}
// Driver code
int main()
{
int L = 5, R = 100;
cout << countHexadecimal(L, R);
return 0;
}
Java
// Java implementation of the approach
class GFG
{
// Function that will count
// total hexadecimal alphabet
static int countHexadecimal(int L, int R)
{
int count = 0;
for (int i = L; i <= R; i++)
{
// All the numbers from 10 to 15
// contain a hexadecimal alphabet
if (i >= 10 && i <= 15)
count++;
// If i > 15 then perform mod by 16
// repeatedly till the number is > 0
// If number % 16 > 10 then increase count
else if (i > 15)
{
int k = i;
while (k != 0)
{
if (k % 16 >= 10)
count++;
k = k / 16;
}
}
}
return count;
}
// Driver code
public static void main(String args[])
{
int L = 5, R = 100;
System.out.print(countHexadecimal(L, R));
}
}
// This code is contributed
// by Akanksha Rai
Python3
# Python3 implementation of the approach
# Function that will count
# total hexadecimal alphabet
def countHexadecimal(L, R) :
count = 0;
for i in range(L, R + 1) :
# All the numbers from 10 to 15
# contain a hexadecimal alphabet
if (i >= 10 and i <= 15) :
count += 1;
# If i > 15 then perform mod by 16
# repeatedly till the number is > 0
# If number % 16 > 10 then
# increase count
elif (i > 15) :
k = i;
while (k != 0) :
if (k % 16 >= 10) :
count += 1;
k = k // 16;
return count;
# Driver code
if __name__ == "__main__" :
L = 5; R = 100;
print(countHexadecimal(L, R));
# This code is contributed by Ryuga
C#
// C# implementation of the approach
using System;
class GFG
{
// Function that will count
// total hexadecimal alphabet
static int countHexadecimal(int L, int R)
{
int count = 0;
for (int i = L; i <= R; i++)
{
// All the numbers from 10 to 15
// contain a hexadecimal alphabet
if (i >= 10 && i <= 15)
count++;
// If i > 15 then perform mod by 16 repeatedly
// till the number is > 0
// If number % 16 > 10 then increase count
else if (i > 15)
{
int k = i;
while (k != 0)
{
if (k % 16 >= 10)
count++;
k = k / 16;
}
}
}
return count;
}
// Driver code
public static void Main()
{
int L = 5, R = 100;
Console.Write(countHexadecimal(L, R));
}
}
// This code is contributed
// by Akanksha Rai
PHP
<?php
// PHP implementation of the approach
// Function that will count
// total hexadecimal alphabet
function countHexadecimal($L, $R)
{
$count = 0;
for ($i = $L; $i <= $R; $i++)
{
// All the numbers from 10 to 15
// contain a hexadecimal alphabet
if ($i >= 10 && $i <= 15)
$count++;
// If i > 15 then perform mod by 16
// repeatedly till the number is > 0
// If number % 16 > 10 then increase count
else if ($i > 15)
{
$k = $i;
while ($k != 0)
{
if ($k % 16 >= 10)
$count++;
$k = $k / 16;
}
}
}
return $count;
}
// Driver code
$L = 5;
$R = 100;
echo countHexadecimal($L, $R);
// This code is contributed by Ita_c
?>
JavaScript
<script>
// Javascript implementation of the approach
// Function that will count
// total hexadecimal alphabet
function countHexadecimal(L, R)
{
var count = 0;
for (var i = L; i <= R; i++) {
// All the numbers from 10 to 15
// contain a hexadecimal alphabet
if (i >= 10 && i <= 15)
count++;
// If i > 15 then perform mod by 16 repeatedly
// till the number is > 0
// If number % 16 > 10 then increase count
else if (i > 15) {
var k = i;
while (k != 0) {
if (k % 16 >= 10)
count++;
k = k / 16;
}
}
}
return count;
}
// Driver code
var L = 5, R = 100;
document.write( countHexadecimal(L, R));
// This code is contributed by rutvik_56.
</script>
Time Complexity: O(N * log16 N), Here N is the range (R - L) and for every number, we need log16 N time to calculate the number of hexadecimal alphabets.
Auxiliary Space: O(1), As constant extra space is used.
Method 2: Using string manipulation and set intersection
1. This method uses a set intersection operation to check if the hexadecimal characters are present in the hexadecimal string of each number in the range.
2. It initializes a set of hexadecimal characters and loops through each number in the range.
3. It then computes the hexadecimal string of the number using the built-in hex function and removes the '0x' prefix using string slicing.
4. It checks if the intersection of the set of hexadecimal characters and the set of characters in the hexadecimal string is non-empty, indicating the presence of a hexadecimal character. If the intersection is non-empty, the count is incremented.
C++
#include <bits/stdc++.h>
using namespace std;
// Function to count the number of hexadecimal numbers between L and R
int count_hex_numbers(int L, int R) {
int count = 0;
set<char> hex_chars = {'a', 'b', 'c', 'd', 'e', 'f', 'A', 'B', 'C', 'D', 'E', 'F'};
for (int num = L; num <= R; num++) {
stringstream ss;
ss << hex << num;
string hex_str = ss.str();
for (char c : hex_str) {
if (hex_chars.count(c)) {
count += 1;
break;
}
}
}
return count;
}
int main() {
int L = 10;
int R = 15;
cout << count_hex_numbers(L, R) << endl;
return 0;
}
Java
import java.util.*;
public class GFG {
public static int countHexNumbers(int L, int R) {
int count = 0;
Set<Character> hexChars = new HashSet<Character>();
hexChars.addAll(Arrays.asList('a', 'b', 'c', 'd', 'e', 'f', 'A', 'B', 'C', 'D', 'E', 'F'));
for (int num = L; num <= R; num++) {
Set<Character> digits = new HashSet<Character>();
for (char c : Integer.toHexString(num).toCharArray()) {
digits.add(c);
}
digits.remove('x');
if (!Collections.disjoint(digits, hexChars)) {
count++;
}
}
return count;
}
public static void main(String[] args) {
int L = 10;
int R = 15;
System.out.println(countHexNumbers(L, R));
}
}
Python3
def count_hex_numbers(L, R):
count = 0
hex_chars = set('abcdefABCDEF')
for num in range(L, R+1):
if set(hex(num)[2:]) & hex_chars:
count += 1
return count
L = 10
R = 15
print(count_hex_numbers(L, R))
JavaScript
function count_hex_numbers(L, R) {
let count = 0;
let hex_chars = new Set(['a', 'b', 'c', 'd', 'e', 'f', 'A', 'B', 'C', 'D', 'E', 'F']);
for (let num = L; num <= R; num++) {
let hex_str = num.toString(16);
if (hex_str.slice(0, 2) === '0x')
hex_str = hex_str.slice(2);
if (hex_chars.has(hex_str))
count += 1;
}
return count;
}
let L = 10;
let R = 15;
console.log(count_hex_numbers(L, R));
C#
using System;
using System.Collections.Generic;
namespace HexCount {
class Program {
// Function to count the number of hexadecimal numbers
// between L and R
static int CountHexNumbers(int L, int R)
{
int count = 0;
HashSet<char> hexChars
= new HashSet<char>{ 'a', 'b', 'c', 'd',
'e', 'f', 'A', 'B',
'C', 'D', 'E', 'F' };
for (int num = L; num <= R; num++) {
// Converting decimal number to hexadecimal
// string
string hexStr = num.ToString("X");
foreach(char c in hexStr)
{
if (hexChars.Contains(c)) {
count += 1;
break;
}
}
}
return count;
}
static void Main(string[] args)
{
int L = 10;
int R = 15;
Console.WriteLine(CountHexNumbers(L, R));
}
}
}
Time complexity: O((R-L+1) * k), where k is the length of the maximum hexadecimal string in the range.
The reason for this is that the method loops through each number in the range (R-L+1 numbers), computes its hexadecimal string using the built-in hex function (which takes time proportional to the number of digits in the hexadecimal representation), removes the '0x' prefix using string slicing (which takes constant time), and checks if the intersection of the set of hexadecimal characters and the set of characters in the hexadecimal string is non-empty using the & operator (which takes time proportional to the length of the sets being intersected). The maximum length of the hexadecimal string is k, which is log base 16 of the maximum number in the range.
Auxiliary space: O(k), since it creates a set of hexadecimal characters with constant size and potentially creates a set of characters in the hexadecimal string for each number in the range with maximum size k. However, since k is a fixed constant, the space complexity is considered O(1) in practice.
Similar Reads
count_if() in C++ STL count_if() function returns the number of elements in a range that satisfy the condition. Syntax: template <class InputT, class UnaryPredicate> typename iterator_traits <InputT> :: difference_type count_if(InputT first, InputT last, UnaryPredicate p); Examples: Input: 0 1 2 3 4 5 6 7 8 9
2 min read
Count Numbers with N digits which consists of odd number of 0's We are given a number N. The task is to find the count of numbers which have N digits and odd number of zeroes. Note: The number can have preceding 0's. Examples: Input : N = 2 Output : Count = 18 Input : N = 3 Output : Count = 244 Suppose a number with N digits which contains only single zero. So t
4 min read
Perform n steps to convert every digit of a number in the format [count][digit] Given a number num as a string and a number N. The task is to write a program which converts the given number num to another number after performing N steps. At each step, every digit of num will be written in the format [count][digit] in the new number, where count is the number of times a digit oc
12 min read
Find the count of natural Hexadecimal numbers of size N Given an integer N, the task is to find the count of natural Hexadecimal numbers with N digits. Examples: Input: N = 1 Output: 15 Input: N = 2 Output: 240 Approach: It can be observed that for the values of N = 1, 2, 3, ..., a series will be formed as 15, 240, 3840, 61440, 983040, 15728640, ... whic
2 min read
Convert a string to hexadecimal ASCII values Given a string as input, write a program to convert the characters of the given string into the hexadecimal equivalent of ASCII values. Examples : Input: GeekOutput: 4765656b Input: IronMan part 3Output :49726f6e4d616e20706172742033 ASCII stands for American Standard Code for Information Interchan
7 min read