Exploration of thoughts: The number of odd numbers in a series

 The Problem

The count of odd numbers between two non-negative integers low and high (inclusively) is given by low and high.

Leet Code Problem 1523


Examples

Input: 1 3

Output: 2


Input:  2 8

Output: 3

You will learn about your thinking style by solving this problem


Programming newbie

If you are solving this with knowledge of basic programming constructs (loops, if else)

Thoughts

Ok so I have a high and a low number.
I can see a series between low to high (question addresses) 
How about I loop over numbers 
Check if they are odd

Algorithm

  1. create variable 'count' assign it integer 0
  2. Loop from low to high, with i  = low
    1. Check if i is odd 
      1. increase value of count by 1
  3. return or print the value of count
I am currently Learning JS

Code

var countOdds = function(low, high) {
    let count = 0;
    for(let i = low; i<=high; i++) {
        if (i % 2 != 0) {
            count++;
        }
    }
    return count
};
	
This solution gets job done. But it will soon starting to take time as series will start becoming big.
O(n)

Giving more thought and a mathematical perspective

It took me 4938 milliseconds (5 seconds) to write my solution, as i saw some graphs of other submissions. There are some runtimes that take 40-50 milliseconds.

My conquest begins. From Here I have to change my thinking

Thoughts

Ok, low and high number
i think in the series from 1 to 10, there are equal number of odds and even (Leads towards solution)
1,2,3,4,5,6,7,8,9,10
even: 2,4,6,8,10
odd:1,3,5,7,9
each of them are 5
If a number is Even, number next to it in the series will be odd (some expriemental finding)
Testing: 
2 and 8; series  = 2,3,4,5,6,7,8 , low = 2, high = 8, number of numbers = 7
So i have 7 numbers in which half will even 
7 / 2 = 3 (round off)

1 and 5; series = 1,2,3,4,5, low = 1, high = 5, number of numbers = 5
5 / 2 = 2 (roundoff) 
This is incorrect
Here is core mathematical division issue.
Whenever low is odd. and we divide number of numbers by 2, one digit gets left behind
1,2,3,4,5
odd,[even,odd,even,odd]

This can by solved
first i will get count of odds from [even, odd, even, odd] which is 2
then i will add 1 to it for odd
Now i will  get 3 which is the solution.

Algorithm

  1. create variables variables : count = 0, numberOfNumbers = 0
  2. check if low is odd
    1. increment count by 1
    2. set low to next value (which would be even) 
  3. Calculate numberOfNumber(high - low + 1)
  4. add (numberOfNumbers / 2) to count
  5. return or print count

Code

var countOdds = function(low, high) {
     let count = 0, numberOfNumbers = 0;
     if (low % 2 != 0) {
        count = 1;
        low = low + 1
    }
   
    numberOfNumbers = high - low + 1
    count += Math.floor(numberOfNumbers / 2)
    return count
};
	

Writing your thoughts about a solution in a general way will help you gain a new perspective on it







Comments

Popular posts from this blog

My Education

React: The Virtual DOM