Find all numbers in range whose digits are increasing decreasing alternatively
Last Updated :
01 Feb, 2022
Given integers L and R, find all numbers in range L to R whose digits are increasing-decreasing alternatively i.e. if the digits in the current number are d1, d2, d3, d4, d5 . . . then d1 < d2 > d3 < d4. . . must hold true.
Examples:
Input: L = 60, R = 100
Output: 67 68 69 78 79 89
Explanation: These numbers follow the increasing decreasing manner of digits
Input: L = 4, R = 12
Output: 4 5 6 7 8 9 12
Approach: Traverse all numbers in range L to R and find the numbers with given pattern of digits. Follow the steps mentioned below:
- Traverse each digit in the number
- Check if the character on two index ahead is increasing from current character,
- Else check if character on two index ahead is decreasing from current character
- If both the case is false, break and check for next number
- If all cases are true, print the number
Below is the implementation of the above approach.
C++
#include <bits/stdc++.h>
using namespace std;
bool check( int N)
{
string S = to_string(N);
for ( int i = 0; i < S.size(); i++) {
if (i == 0) {
int next = i + 1;
if (next < S.size()) {
if (S[i] >= S[next]) {
return false ;
}
}
}
else if (i == S.size() - 1) {
int prev = i - 1;
if (prev >= 0) {
if (i & 1) {
if (S[i] <= S[prev]) {
return false ;
}
}
else {
if (S[i] >= S[prev]) {
return false ;
}
}
}
}
else {
int prev = i - 1;
int next = i + 1;
if (i & 1) {
if ((S[i] > S[prev]) &&
(S[i] > S[next])) {
}
else {
return false ;
}
}
else {
if ((S[i] < S[prev]) &&
(S[i] < S[next])) {
}
else {
return false ;
}
}
}
}
return true ;
}
void findNumbers( int L, int R)
{
for ( int i = L; i <= R; i++)
if (check(i))
cout << i << " " ;
}
int main()
{
int L = 60, R = 100;
findNumbers(L, R);
return 0;
}
|
Java
class GFG
{
static boolean check( int N)
{
String S = Integer.toString(N);
for ( int i = 0 ; i < S.length(); i++) {
if (i == 0 ) {
int next = i + 1 ;
if (next < S.length()) {
if (S.charAt(i) >= S.charAt(next)) {
return false ;
}
}
}
else if (i == S.length() - 1 ) {
int prev = i - 1 ;
if (prev >= 0 ) {
if ((i & 1 ) > 0 ) {
if (S.charAt(i) <= S.charAt(prev)) {
return false ;
}
} else {
if (S.charAt(i) >= S.charAt(prev)) {
return false ;
}
}
}
} else {
int prev = i - 1 ;
int next = i + 1 ;
if ((i & 1 ) > 0 ) {
if ((S.charAt(i) > S.charAt(prev)) &&
(S.charAt(i) > S.charAt(next))) {
} else {
return false ;
}
} else {
if ((S.charAt(i) < S.charAt(prev)) &&
(S.charAt(i) < S.charAt(next))) {
} else {
return false ;
}
}
}
}
return true ;
}
static void findNumbers( int L, int R) {
for ( int i = L; i <= R; i++)
if (check(i))
System.out.print(i + " " );
}
public static void main(String args[]) {
int L = 60 , R = 100 ;
findNumbers(L, R);
}
}
|
Python3
def check(N):
S = str (N);
for i in range ( len (S)):
if (i = = 0 ):
next = i + 1 ;
if ( next < len (S)):
if ( ord (S[i]) > = ord (S[ next ])):
return False ;
elif (i = = len (S) - 1 ):
prev = i - 1 ;
if (prev > = 0 ):
if (i & 1 ):
if ( ord (S[i]) < = ord (S[prev])):
return False ;
else :
if ( ord (S[i]) > = ord (S[prev])):
return False ;
else :
prev = i - 1 ;
next = i + 1 ;
if (i & 1 ):
if ( ord (S[i]) > ord (S[prev])) and ( ord (S[i]) > ord (S[ next ])):
print (" ", end=" ")
else :
return False ;
else :
if ( ord (S[i]) < ord (S[prev])) and ( ord (S[i]) < ord (S[ next ])):
print (" ", end=" ")
else :
return False ;
return True ;
def findNumbers(L, R):
for i in range (L, R + 1 ):
if (check(i)):
print (i, end = " " )
L = 60
R = 100 ;
findNumbers(L, R);
|
C#
using System;
class GFG
{
static bool check( int N)
{
string S = N.ToString();
for ( int i = 0; i < S.Length; i++) {
if (i == 0) {
int next = i + 1;
if (next < S.Length) {
if (S[i] >= S[next]) {
return false ;
}
}
}
else if (i == S.Length - 1) {
int prev = i - 1;
if (prev >= 0) {
if ((i & 1) > 0) {
if (S[i] <= S[prev]) {
return false ;
}
}
else {
if (S[i] >= S[prev]) {
return false ;
}
}
}
}
else {
int prev = i - 1;
int next = i + 1;
if ((i & 1) > 0) {
if ((S[i] > S[prev]) &&
(S[i] > S[next])) {
}
else {
return false ;
}
}
else {
if ((S[i] < S[prev]) &&
(S[i] < S[next])) {
}
else {
return false ;
}
}
}
}
return true ;
}
static void findNumbers( int L, int R)
{
for ( int i = L; i <= R; i++)
if (check(i))
Console.Write(i + " " );
}
public static void Main()
{
int L = 60, R = 100;
findNumbers(L, R);
}
}
|
Javascript
<script>
function check(N)
{
let S = N.toString();
for (let i = 0; i < S.length; i++)
{
if (i == 0)
{
let next = i + 1;
if (next < S.length)
{
if (S[i].charCodeAt(0) >= S[next].charCodeAt(0))
{
return false ;
}
}
}
else if (i == S.length - 1)
{
let prev = i - 1;
if (prev >= 0) {
if (i & 1) {
if (S[i].charCodeAt(0) <= S[prev].charCodeAt(0)) {
return false ;
}
}
else {
if (S[i].charCodeAt(0) >= S[prev].charCodeAt(0)) {
return false ;
}
}
}
}
else {
let prev = i - 1;
let next = i + 1;
if (i & 1) {
if ((S[i].charCodeAt(0) > S[prev].charCodeAt(0)) &&
(S[i].charCodeAt(0) > S[next].charCodeAt(0))) {
}
else {
return false ;
}
}
else {
if ((S[i].charCodeAt(0) < S[prev].charCodeAt(0)) &&
(S[i].charCodeAt(0) < S[next].charCodeAt(0))) {
}
else {
return false ;
}
}
}
}
return true ;
}
function findNumbers(L, R) {
for (let i = L; i <= R; i++)
if (check(i))
document.write(i + " " )
}
let L = 60, R = 100;
findNumbers(L, R);
</script>
|
Time Complexity: O((R-L) * D) where D is the number of digits in R
Auxiliary Space: O(D)
Like Article
Suggest improvement
Share your thoughts in the comments
Please Login to comment...