Search
 
SCRIPT & CODE EXAMPLE
 

CPP

1523. Count Odd Numbers in an Interval Range solution in c++

//solution<1>

class Solution {
public:
    int countOdds(int low, int high) 
{
    if(low%2==1||high%2==1)
        return 1+(high-low)/2;
        else
            return (high-low)/2;
}
};

//solution<2>

class Solution {
public:
    int countOdds(int low, int high) {
       return (high+1)/2-(low/2);
    }
};

//solution<3>

#include<iostream>
using namespace std;

class Solution {
public:
	int countOdds(int low, int high) {
		int count{0};
		for (int i = low; i <= high; i++)
		{
			if (i % 2 == 1)
				count++;
		}
		return count;
	}
};

int main()
{
	int high, low;
	cin >> low >> high;
	Solution s;
	int result = s.countOdds(low, high);
	cout << result << "
";
	return 0;
}
Comment

PREVIOUS NEXT
Code Example
Cpp :: file c++ 
Cpp :: pop_back 
Cpp :: check if a string is palindrome cpp 
Cpp :: create a 2d vector in c++ 
Cpp :: c++ friend class 
Cpp :: tolower funciton in cpp 
Cpp :: delete dynamic array c++ 
Cpp :: console colors in C++ 
Cpp :: lambda c++ 
Cpp :: c++ vector push if not exist 
Cpp :: stringstream stream number to string 
Cpp :: std vector c++ 
Cpp :: cpp string slice 
Cpp :: find second highest number in c++ 
Cpp :: reverse order binary tree in c++ 
Cpp :: conditional operator in c++ 
Cpp :: number of digits in int c++ 
Cpp :: c++ vector of class objects 
Cpp :: sort vector of strings 
Cpp :: remove first occurrence of value from vector c++ 
Cpp :: tree to array c++ 
Cpp :: iterate vector c++ 
Cpp :: use of alphanumeric function c++, check if alphabet or digit from string 
Cpp :: set width qpushbutton 
Cpp :: cpp map insert 
Cpp :: difference between --a and a-- c++ 
Cpp :: enum c++ 
Cpp :: convert std vector to array 
Cpp :: toString method in c++ using sstream 
Cpp :: friend function in c++ 
ADD CONTENT
Topic
Content
Source link
Name
7+5 =