Return a list of n smallest even integers greater than or equal to start

Function must return a list of the smallest even integers that are greater than or equal to, sorted into ascending order(函数必须返回大于或等于的最小偶数整数的列表,按升序排序)-python

  • 发表于: 6月 3, 2021 6月 3, 2021
  • 标签: maxlength, numbers, python, range

Smallest even digits number not less than N

Given a number N, we need to write a program to find the smallest number not less than N, which has all digits even.
Examples:

Input: N = 1345 Output: 2000 Explanation: 2000 is the smallest number not less than N, whose all digits are even. Input : N = 2397 Output : 2400 Explanation: 2400 is the smallest number not less than N, whose all digits are even.

Recommended: Please try your approach on {IDE} first, before moving on to the solution.

Naive approach: A naive approach is to keep iterating from N until we find a number with all digits even.
Below is the implementation of above approach:

C++




// CPP program to print the smallest
// integer not less than N with all even digits
#include <bits/stdc++.h>
using namespace std;
// function to check if all digits
// are even of a given number
int check_digits(int n)
{
// iterate for all digits
while (n) {
if ((n % 10) % 2) // if digit is odd
return 0;
n /= 10;
}
// all digits are even
return 1;
}
// function to return the smallest number
// with all digits even
int smallest_number(int n)
{
// iterate till we find a
// number with all digits even
for (int i = n;; i++)
if (check_digits(i))
return i;
}
// Driver Code
int main()
{
int N = 2397;
cout << smallest_number(N);
return 0;
}
Java




// Java program to print the smallest
// integer not less than N with all
// even digits
class GFG {
// function to check if all digits
// are even of a given number
static int check_digits(int n)
{
// iterate for all digits
while (n != 0) {
// if digit is odd
if ((n % 10) % 2 != 0)
return 0;
n /= 10;
}
// all digits are even
return 1;
}
// function to return the smallest
// number with all digits even
static int smallest_number(int n)
{
// iterate till we find a
// number with all digits even
for (int i = n; ; i++)
if (check_digits(i) != 0)
return i;
}
// Driver Code
public static void main(String[] args)
{
int N = 2397;
System.out.println(smallest_number(N));
}
}
// This code is contributed by
// Smitha Dinesh Semwal
Python3




# Python3 program to print the smallest
# integer not less than N with
# all even digits
# function to check if all digits
# are even of a given number
def check_digits(n) :
# iterate for all digits
while (n) :
# if digit is odd
if ((n % 10) % 2) :
return 0
n = int(n / 10)
# all digits are even
return 1
# function to return the
# smallest number with
# all digits even
def smallest_number(n) :
# iterate till we find a
# number with all digits even
for i in range(n, 2401) :
if (check_digits(i) == 1) :
return (i)
# Driver Code
N = 2397
print (str(smallest_number(N)))
# This code is contributed by
# Manish Shaw (manishshaw1)
C#




// C# program to print the smallest
// integer not less than N with all
// even digits
using System;
class GFG {
// function to check if all digits
// are even of a given number
static int check_digits(int n)
{
// iterate for all digits
while (n != 0) {
// if digit is odd
if ((n % 10) % 2 != 0)
return 0;
n /= 10;
}
// all digits are even
return 1;
}
// function to return the smallest
// number with all digits even
static int smallest_number(int n)
{
// iterate till we find a
// number with all digits even
for (int i = n; ; i++)
if (check_digits(i) != 0)
return i;
}
// Driver Code
public static void Main()
{
int N = 2397;
Console.WriteLine(smallest_number(N));
}
}
// This code is contributed by anuj_67.
PHP




<?php
// PHP program to print the smallest
// integer not less than N with
// all even digits
// function to check if all digits
// are even of a given number
function check_digits($n)
{
// iterate for all digits
while ($n)
{
// if digit is odd
if (($n % 10) % 2)
return 0;
$n /= 10;
}
// all digits are even
return 1;
}
// function to return the
// smallest number with
// all digits even
function smallest_number( $n)
{
// iterate till we find a
// number with all digits even
for ($i = $n; ; $i++)
if (check_digits($i))
return $i;
}
// Driver Code
$N = 2397;
echo smallest_number($N);
// This code is contributed by m_kit
?>
Javascript




<script>
// Javascript program to print the smallest
// integer not less than N with all
// even digits
// function to check if all digits
// are even of a given number
function check_digits(n)
{
// iterate for all digits
while (n != 0) {
// if digit is odd
if ((n % 10) % 2 != 0)
return 0;
n = parseInt(n/10);
}
// all digits are even
return 1;
}
// function to return the smallest
// number with all digits even
function smallest_number(n)
{
// iterate till we find a
// number with all digits even
for (i = n; ; i++)
if (check_digits(i) != 0)
return i;
}
// Driver Code
var N = 2397;
document.write(smallest_number(N));
// This code is contributed by 29AjayKumar
</script>

Output:

2400

Time Complexity: O(N)
Efficient Approach: We can find the number by increasing the first odd digit in N by one and replacing all digits to the right of that odd digit with the smallest even digit (i.e. 0). If there are no odd digits in N, then N is the smallest number itself. For example, consider N = 213. Increment first odd digit in N i.e., 1 to 2 and replace all digits right to it by 0. So, our required number will be 220.
Tricky Cases:



  • If the first odd digit in N is 9, then we must replace the digit immediately to the left of that odd digit with the next even digit. For example, if N=44934, then smallest number=46000.
  • Another tricky case is when the first odd digit is 9 and the digit directly to the left of the first odd digit is 8. In this case, we must replace the digit directly to the left of first odd digit by 0 and the digit left to this digit by next even digit, and keep doing this until we find a digit other than 8. For example, if N=86891, then Y=88000. Finally, if all digits to the left continue to be 8 until we reach the leftmost digit, or if the first digit of N is 9, then we must add the smallest non-zero even digit (i.e. 2) as a new digit on the left. For example, if N=891 or N=910, then Y=2000.

Below is the implementation of the efficient approach:

C++




// CPP program to print the smallest
// integer not less than N with all even digits
#include <bits/stdc++.h>
using namespace std;
// function to return the answer when the
// first odd digit is 9
int trickyCase(string s, int index)
{
int index1 = -1;
// traverse towards the left to find the non-8 digit
for (int i = index - 1; i >= 0; i--) {
// index digit
int digit = s[i] - '0';
// if digit is not 8, then break
if (digit != 8) {
index1 = i;
break;
}
}
// if on the left side of the '9', no 8
// is found then we return by adding a 2 and 0's
if (index1 == -1)
return 2 * pow(10, s.length());
int num = 0;
// till non-8 digit add all numbers
for (int i = 0; i < index1; i++)
num = num * 10 + (s[i] - '0');
// if non-8 is even or odd than add the next even.
if (s[index1] % 2 == 0)
num = num * 10 + (s[index1] - '0' + 2);
else
num = num * 10 + (s[index1] - '0' + 1);
// add 0 to right of 9
for (int i = index1 + 1; i < s.length(); i++)
num = num * 10;
return num;
}
// function to return the smallest number
// with all digits even
int smallestNumber(int n)
{
int num = 0;
string s = "";
int duplicate = n;
// convert the number to string to
// perform operations
while (n) {
s = char(n % 10 + 48) + s;
n /= 10;
}
int index = -1;
// find out the first odd number
for (int i = 0; i < s.length(); i++) {
int digit = s[i] - '0';
if (digit & 1) {
index = i;
break;
}
}
// if no odd numbers are there, than n is the answer
if (index == -1)
return duplicate;
// if the odd number is 9,
// than tricky case handles it
if (s[index] == '9') {
num = trickyCase(s, index);
return num;
}
// add all digits till first odd
for (int i = 0; i < index; i++)
num = num * 10 + (s[i] - '0');
// increase the odd digit by 1
num = num * 10 + (s[index] - '0' + 1);
// add 0 to the right of the odd number
for (int i = index + 1; i < s.length(); i++)
num = num * 10;
return num;
}
// Driver Code
int main()
{
int N = 2397;
cout << smallestNumber(N);
return 0;
}
Java




// Java program to print the
// smallest integer not less
// than N with all even digits
import java.io.*;
import java.util.*;
import java.lang.*;
class GFG
{
// function to return
// the answer when the
// first odd digit is 9
static int trickyCase(String s,
int index)
{
int index1 = -1;
// traverse towards the left
// to find the non-8 digit
for (int i = index - 1; i >= 0; i--)
{
// index digit
int digit = s.charAt(i) - '0';
// if digit is not 8,
// then break
if (digit != 8)
{
index1 = i;
break;
}
}
// if on the left side of the
// '9', no 8 is found then we
// return by adding a 2 and 0's
if (index1 == -1)
return 2 * (int)Math.pow(10, s.length());
int num = 0;
// till non-8 digit
// add all numbers
for (int i = 0; i < index1; i++)
num = num * 10 + (s.charAt(i) - '0');
// if non-8 is even or odd
// than add the next even.
if (s.charAt(index1) % 2 == 0)
num = num * 10 +
(s.charAt(index1) - '0' + 2);
else
num = num * 10 +
(s.charAt(index1) - '0' + 1);
// add 0 to right of 9
for (int i = index1 + 1;
i < s.length(); i++)
num = num * 10;
return num;
}
// function to return
// the smallest number
// with all digits even
static int smallestNumber(int n)
{
int num = 0;
String s = "";
int duplicate = n;
// convert the number to
// string to perform operations
while (n > 0)
{
s = (char)(n % 10 + 48) + s;
n /= 10;
}
int index = -1;
// find out the
// first odd number
for (int i = 0; i < s.length(); i++)
{
int digit = s.charAt(i) - '0';
int val = digit & 1;
if (val == 1)
{
index = i;
break;
}
}
// if no odd numbers are there,
// than n is the answer
if (index == -1)
return duplicate;
// if the odd number is 9,
// than tricky case handles it
if (s.charAt(index) == '9')
{
num = trickyCase(s, index);
return num;
}
// add all digits till first odd
for (int i = 0; i < index; i++)
num = num * 10 +
(s.charAt(i) - '0');
// increase the
// odd digit by 1
num = num * 10 +
(s.charAt(index) - '0' + 1);
// add 0 to the right
// of the odd number
for (int i = index + 1;
i < s.length(); i++)
num = num * 10;
return num;
}
// Driver Code
public static void main(String args[])
{
int N = 2397;
System.out.print(smallestNumber(N));
}
}
// This code is contributed
// by Akanksha rai(Abby_akku)
Python3




# Python3 program to print the smallest
# integer not less than N with all even digits
# Function to return the answer when the
# first odd digit is 9
def trickyCase(s, index):
index1 = -1;
# traverse towards the left to find
# the non-8 digit
for i in range(index - 1, -1, -1):
# index digit
digit = s[i] - '0';
# if digit is not 8, then break
if (digit != 8):
index1 = i;
break;
# if on the left side of the '9',
# no 8 is found then we return by
# adding a 2 and 0's
if (index1 == -1):
return 2 * pow(10, len(s));
num = 0;
# till non-8 digit add all numbers
for i in range(index1):
num = num * 10 + (s[i] - '0');
# if non-8 is even or odd
# than add the next even.
if (s[index1] % 2 == 0):
num = num * 10 + (s[index1] - '0' + 2);
else:
num = num * 10 + (s[index1] - '0' + 1);
# add 0 to right of 9
for i in range(index1 + 1, len(s)):
num = num * 10;
return num;
# function to return the smallest
# number with all digits even
def smallestNumber(n):
num = 0;
s = "";
duplicate = n;
# convert the number to string to
# perform operations
while (n):
s = chr(n % 10 + 48) + s;
n = int(n / 10);
index = -1;
# find out the first odd number
for i in range(len(s)):
digit = ord(s[i]) - ord('0');
if (digit & 1):
index = i;
break;
# if no odd numbers are
# there, than n is the answer
if (index == -1):
return duplicate;
# if the odd number is 9, than
# tricky case handles it
if (s[index] == '9'):
num = trickyCase(s, index);
return num;
# add all digits till first odd
for i in range(index):
num = num * 10 + ord(s[i]) - ord('0');
# increase the odd digit by 1
num = num * 10 + (ord(s[index]) -
ord('0') + 1);
# add 0 to the right of the odd number
for i in range(index + 1, len(s)):
num = num * 10;
return num;
# Driver Code
N = 2397;
print(smallestNumber(N));
# This code is contributed
# by mits
C#




// C# program to print the smallest integer
// not less than N with all even digits
using System;
class GFG
{
// function to return the answer when
// the first odd digit is 9
static int trickyCase(string s,
int index)
{
int index1 = -1;
// traverse towards the left
// to find the non-8 digit
for (int i = index - 1; i >= 0; i--)
{
// index digit
int digit = s[i] - '0';
// if digit is not 8, then break
if (digit != 8)
{
index1 = i;
break;
}
}
// if on the left side of the
// '9', no 8 is found then we
// return by adding a 2 and 0's
if (index1 == -1)
return 2 * (int)Math.Pow(10, s.Length);
int num = 0;
// till non-8 digit add all numbers
for (int i = 0; i < index1; i++)
num = num * 10 + (s[i] - '0');
// if non-8 is even or odd
// than add the next even.
if (s[index1] % 2 == 0)
num = num * 10 +
(s[index1] - '0' + 2);
else
num = num * 10 +
(s[index1] - '0' + 1);
// add 0 to right of 9
for (int i = index1 + 1;
i < s.Length; i++)
num = num * 10;
return num;
}
// function to return the smallest number
// with all digits even
static int smallestNumber(int n)
{
int num = 0;
string s = "";
int duplicate = n;
// convert the number to
// string to perform operations
while (n > 0)
{
s = (char)(n % 10 + 48) + s;
n /= 10;
}
int index = -1;
// find out the first odd number
for (int i = 0; i < s.Length; i++)
{
int digit = s[i] - '0';
int val = digit & 1;
if (val == 1)
{
index = i;
break;
}
}
// if no odd numbers are there,
// than n is the answer
if (index == -1)
return duplicate;
// if the odd number is 9,
// than tricky case handles it
if (s[index] == '9')
{
num = trickyCase(s, index);
return num;
}
// add all digits till first odd
for (int i = 0; i < index; i++)
num = num * 10 +
(s[i] - '0');
// increase the odd digit by 1
num = num * 10 +
(s[index] - '0' + 1);
// add 0 to the right of the odd number
for (int i = index + 1;
i < s.Length; i++)
num = num * 10;
return num;
}
// Driver Code
public static void Main()
{
int N = 2397;
Console.Write(smallestNumber(N));
}
}
// This code is contributed
// by Akanksha Rai
?>
PHP




<?php
// PHP program to print
// the smallest integer
// not less than N with
// all even digits
// function to return
// the answer when the
// first odd digit is 9
function trickyCase($s, $index)
{
$index1 = -1;
// traverse towards the
// left to find the
// non-8 digit
for ($i = $index - 1;
$i >= 0; $i--)
{
// index digit
$digit = $s[$i] - '0';
// if digit is not
// 8, then break
if ($digit != 8)
{
$index1 = $i;
break;
}
}
// if on the left side
// of the '9', no 8
// is found then we
// return by adding a 2
// and 0's
if ($index1 == -1)
return 2 * pow(10,
strlen($s));
$num = 0;
// till non-8 digit
// add all numbers
for ($i = 0; $i < $index1; $i++)
$num = $num * 10 +
($s[$i] - '0');
// if non-8 is even or
// odd than add the next even.
if ($s[$index1] % 2 == 0)
$num = $num * 10 +
($s[$index1] - '0' + 2);
else
$num = $num * 10 +
($s[$index1] - '0' + 1);
// add 0 to right of 9
for ($i = $index1 + 1;
$i < strlen($s); $i++)
$num = $num * 10;
return $num;
}
// function to return
// the smallest number
// with all digits even
function smallestNumber($n)
{
$num = 0;
$s = "";
$duplicate = $n;
// convert the number
// to string to perform
// operations
while ($n)
{
$s = chr($n % 10 + 48) . $s;
$n = (int)($n / 10);
}
$index = -1;
// find out the
// first odd number
for ($i = 0;
$i < strlen($s); $i++)
{
$digit = $s[$i] - '0';
if ($digit & 1)
{
$index = $i;
break;
}
}
// if no odd numbers are
// there, than n is the answer
if ($index == -1)
return $duplicate;
// if the odd number
// is 9, than tricky
// case handles it
if ($s[$index] == '9')
{
$num = trickyCase($s, $index);
return $num;
}
// add all digits
// till first odd
for ($i = 0; $i < $index; $i++)
$num = $num * 10 +
($s[$i] - '0');
// increase the
// odd digit by 1
$num = $num * 10 +
($s[$index] - '0' + 1);
// add 0 to the right
// of the odd number
for ($i = $index + 1;
$i < strlen($s); $i++)
$num = $num * 10;
return $num;
}
// Driver Code
$N = 2397;
echo smallestNumber($N);
// This code is contributed
// by mits
?>
Javascript




<script>
// Javascript program to print the smallest integer
// not less than N with all even digits
// function to return the answer when
// the first odd digit is 9
function trickyCase(s, index)
{
let index1 = -1;
// traverse towards the left
// to find the non-8 digit
for (let i = index - 1; i >= 0; i--)
{
// index digit
let digit = s[i].charCodeAt() - '0'.charCodeAt();
// if digit is not 8, then break
if (digit != 8)
{
index1 = i;
break;
}
}
// if on the left side of the
// '9', no 8 is found then we
// return by adding a 2 and 0's
if (index1 == -1)
return 2 * Math.pow(10, s.length);
let num = 0;
// till non-8 digit add all numbers
for (let i = 0; i < index1; i++)
num = num * 10 + (s[i].charCodeAt() - '0'.charCodeAt());
// if non-8 is even or odd
// than add the next even.
if (s[index1].charCodeAt() % 2 == 0)
num = num * 10 +
(s[index1].charCodeAt() - '0'.charCodeAt() + 2);
else
num = num * 10 +
(s[index1].charCodeAt() - '0'.charCodeAt() + 1);
// add 0 to right of 9
for (let i = index1 + 1;
i < s.length; i++)
num = num * 10;
return num;
}
// function to return the smallest number
// with all digits even
function smallestNumber(n)
{
let num = 0;
let s = "";
let duplicate = n;
// convert the number to
// string to perform operations
while (n > 0)
{
s = String.fromCharCode(n % 10 + 48) + s;
n = parseInt(n / 10, 10);
}
let index = -1;
// find out the first odd number
for (let i = 0; i < s.length; i++)
{
let digit = s[i].charCodeAt() - '0'.charCodeAt();
let val = digit & 1;
if (val == 1)
{
index = i;
break;
}
}
// if no odd numbers are there,
// than n is the answer
if (index == -1)
return duplicate;
// if the odd number is 9,
// than tricky case handles it
if (s[index] == '9')
{
num = trickyCase(s, index);
return num;
}
// add all digits till first odd
for (let i = 0; i < index; i++)
num = num * 10 +
(s[i].charCodeAt() - '0'.charCodeAt());
// increase the odd digit by 1
num = num * 10 +
(s[index].charCodeAt() - '0'.charCodeAt() + 1);
// add 0 to the right of the odd number
for (let i = index + 1;
i < s.length; i++)
num = num * 10;
return num;
}
let N = 2397;
document.write(smallestNumber(N));
// This code is contributed by divyeshrabadiya07.
</script>

Output:

2400

Time Complexity: O(M), where M is the number of digits in N.




Article Tags :
Mathematical
number-digits
Practice Tags :
Mathematical
Read Full Article

Python program to print all even numbers in a range

Given starting and end points, write a Python program to print all even numbers in that given range.

Example:

Input: start = 4, end = 15 Output: 4, 6, 8, 10, 12, 14 Input: start = 8, end = 11 Output: 8, 10

Example #1: Print all even numbers from given list using for loop

Define start and end limit of range. Iterate from start till the range in the list using for loop and check if num % 2 == 0. If the condition satisfies, then only print the number.




# Python program to print Even Numbers in given range
start, end = 4, 19
# iterating each number in list
for num in range(start, end + 1):
# checking condition
if num % 2 == 0:
print(num, end = " ")

Output:

4 6 8 10 12 14 16 18


Example #2: Taking range limit from user input




# Python program to print Even Numbers in given range
start = int(input("Enter the start of range: "))
end = int(input("Enter the end of range: "))
# iterating each number in list
for num in range(start, end + 1):
# checking condition
if num % 2 == 0:
print(num, end = " ")

Output:

Enter the start of range: 4 Enter the end of range: 10 4 6 8 10




Article Tags :
Python
Python Programs
School Programming
Python list-programs
python-list
Practice Tags :
python-list
Read Full Article

Python Exercise: Print the even numbers from a given list

Last update on September 30 2021 07:12:12 (UTC/GMT +8 hours)

Video liên quan

Postingan terbaru

LIHAT SEMUA