Demlo number (Square of 11…1)
Given a number of the form 11..1 such that number of digits in it is smaller than 10, find square of the number.
Examples:
Input : 111111
Output : 12345654321
Input : 1111
Output : 1234321
Squares of 11…1 (where length is smaller than 10) are called Demlo numbers. A demlo number is number which consist of 1, 2, 3….n, n-1, n-2, …..1.
Demlo numbers are:
1 = 1
11 * 11 = 121
111 * 111 = 12321
1111 * 1111 = 1234321
To find demlo number, first we find append n numbers which are increasing and then append numbers which are decreasing.
C++
#include <bits/stdc++.h>
using namespace std;
string printDemlo(string str)
{
int len = str.length();
string res = "" ;
for ( int i = 1; i <= len; i++)
res += char (i + '0' );
for ( int i = len - 1; i >= 1; i--)
res += char (i + '0' );
return res;
}
int main()
{
string str = "111111" ;
cout << printDemlo(str);
return 0;
}
|
Java
public class Main {
static String printDemlo(String str)
{
int len = str.length();
String res = "" ;
for ( int i = 1 ; i <= len; i++)
res += Integer.toString(i);
for ( int i = len - 1 ; i >= 1 ; i--)
res += Integer.toString(i);
return res;
}
public static void main(String[] args)
{
String str = "111111" ;
System.out.println(printDemlo(str));
}
}
|
Python3
def printDemlo(s):
l = len (s)
res = ""
for i in range ( 1 ,l + 1 ):
res = res + str (i)
for i in range (l - 1 , 0 , - 1 ):
res = res + str (i)
return res
s = "111111"
print (printDemlo(s))
|
C#
using System;
public class GFG {
static String printDemlo(String str)
{
int len = str.Length;
String res = "" ;
for ( int i = 1; i <= len; i++)
res += i.ToString();
for ( int i = len - 1; i >= 1; i--)
res += i.ToString();
return res;
}
public static void Main()
{
String str = "111111" ;
Console.WriteLine(printDemlo(str));
}
}
|
PHP
<?php
function printDemlo( $str )
{
$len = strlen ( $str );
$res = "" ;
for ( $i = 1; $i <= $len ; $i ++)
$res .= chr ( $i + 48);
for ( $i = $len - 1; $i >= 1; $i --)
$res .= chr ( $i + 48);
return $res ;
}
$str = "111111" ;
echo printDemlo( $str );
?>
|
Javascript
<script>
function printDemlo(str)
{
let len = str.length;
let res = "" ;
for (let i = 1; i <= len; i++)
res += i.toString();
for (let i = len - 1; i >= 1; i--)
res += i.toString();
return res;
}
let str = "111111" ;
document.write(printDemlo(str));
</script>
|
Output:
12345654321
Time complexity: O(log N) where N is the length of string “str” of number n
Auxiliary space: O(1) because it is using constant space
Last Updated :
18 Sep, 2023
Like Article
Save Article
Share your thoughts in the comments
Please Login to comment...