Nontrivial undulant Numbers
Last Updated :
15 Sep, 2021
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 aba
Input: 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++
#include<bits/stdc++.h>
using namespace std;
bool isDouble( int num)
{
string s = to_string(num);
int l = s.length();
if (s[0] == s[1])
return false ;
if (l % 2 == 1)
{
s = s + s[1];
l++;
}
string s1 = s.substr(0, l/2);
string s2 = s.substr(l/2);
return s1 == s2;
}
bool isNontrivialUndulant( int N)
{
return N > 100 && isDouble(N);
}
int main()
{
int n = 121;
if (isNontrivialUndulant(n))
cout << "Yes" ;
else
cout << "No" ;
return 0;
}
|
Java
class GFG{
static boolean isDouble( int num)
{
String s = Integer.toString(num);
int l = s.length();
if (s.charAt( 0 ) == s.charAt( 1 ))
return false ;
if (l % 2 == 1 )
{
s = s + s.charAt( 1 );
l++;
}
String s1 = s.substring( 0 , l / 2 );
String s2 = s.substring(l / 2 );
return s1.equals(s2);
}
static boolean isNontrivialUndulant( int N)
{
return N > 100 && isDouble(N);
}
public static void main(String[] args)
{
int n = 121 ;
if (isNontrivialUndulant(n))
{
System.out.println( "Yes" );
}
else
{
System.out.println( "No" );
}
}
}
|
Python3
def isDouble(num):
s = str (num)
l = len (s)
if (s[ 0 ] = = s[ 1 ]):
return False
if (l % 2 = = 1 ):
s = s + s[ 1 ]
l + = 1
s1 = s[:l / / 2 ]
s2 = s[l / / 2 :]
return s1 = = s2
def isNontrivialUndulant(N):
return N > 100 and isDouble(N)
n = 121
if (isNontrivialUndulant(n)):
print ( "Yes" )
else :
print ( "No" )
|
C#
using System;
class GFG{
static bool isDouble( int num)
{
String s = num.ToString();
int l = s.Length;
if (s[0] == s[1])
return false ;
if (l % 2 == 1)
{
s = s + s[1];
l++;
}
String s1 = s.Substring(0, l / 2);
String s2 = s.Substring(l / 2);
return s1.Equals(s2);
}
static bool isNontrivialUndulant( int N)
{
return N > 100 && isDouble(N);
}
public static void Main(String[] args)
{
int n = 121;
if (isNontrivialUndulant(n))
{
Console.WriteLine( "Yes" );
}
else
{
Console.WriteLine( "No" );
}
}
}
|
Javascript
<script>
function isDouble(num)
{
let s = num.toString();
let l = s.length;
if (s[0] == s.charAt[1])
return false ;
if (l % 2 == 1)
{
s = s + s[1];
l++;
}
let s1 = s.substr(0, l / 2);
let s2 = s.substr(l / 2);
return (s1 == s2);
}
function isNontrivialUndulant(N)
{
return N > 100 && isDouble(N);
}
let n = 121;
if (isNontrivialUndulant(n))
{
document.write( "Yes" );
}
else
{
document.write( "No" );
}
</script>
|
Time Complexity: O(|N|)
References: OEIS
Share your thoughts in the comments
Please Login to comment...