Given N three-digit numbers, your task is to find bit score of all N numbers and then print the number of pairs possible based on these calculated bit score.
Rule for calculating bit score from three digit number:
From the 3-digit number,
· extract largest digit and multiply by 11 then
· extract smallest digit multiply by 7 then
· add both the result for getting bit pairs.
Note: – Bit score should be of 2-digits, if above results in a 3-digit bit score, simply ignore most significant digit.
Consider following examples:
Say, number is 286
Largest digit is 8 and smallest digit is 2
So, 811+27 =102 so ignore most significant bit , So bit score = 02.
Say, Number is 123
Largest digit is 3 and smallest digit is 1
So, 311+71=40, so bit score is 40.
Rules for making pairs from above calculated bit scores
Condition for making pairs are
· Both bit scores should be in either odd position or even position to be eligible to form a pair.
· Pairs can be only made if most significant digit are same and at most two pair can be made for a given significant digit.
Constraints
N<=500
Input Format
First line contains an integer N, denoting the count of numbers.
Second line contains N 3-digit integers delimited by space
Output
One integer value denoting the number of bit pairs.
Explanation
Example 1
Input
8 234 567 321 345 123 110 767 111
Output
3
Explanation
After getting the most and least significant digits of the numbers and applying the formula given in Rule 1 we get the bit scores of the numbers as:
58 12 40 76 40 11 19 18
No. of pair possible are 3:
40 appears twice at odd-indices 3 and 5 respectively. Hence, this is one pair.
12, 11, 18 are at even-indices. Hence, two pairs are possible from these three-bit scores.
Hence total pairs possible is 3.
Solution
#include<iostream>
using namespace std;
int bit_score(int n)
{
int a, b, c, largest, smallest;
int score;
a = n%10; n/=10;
b = n%10; n/=10;
c = n%10; n/=10;
largest = (a>b)?a:b;
largest = (c>largest)?c:largest;
smallest = (a<b)?a:b;
smallest = (c<smallest)?c:smallest;
score = largest*11 + smallest*7;
score = score % 100;
return score;
}
int findPairs (int score_array[], int N)
{
int sig_dig[10], i, pairs = 0, msb;
for(i=0; i<10; i++)
{
sig_dig[i] = 0;
}
for(i=0; i<N; i=i+2)
{
msb = score_array[i] / 10;
for(int j =i+2; j<N; j=j+2)
{
if(msb == score_array[j]/10)
{
if(sig_dig[msb] < 2)
{
sig_dig[msb]++;
}
}
}
}
for(i=1; i<N; i=i+2)
{
msb = score_array[i] / 10;
for(int j =i+2; j<N; j=j+2)
{
if(msb == score_array[j]/10)
{
if(sig_dig[msb] < 2)
{
sig_dig[msb]++;
}
}
}
}
for(i=0; i<10; i++)
{
pairs = pairs + sig_dig[i];
}
return pairs;
}
int main()
{
int N, i;
int ip_array[501];
int score_array[501];
int pairs;
cin>>N;
for(i=0; i<N; i++)
{
cin>>ip_array[i];
}
for(i=0; i<N; i++)
{
score_array[i] = bit_score(ip_array[i]);
}
pairs = findPairs(score_array, N);
cout<<pairs;
return 0;
}
Code contributed by Divik.
this program may also help. 🙂
#include
#include
#include
char findBitScore(int n){
int left = n / 100,mid=(int(n/10)%10),right=n%10;
int max = left > mid && left > right ? left : (mid > right ? mid : right);
int min = left < mid && left < right ? left : (mid < right ? mid : right);
std::string bitscore = std::to_string(max*11 + min * 7);
return bitscore.length() == 3 ? bitscore.substr(1)[0] : bitscore[0];
}
int main(){
int N,pairs=0;
std::map hashForOdd,hashForEven;
printf(“Enter number of Total Numbers: “);
scanf(“%d”,&N);
printf(“Enter total %d numbers of only 3 Digits:\n”,N);
for (int i=0;i<N;i++){
int temp=0;
scanf("%d",&temp);
char bitScore = findBitScore(temp);
if(i % 2 == 0 && hashForEven[bitScore] =2)
pairs++;
}
else if(hashForOdd[bitScore] =2)
pairs++;
}
printf(“%d”,pairs);
return 0;
}
n=int(input())
l=[]
l=list(map(int, input().split()))
bit_score=[]
for i in range(n):
max_digit=max(list(str(l[i])))
min_digit=min(list(str(l[i])))
score=(int(max_digit)*11+int(min_digit)*7)
if(len(str(score))==2):
bit_score.append(score)
else:
bit_score.append(int(str(score)[1:]))
#print(bit_score)
d=[int(str(x)[0]) for x in bit_score]
#print(d)
cde=[0]*10
cdo=[0]*10
cp=0
for i in range (len(d)):
if(i%2==0):
cde[d[i]]+=1
else:
cdo[d[i]]+=1
for i in range(1,10):
if (cde[i]>3 and cdo[i]>3) or (cde[i]>3 and cdo[i]<3) or (cde[i]3):
cp+=3
elif cde[i]==2 and cdo[i]==0:
cp+=1
elif cde[i]==3 and cdo[i]==0:
cp+=2
elif cde[i]==2 and cdo[i]==1:
cp+=1
elif cde[i]==3 and cdo[i]==1:
cp+=2
elif cde[i]==2 and cdo[i]==2:
cp+=2
elif cde[i]==3 and cdo[i]==2:
cp+=2
elif cde[i]==2 and cdo[i]==3:
cp+=2
elif cde[i]==3 and cdo[i]==3:
cp+=2
elif cde[i]==0 and cdo[i]==2:
cp+=1
elif cde[i]==0 and cdo[i]==3:
cp+=2
elif cde[i]==1 and cdo[i]==2:
cp+=1
elif cde[i]==1 and cdo[i]==3:
cp+=2
print(cp)
bit_score(286) -> 2
but it should return -> 02 (i.e two digits)..that’s where your code fails..
“`
Note: – Bit score should be of 2-digits, if above results in a 3-digit bit score, simply ignore most significant digit.
“`
n=int(input())
a=[x for x in input().split()]
re=[];ro=[]
pairs=0
for i in range(len(a)):
x=int(max(a[i]))*11+int(min(a[i]))*7
if(x>100):
x-=100
if(i%2==0):
re.append(str(x).zfill(2))
else:
ro.append(str(x).zfill(2))
for i in range(10):
els=0;ols=0
for j in range((n+1)//2):
if(int(re[j])//10==i):
els+=1
if(int(ro[j])//10==i):
ols+=1
if(els==2):
pairs+=1
elif(els>2):
pairs+=2
if(ols==2):
pairs+=1
elif(ols>2):
pairs+=2
print(pairs)