Check whether an array of strings can correspond to a particular number X | C ++ implementation to check whether array of strings can correspond to a number X ; Function to find the maximum base possible for the number N ; Function to find the decimal equivalent of the number ; Condition to check if the number is convertible to another base ; Function to check that the array can correspond to a number X ; counter to count the numbers those are convertible to X ; Loop to iterate over the array ; Convert the current string to every base for checking whether it will correspond to X from any base ; Condition to check if every number of the array can be converted to X ; Driver Code ; The set of strings in base from [ 2 , 36 ]
#include <bits/stdc++.h> NEW_LINE using namespace std ; int val ( char c ) { if ( c >= '0' && c <= '9' ) return ( int ) c - '0' ; else return ( int ) c - ' A ' + 10 ; } int toDeci ( string str , int base ) { int len = str . size ( ) ; int power = 1 ; int num = 0 ; int i ; for ( i = len - 1 ; i >= 0 ; i -- ) { if ( val ( str [ i ] ) >= base ) { return -1 ; } num += val ( str [ i ] ) * power ; power = power * base ; } return num ; } void checkCorrespond ( vector < string > str , int x ) { int counter = 0 ; int n = str . size ( ) ; for ( int i = 0 ; i < n ; i ++ ) { for ( int j = 2 ; j <= 36 ; j ++ ) { if ( toDeci ( str [ i ] , j ) == x ) { counter ++ ; break ; } } } if ( counter == n ) cout << " YES " << " STRNEWLINE " ; else cout << " NO " << " STRNEWLINE " ; } int main ( ) { int x = 16 ; vector < string > str = { "10000" , "20" , "16" } ; checkCorrespond ( str , x ) ; return 0 ; }