Problem Statement:
You are given an array of size n. And the next line will contain n positive integers. Your favourite number is 7. The array will be nice if half or more of the numbers in the array have 7 digits. Implement it using a function and traverse the array using pointers.
See the sample input-output for more clarification.
Note - If the array size is odd that time consider the ceil value as half, for example n=5, that means n/2 = 5/2 = 3
Sample Input- 1
-----------------------
6
33 1 17 171 88 734
Sample Output- 1
-----------------------
Beautiful
Sample Input- 2
-----------------------
5
33 1 17 11 88
Sample Output- 2
-----------------------
Ugly
Explanation -
In sample input 1 -
17 have the last digit 7
171 have the middle digit 7
734 has the first digit 7
answer:
Here is a function in C that returns 1 if half or more of the numbers in an array have 7 digits, and 0 otherwise:
int is_beautiful(int *arr, int n) {
int count = 0;
for (int *p = arr; p < arr + n; p++) {
if (*p % 10 == 7 || *p / 10 % 10 == 7 || *p / 100 == 7) {
count++;
}
}
return count * 2 >= n;
}
This function uses a loop to iterate over the elements of the array, and for each element it checks if any of the digits is 7. If any of the digits is 7, it increments the count. Finally, it returns 1 if the count is greater than or equal to half of the array size, and 0 otherwise.
For example, if you call is_beautiful([33, 1, 17, 171, 88, 734], 6)
, the function will return 1, as three of the numbers in the array have 7 digits.
If you call is_beautiful([33, 1, 17, 11, 88], 5)
, the function will return 0, as only one of the numbers in the array has 7 digits. ..
0 Comments