Need help with this question

I am getting wrong answer for my code. I cant figure out what i am doing wrong.

Link to the problem:- http://www.codeplanted.com/practice/6

My code:-

#include
#include
using namespace std;

bool CheckNumber(int* arr, int total, int num) {

int temp = num;

while (temp != 0) {
	for (int i = 0; i < total; i++) {
		if ((temp % 10) == arr[i]) {
			return false;
		}
	}
	temp /= 10;
}

return true;

}

int main() {

int cases;

cin >> cases;

while (cases > 0) {

	int total;

	cin >> total;

	int* arr = new int[total];

	for (int i = 0; i < total; i++) {
		cin >> arr[i];
	}

	int target;
	cin >> target;

	int temp = target;

	int count = 0;

	while (temp != 0) {
		temp /= 10;
		count++;
	}



	temp = target;

	bool flag = true;
	while (temp != 0) {
		for (int i = 0; i < total; i++) {
			if ((temp % 10) == arr[i]) {
				flag = false;
				break;

			}
		}
		temp /= 10;
	}


	if (flag == true) {
		cout << 0 << endl;
	}

	else {

		int upper = -1, lower = -1;

		for (int i = target + 1; i <= 999; i++) {
			if (CheckNumber(arr, total, i)) {
				upper = i;
				break;
			}
		}

		for (int i = target - 1; i >= 0; i--) {
			if (CheckNumber(arr, total, i)) {
				lower = i;
				break;
			}
		}

		if (upper == -1) {
			cout << abs(target - lower) << endl;
		}
		else if (lower == -1) {
			cout << abs(target - upper) << endl;
		}
		else {

			int min;



			int l1 = abs(target - lower);
			int l2 = abs(target - upper);
			if (l1 < l2) {
				min = l1;
			}
			else {
				min = l2;
			}

			cout << min << endl;
		}
	}

	cases--;
}


return 0;

}

def greater_number(arr,num):
s = num
s1 = str(s)
j = len(s1)-1
while(j>=0):
c = 0
for i in range(0,len(arr)):
if(arr[i]==int(s1[j])):
s1 = str(int(s1)+1)
c=c+1
if(c==0):
j=j-1

return int(s1)

def lesser_number(arr,num):
s = num
s1 = str(s)
j = len(s1)-1
while(j>=0):
c = 0
for i in range(0,len(arr)):
if(arr[i]==int(s1[j])):
s1 = str(int(s1)-1)
c=c+1
if(c==0):
j=j-1

return int(s1)

def final_result(p,q,num):
if((p-num) >= abs(q-num)):
return abs(q-num)
else:
return p-num
test_cases = int(input())
a = []
b = []
for i in range(0,test_cases):
failed_buttons = list(map(int,input().split()))
chanel_target = int(input())
a.append(failed_buttons)
b.append(chanel_target)
for j in range(0,test_cases):
arr = a[j]
arr = arr[1:]
arr.sort()
num = b[j]
p = greater_number(arr,num)
q = lesser_number(arr,num)
print(final_result(p,q,num))

Try this python code it works