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++ program for the above approach #include <bits/stdc++.h> using namespace std;
// Function to check if a string is // of the form even odd even odd... 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 ;
} // Function to check if a string is // of the form odd even odd even ... 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 ;
} // Function to check if n is an // alternating number bool isAlternating( int n)
{ string str = to_string(n);
return (isEvenOddForm(str)
|| isOddEvenForm(str));
} // Driver Code int main()
{ // Given Number N
int N = 129;
// Function Call
if (isAlternating(N))
cout << "Yes" ;
else
cout << "No" ;
return 0;
} |
// Java program for the above approach class GFG{
// Function to check if a string is // of the form even odd even odd... 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 ;
} // Function to check if a string is // of the form odd even odd even ... 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 ;
} // Function to check if n is an // alternating number static boolean isAlternating( int n)
{ String str = Integer.toString(n);
return (isEvenOddForm(str) ||
isOddEvenForm(str));
} // Driver Code public static void main(String[] args)
{ // Given number N
int N = 129 ;
// Function call
if (isAlternating(N))
System.out.println( "Yes" );
else
System.out.println( "No" );
} } // This Code is contributed by rock_cool |
# Python3 program for the above approach # Function to check if a string is # of the form even odd even odd... 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
# Function to check if a string is # of the form odd even odd even ... 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
# Function to check if n is an # alternating number def isAlternating(n):
s = str (n)
return (isEvenOddForm(s) or
isOddEvenForm(s))
# Driver Code # Given Number N N = 129
# Function Call if (isAlternating(N)):
print ( "Yes" )
else :
print ( "No" )
# This code is contributed by Vishal Maurya |
// C# program for the above approach using System;
class GFG{
// Function to check if a string is // of the form even odd even odd... 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 ;
} // Function to check if a string is // of the form odd even odd even ... 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 ;
} // Function to check if n is an // alternating number static bool isAlternating( int n)
{ String str = n.ToString();
return (isEvenOddForm(str) ||
isOddEvenForm(str));
} // Driver Code public static void Main(String[] args)
{ // Given number N
int N = 129;
// Function call
if (isAlternating(N))
Console.WriteLine( "Yes" );
else
Console.WriteLine( "No" );
} } // This code is contributed by Princi Singh |
<script> // Javascript program for the above approach // Function to check if a string is // of the form even odd even odd... 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 to check if a string is // of the form odd even odd even ... 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 to check if n is an // alternating number function isAlternating(n)
{ let str = n.toString();
return (isEvenOddForm(str) ||
isOddEvenForm(str));
} // Driver code // Given number N let N = 129; // Function call if (isAlternating(N))
document.write( "Yes" );
else document.write( "No" );
// This code is contributed by divyesh072019 </script> |
Yes
Time Complexity: O(log10N)