Alternating Numbers
Last Updated :
31 Mar, 2021
Given a number N, the task is to check if N is an Alternating Number or not. If N is an Alternating Number then print “Yes” else print “No”.
Alternating Number is a positive integer for which, in base-10, the parity of its digits are alternates i.e., the digits in number N is followed by odd, even, odd, … or even, odd, even, … order.
Examples:
Input: N = 129
Output: Yes
Explanation:
129 has digits which is in alternate odd even odd form.
Input: N = 28
Output: No
Explanation:
28 has digits which is not in alternate odd even odd form.
Approach: The idea is to convert the number into a string and check if any digit is followed by the digit of the same parity then N is not an Alternating Numbers, else N is an Alternating Numbers.
Below is the implementation of the above approach:
C++
#include <bits/stdc++.h>
using namespace std;
bool isEvenOddForm(string s)
{
int n = s.length();
for ( int i = 0; i < n; i++) {
if (i % 2 == 0 && s[i] % 2 != 0)
return false ;
if (i % 2 == 1 && s[i] % 2 != 1)
return false ;
}
return true ;
}
bool isOddEvenForm(string s)
{
int n = s.length();
for ( int i = 0; i < n; i++) {
if (i % 2 == 0 && s[i] % 2 != 1)
return false ;
if (i % 2 == 1 && s[i] % 2 != 0)
return false ;
}
return true ;
}
bool isAlternating( int n)
{
string str = to_string(n);
return (isEvenOddForm(str)
|| isOddEvenForm(str));
}
int main()
{
int N = 129;
if (isAlternating(N))
cout << "Yes" ;
else
cout << "No" ;
return 0;
}
|
Java
class GFG{
static boolean isEvenOddForm(String s)
{
int n = s.length();
for ( int i = 0 ; i < n; i++)
{
if (i % 2 == 0 && s.charAt(i) % 2 != 0 )
return false ;
if (i % 2 == 1 && s.charAt(i) % 2 != 1 )
return false ;
}
return true ;
}
static boolean isOddEvenForm(String s)
{
int n = s.length();
for ( int i = 0 ; i < n; i++)
{
if (i % 2 == 0 && s.charAt(i) % 2 != 1 )
return false ;
if (i % 2 == 1 && s.charAt(i) % 2 != 0 )
return false ;
}
return true ;
}
static boolean isAlternating( int n)
{
String str = Integer.toString(n);
return (isEvenOddForm(str) ||
isOddEvenForm(str));
}
public static void main(String[] args)
{
int N = 129 ;
if (isAlternating(N))
System.out.println( "Yes" );
else
System.out.println( "No" );
}
}
|
Python3
def isEvenOddForm(s):
n = len (s)
for i in range (n):
if (i % 2 = = 0 and
int (s[i]) % 2 ! = 0 ):
return False
if (i % 2 = = 1 and
int (s[i]) % 2 ! = 1 ):
return False
return True
def isOddEvenForm(s):
n = len (s)
for i in range (n):
if (i % 2 = = 0 and
int (s[i]) % 2 ! = 1 ):
return False
if (i % 2 = = 1 and
int (s[i]) % 2 ! = 0 ):
return False
return True
def isAlternating(n):
s = str (n)
return (isEvenOddForm(s) or
isOddEvenForm(s))
N = 129
if (isAlternating(N)):
print ( "Yes" )
else :
print ( "No" )
|
C#
using System;
class GFG{
static bool isEvenOddForm(String s)
{
int n = s.Length;
for ( int i = 0; i < n; i++)
{
if (i % 2 == 0 && s[i] % 2 != 0)
return false ;
if (i % 2 == 1 && s[i] % 2 != 1)
return false ;
}
return true ;
}
static bool isOddEvenForm(String s)
{
int n = s.Length;
for ( int i = 0; i < n; i++)
{
if (i % 2 == 0 && s[i] % 2 != 1)
return false ;
if (i % 2 == 1 && s[i] % 2 != 0)
return false ;
}
return true ;
}
static bool isAlternating( int n)
{
String str = n.ToString();
return (isEvenOddForm(str) ||
isOddEvenForm(str));
}
public static void Main(String[] args)
{
int N = 129;
if (isAlternating(N))
Console.WriteLine( "Yes" );
else
Console.WriteLine( "No" );
}
}
|
Javascript
<script>
function isEvenOddForm(s)
{
let n = s.length;
for (let i = 0; i < n; i++)
{
if (i % 2 == 0 && s[i] % 2 != 0)
return false ;
if (i % 2 == 1 && s[i] % 2 != 1)
return false ;
}
return true ;
}
function isOddEvenForm(s)
{
let n = s.length;
for (let i = 0; i < n; i++)
{
if (i % 2 == 0 && s[i] % 2 != 1)
return false ;
if (i % 2 == 1 && s[i] % 2 != 0)
return false ;
}
return true ;
}
function isAlternating(n)
{
let str = n.toString();
return (isEvenOddForm(str) ||
isOddEvenForm(str));
}
let N = 129;
if (isAlternating(N))
document.write( "Yes" );
else
document.write( "No" );
</script>
|
Time Complexity: O(log10N)
Share your thoughts in the comments
Please Login to comment...