Reputation: 65
I am doing a question on leetcode.com that says
Given an array nums of integers, return how many of them contain an even number of digits I tried to solve it in C
int numDigit(int n);
// returns the amount of odd numbers in the array
int findNumbers(int* nums, int numsSize){
int c = 0;
for(int i = 0; i < numsSize; i++){
if(numDigit(nums[i]) % 2 == 0){
c++;
i++;
}
else{
i++;
}
}
return(c);
}
//using recursion to find the amount of digits in a number
int numDigit(int n){
int d = 0;
while (n > 0){
n = n/10;
d++;
numDigit(n);
}
return(d);
}
Using this approach, some test cases pass while others fail. An example of a failed test case is this
Input: [555,901,482,1771]
Output: 0
Expected: 1
Kindly help me improve my solution to suit all test cases.
Upvotes: 2
Views: 2214
Reputation: 11
For each element nums[i]
it will go through numberofDigit
function where it counts the number of digit in the element and return the number of digit which is then assigned to cnt
.
res
counts the then number of times cnt
is even will be given:
class Solution {
static int numberOfDigit(int n) {
int count = 0;
while (n > 0) {
n = n / 10;
count++;
}
return count;
}
public int findNumbers(int[] nums) {
int res = 0;
for (int i=0;i<nums.length;i++) {
int cnt = numberOfDigit(nums[i]);
if (cnt % 2 == 0) {
res++;
}
}
return res;
}
}
Upvotes: 0
Reputation: 31306
You're incrementing i
twice in findNumbers
. Change to:
int findNumbers(int* nums, int numsSize){
int c = 0;
for(int i = 0; i < numsSize; i++){
if(numDigit(nums[i]) % 2 == 0){
c++;
}
}
return c;
}
You could have discovered this easily by printing num[i]
first thing in each iteration.
You also had a big no no. You had the same statement (i++
) in both branches of the if statement.
And there's also this issue:
while (n > 0){
n = n/10;
d++;
numDigit(n); // Completely pointless statement
}
You can remove the recursive call, because it accomplish absolutely nothing. If you want to do this recursively, it would look like this:
int numDigits(int n) {
if(n > 0)
return 1 + numDigits(n/10);
return 0;
}
Upvotes: 6
Reputation: 324
I think a better solution would be dividing by 10..100..1000 and if the result is 0,.. while your are dividing by 10^n where n is and odd number. The digits are odd.
Something like this:
int main()
{
int number=1324; //The input number
int auxNumber;
int div=10;
int divExp=1;
while(1)
{
auxNumber=number/div;
if(auxNumber==0)
{
if(divExp%2==0)
{
//Number of digits even
}else
{
//Number of digits odd
}
}
divExp++;
div*=10;
}
}
This function can be called for each number in the array and increment a counter if returns even number for example.
More detailed explanation of how it works:
Let's say you have this array: 132, 58 First, you call this function for the first number: 132 In the function you start dividing it by 10 and the result is 13 (it's an int so you won't have decimals) so there will be another iteration because it's not 0. This time you divide it by 100 and the result still won't be 0. At the third iteration you divide it by 1000 and the result is 0. because you are dividing by 10^3 and 3 is and odd number the number of digits in 132 is odd.
Next, you call this function for the next number: 58. First iteration: 58/10=5 Second iteration: 58/100=0. Because you are dividing by 10^2 and 2 is an even number the number of digits in 58 is even.
Upvotes: 0
Reputation: 725
Klutt is right, I was testing your solution with the bug fixed:
#include <stdio.h>
//Using recursion to find the amount of digits in a number
int numDigit(int n){
int d = 0;
while (n > 0){
n = n/10;
d++;
numDigit(n);
}
return(d);
}
// Returns the amount of odd numbers in the array nums
int findNumbers(int* nums, int numsSize){
int c = 0;
for(int i = 0; i < numsSize; i++){
if(numDigit(nums[i]) % 2 == 0){
c++;
}
}
return(c);
}
int main()
{
//The test case
int input [4] = {555, 901, 482, 1771};
printf("%d", findNumbers(input, 4));
return 0;
}
Upvotes: 0