Check if digits of a number are increasing and decreasing alternatively
Last Updated :
27 Jan, 2022
Given a number N, the task is to check whether the digits of the number are increasing and decreasing alternatively. If the number of digits is less than 3, return false. For example, if d1, d2, and d3 are the digits of N, the d1 < d2 > d3, must hold true.
Examples:
Input: N = 6834
Output: true
Explanation: Digits of N are increasing and decreasing alternatively, i.e 6 < 8 > 3 < 4
Input: N = 32
Output: false
Approach: The task can be solved by converting the given integer into a string. Iterate over the string and check whether the adjacent characters follow the given conditions or not.
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 ;
}
int main()
{
int N = 64;
cout << (check(N) ? "true" : "false" );
}
|
Java
import java.io.*;
import java.lang.*;
import java.util.*;
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 % 2 == 1 ) {
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 % 2 == 1 ) {
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 ;
}
public static void main (String[] args) {
int N = 64 ;
if (check(N) == true )
System.out.print( "true" );
else
System.out.print( "false" );
}
}
|
Python3
def check(N):
S = str (N)
for i in range ( len (S)):
if (i = = 0 ):
next = i + 1
if ( next < len (S)):
if (S[i] > = S[ next ]):
return False
elif (i = = len (S) - 1 ):
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 :
prev = i - 1
next = i + 1
if (i & 1 ):
if ((S[i] > S[prev]) and (S[i] > S[ next ])):
print (" ", end=" ")
else :
return False
else :
if ((S[i] < S[prev]) and (S[i] < S[ next ])):
print (" ", end=" ")
else :
return False
return True
N = 64
print ( "true" if check(N) else "false" )
|
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 % 2 == 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 % 2 == 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 ;
}
public static void Main () {
int N = 64;
if (check(N) == true )
Console.Write( "true" );
else
Console.Write( "false" );
}
}
|
Javascript
<script>
function check(N){
let S = new String(N);
for (let i = 0; i < S.length; i++) {
if (i == 0) {
let next = i + 1;
if (next < S.length) {
if (S[i] >= S[next]) {
return false ;
}
}
}
else if (i == S.length - 1) {
let 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 {
let prev = i - 1;
let 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 ;
}
let N = 64;
document.write(check(N) ? "true" : "false" );
</script>
|
Time Complexity: O(N), N is the number of digits
Auxiliary Space: O(N)
Like Article
Suggest improvement
Share your thoughts in the comments
Please Login to comment...