Problem Statement -:
Suppose "arr" is an array of 101 elements with every value ranging from 1 to 256.
Now, we need to select 3 different values a, b, c from arr (basically no repetition which means index_value of a, b, c w.r.t. arr should be different) which satisfies below 2 equations.
Eq1 -> 3z = (a - b + 3y - x)%m + 1
Eq 2 -> 2z = (a - c + y)%m + 1
Total no of permutations which needs to be checked will be 101x100x99
% (mod) is the remainder operator in python
x, y, z & arr are known. m is 256. We need to find all possible [a, b, c].
My Try -:
"""Known Values - x, y, z & arr"""
m = 256
no_diff_max = 100
set_value = [0 for i in range(3)]
temp_arr = [i for i in range(no_diff_max + 1)]
"""arr also has (no_diff_max+1) elements, i.e. 101 elements"""
"""To prevent running the loop for full size of 101*100*99, first 2 position arr will be iterated"""
"""Finds all permutations of size 2 (without repeatitions) from temp_arr. Size of perm_arr would be 101*100"""
perm_arr = permutation(temp_arr, 2)
for perm_set in perm_arr:
"""To put the first two values of perm_set in set_value. Third value will be looked in next step"""
set_value[0] = perm_set[0]
set_value[1] = perm_set[1]
"""Value of a & b are assigned as per the two different index values in perm_set"""
a = arr[set_value[0]]
b = arr[set_value[1]]
"""Find the value of z from Eq1 as mentioned above. solve_c3 function solves this."""
z = solve_c3(x, y, a, b, m)
"""From value of z, find c. After that, we will find this value in arr"""
c = (a - 2*z + y)%m + 1
"""This will find all the index in arr which equates to s2, meaning arr[index] == s2"""
indices = find_indices(arr, c)
if (len(indices) > 0): #If c is present in arr for at least 1 times
"""To find the third value of set_value. Total iterations are reduced as we are iterating in limited index values only as against to 99 values"""
for set_value[2] in indices:
"""This condition is to make sure that set_value[2] != set_value[0] & set_value[1]."""
"""This line makes sure that set_value has unique elements always"""
if (len(set_value) == len(set(set_value))):
"""Here, we can say for sure that set_value array exists with all the unique values. So, one set_value will be counted as one result"""
Is there any shorter method possible mathematically to solve this more quickly?
It would be great if someone could suggest me a better method to solve this.
Any help would be appreciated. Thanks in advance!