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.
Input: N = 129
129 has digits which is in alternate odd even odd form.
Input: N = 28
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:
Time Complexity: O(log10N)
Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the DSA Self Paced Course at a student-friendly price and become industry ready.
- Sum of alternating sign Squares of first N natural numbers
- Euler zigzag numbers ( Alternating Permutation )
- Sum of alternating sign cubes of first N Natural numbers
- Length of the longest alternating even odd subarray
- Longest alternating subsequence with maximum sum | Set 2
- Minimum number of replacements to make the binary string alternating | Set 2
- Maximum length Subsequence with alternating sign and maximum Sum
- Count of N-digit Numbers having Sum of even and odd positioned digits divisible by given numbers
- Fill the missing numbers in the array of N natural numbers such that arr[i] not equal to i
- Count of numbers upto M divisible by given Prime Numbers
- Numbers less than N which are product of exactly two distinct prime numbers
- Check if a given pair of Numbers are Betrothed numbers or not
- Print N lines of 4 numbers such that every pair among 4 numbers has a GCD K
- Maximum sum of distinct numbers such that LCM of these numbers is N
- Count numbers which can be constructed using two numbers
- Count numbers which are divisible by all the numbers from 2 to 10
- Absolute difference between the Product of Non-Prime numbers and Prime numbers of an Array
- Number of ways to obtain each numbers in range [1, b+c] by adding any two numbers in range [a, b] and [b, c]
- Absolute Difference between the Sum of Non-Prime numbers and Prime numbers of an Array
- Print numbers such that no two consecutive numbers are co-prime and every three consecutive numbers are co-prime
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to firstname.lastname@example.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.