Nontrivial undulant Numbers
Given an integer N, the task is to check if N is a Nontrivial undulant Number.
Nontrivial undulant Number are numbers in base 10 > 100 which are of the form aba, abab, ababa, …, where a != b.
Examples:
Input: N = 121
Output: Yes
Explanation:
121 is in the form abaInput: N = 123
Output: No
Approach: The idea is to convert the number to the string. If the length of the string is even then we will check if the first half of the string is equal to the second half or not. If the length is odd we will add a 2nd character of the string at the last of the string to make it’s length even. Finally, check the first half of the string is equal to the second half or not.
Below is the implementation of the above approach:
C++
// C++ implementation to check if N // is a Nontrivial undulant number #include<bits/stdc++.h> using namespace std; // Function to check if a string // is double string or not bool isDouble( int num) { string s = to_string(num); int l = s.length(); // a and b should not be equal if (s[0] == s[1]) return false ; // Condition to check // if length is odd // make length even if (l % 2 == 1) { s = s + s[1]; l++; } // first half of s string s1 = s.substr(0, l/2); // second half of s string s2 = s.substr(l/2); // Double string if first // and last half are equal return s1 == s2; } // Function to check if N is an // Nontrivial undulant number bool isNontrivialUndulant( int N) { return N > 100 && isDouble(N); } // Driver Code int main() { int n = 121; if (isNontrivialUndulant(n)) cout << "Yes" ; else cout << "No" ; return 0; } |
Java
// Java implementation to check if N // is a Nontrivial undulant number class GFG{ // Function to check if a string // is double string or not static boolean isDouble( int num) { String s = Integer.toString(num); int l = s.length(); // a and b should not be equal if (s.charAt( 0 ) == s.charAt( 1 )) return false ; // Condition to check if length // is odd make length even if (l % 2 == 1 ) { s = s + s.charAt( 1 ); l++; } // First half of s String s1 = s.substring( 0 , l / 2 ); // Second half of s String s2 = s.substring(l / 2 ); // Double string if first // and last half are equal return s1.equals(s2); } // Function to check if N is an // Nontrivial undulant number static boolean isNontrivialUndulant( int N) { return N > 100 && isDouble(N); } // Driver code public static void main(String[] args) { int n = 121 ; if (isNontrivialUndulant(n)) { System.out.println( "Yes" ); } else { System.out.println( "No" ); } } } // This code is contributed by shubham |
Python3
# Python3 implementation to check if N # is a Nontrivial undulant number # Function to check if a string # is double string or not def isDouble(num): s = str (num) l = len (s) # a and b should not be equal if (s[ 0 ] = = s[ 1 ]): return False # Condition to check # if length is odd # make length even if (l % 2 = = 1 ): s = s + s[ 1 ] l + = 1 # First half of s s1 = s[:l / / 2 ] # Second half of s s2 = s[l / / 2 :] # Double string if first # and last half are equal return s1 = = s2 # Function to check if N is an # Nontrivial undulant number def isNontrivialUndulant(N): return N > 100 and isDouble(N) # Driver Code n = 121 if (isNontrivialUndulant(n)): print ( "Yes" ) else : print ( "No" ) # This code is contributed by vishu2908 |
C#
// C# implementation to check if N // is a Nontrivial undulant number using System; class GFG{ // Function to check if a string // is double string or not static bool isDouble( int num) { String s = num.ToString(); int l = s.Length; // a and b should not be equal if (s[0] == s[1]) return false ; // Condition to check if length // is odd make length even if (l % 2 == 1) { s = s + s[1]; l++; } // First half of s String s1 = s.Substring(0, l / 2); // Second half of s String s2 = s.Substring(l / 2); // Double string if first // and last half are equal return s1.Equals(s2); } // Function to check if N is an // Nontrivial undulant number static bool isNontrivialUndulant( int N) { return N > 100 && isDouble(N); } // Driver code public static void Main(String[] args) { int n = 121; if (isNontrivialUndulant(n)) { Console.WriteLine( "Yes" ); } else { Console.WriteLine( "No" ); } } } // This code is contributed by gauravrajput1 |
Javascript
<script> // JavaScript implementation to check if N // is a Nontrivial undulant number // Function to check if a string // is double string or not function isDouble(num) { let s = num.toString(); let l = s.length; // a and b should not be equal if (s[0] == s.charAt[1]) return false ; // Condition to check if length // is odd make length even if (l % 2 == 1) { s = s + s[1]; l++; } // First half of s let s1 = s.substr(0, l / 2); // Second half of s let s2 = s.substr(l / 2); // Double string if first // and last half are equal return (s1 == s2); } // Function to check if N is an // Nontrivial undulant number function isNontrivialUndulant(N) { return N > 100 && isDouble(N); } // Driver Code let n = 121; if (isNontrivialUndulant(n)) { document.write( "Yes" ); } else { document.write( "No" ); } // This code is contributed by susmitakundugoaldanga </script> |
Yes
Time Complexity: O(|N|)
References: OEIS
Contact Us