The similarity between two RGB color codes say "#ABCDEF" and "#UVWXYZ" is defined as negative of (hex(AB) - hex(UV)) ^ 2 + (hex(CD) - hex(WX)) ^ 2 + (hex(EF) - hex(YZ)) ^ 2. Where hex() is the hexadecimal value corresponding to the input character.
An RGB color code is called perfect if it is in form of ‘#AABBCC’. For example, ‘#11aabb’, ‘#000011’, and ‘#aabbff’ are perfect RGB color codes but ‘#123456’, ‘#112234’ are not perfect.
Note:An RGB color code is consists of 7 characters in which the first character is ‘#’ and every other character represents a hexadecimal digit from 0 to f.
You are given a string of size 7 that represents an RGB color code, Your task is to find a perfect RGB color code that is most similar to the given RGB color code, i.e. have the maximum similarity value.
The first line contains an integer 'T' which denotes the number of test cases or queries to be run.
The first line of each test case contains a string ‘color’ representing an RGB color code.
Output Format:
For each test case, print a string that represents a perfect RGB color code and is most similar to the given RGB color code.
The output of each test case will be printed in a separate line.
Note:
You do not need to input or print anything, and it has already been taken care of. Just implement the given function.
1 <= T <= 5
length of color = 7
color[0] = ‘#’
Color[i] is a hexadecimal digit from 0 to f, for every i - 1 to 6.
Time Limit: 1sec
2
#01aa45
#000000
#00aa44
#000000
Test case 1:
‘#00aa44’ is the perfect rgb color code that is most similar to the the given string as -( ((01) - (00)) ^ 2 + ((aa) - (aa)) ^ 2 + ((45) - (44)) ^ 2) = - (1 + 0 + 1) = -2 which is the maximum possible.
Test case 2:
The given string is already a perfect RGB color code, so we can return the same.
1
#fa4411
#ff4411
There are not many cases, try to think of solution by considering all cases
We know that the given string can be divided into three parts, and each part can be handled separately.
For example, for the string "#09f166", we will process "09", ''f1”, and ''66”separately. Our goal is to make the two characters of each part the same with the smallest distance from the original. In fact, there are not many choices for each path as there can be only 16 characters possible i.e [ ‘0’, ‘1’, ‘2’ …. ‘f’ ].
O(1)
As there are only at most 16 possible hexadecimal, and we are iterating over these only 3 times that takes constant time. Hence the overall time complexity is O(1).
O(1)
We are using an array to store values corresponding to hexadecimal values of 16 elements that will take constant space. Hence the overall space complexity is O(1).