Count of numbers in given range [L, R] that is perfect square and digits are in wave form
Last Updated :
12 Jan, 2022
Given two integers L and R, the task is to count the integers in the range [L, R] such that they satisfy the following two properties:
Examples:
Input: L = 1, R = 64
Output: 7
Explanation:
The special numbers in the range [1, 64] are 1, 4, 9, 16, 25, 36 and 49.
Input: L = 80, R = 82
Output: 0
Naive Approach: The simplest approach to solve the problem is to traverse in the range [L, R], and for each number in the range check for the above two conditions.
Time Complexity: O(N)
Auxiliary Space: O(1)
Efficient Approach: To optimize the above approach iterate over only perfect squares and check for the second condition. Follow the steps below for the approach:
- Initialize a variable say, count = 0, to count all the special numbers in the range [L, R].
- Iterate over all the perfect squares that are less than R.
- Define a function, say check(N), to check whether the number N satisfies the second condition by iterating over even and odd digits.
- Increase the count, if the number is greater than L and function check returns true for the given number.
- Finally, return the count.
Below is the implementation of the above approach:
C++
#include <bits/stdc++.h>
using namespace std;
bool check( int N)
{
string S = to_string(N);
for ( int i = 0; i < S.size(); i++) {
if (i == 0) {
int next = i + 1;
if (next < S.size()) {
if (S[i] >= S[next]) {
return false ;
}
}
}
else if (i == S.size() - 1) {
int prev = i - 1;
if (prev >= 0) {
if (i & 1) {
if (S[i] <= S[prev]) {
return false ;
}
}
else {
if (S[i] >= S[prev]) {
return false ;
}
}
}
}
else {
int prev = i - 1;
int next = i + 1;
if (i & 1) {
if ((S[i] > S[prev])
&& (S[i] > S[next])) {
}
else {
return false ;
}
}
else {
if ((S[i] < S[prev])
&& (S[i] < S[next])) {
}
else {
return false ;
}
}
}
}
return true ;
}
int totalUniqueNumber( int L, int R)
{
if (R <= 0) {
return 0;
}
int cur = 1;
int count = 0;
while ((cur * cur) <= R) {
int num = cur * cur;
if (num >= L && check(num)) {
count++;
}
cur++;
}
return count;
}
int main()
{
int L = 1, R = 64;
cout << totalUniqueNumber(L, R);
}
|
Java
import java.util.*;
public class GFG
{
static boolean check( int N)
{
String S = Integer.toString(N);
for ( int i = 0 ; i < S.length(); i++) {
if (i == 0 ) {
int next = i + 1 ;
if (next < S.length()) {
if (S.charAt(i) >= S.charAt(next)) {
return false ;
}
}
}
else if (i == S.length() - 1 ) {
int prev = i - 1 ;
if (prev >= 0 ) {
if ((i & 1 ) == 1 ) {
if (S.charAt(i) <= S.charAt(prev)) {
return false ;
}
}
else
{
if (S.charAt(i) >= S.charAt(prev)) {
return false ;
}
}
}
}
else {
int prev = i - 1 ;
int next = i + 1 ;
if ((i & 1 ) == 1 ) {
if ((S.charAt(i) > S.charAt(prev))
&& (S.charAt(i) > S.charAt(next))) {
}
else {
return false ;
}
}
else
{
if ((S.charAt(i) < S.charAt(prev))
&& (S.charAt(i) < S.charAt(next))) {
}
else {
return false ;
}
}
}
}
return true ;
}
static int totalUniqueNumber( int L, int R)
{
if (R <= 0 ) {
return 0 ;
}
int cur = 1 ;
int count = 0 ;
while ((cur * cur) <= R) {
int num = cur * cur;
if (num >= L && check(num)) {
count++;
}
cur++;
}
return count;
}
public static void main(String args[])
{
int L = 1 , R = 64 ;
System.out.println(totalUniqueNumber(L, R));
}
}
|
Python3
def check(N):
S = str (N);
for i in range ( len (S)):
if (i = = 0 ):
next = i + 1 ;
if ( next < len (S)):
if (S[i] > = S[ next ]):
return False ;
elif (i = = len (S) - 1 ):
prev = i - 1 ;
if (prev > = 0 ):
if ((i & 1 ) = = 1 ):
if (S[i] < = S[prev]):
return False ;
else :
if (S[i] > = S[prev]):
return False ;
else :
prev = i - 1 ;
next = i + 1 ;
if ((i & 1 ) = = 1 ):
if ((S[i] > S[prev]) and (S[i] > S[ next ])):
return True ;
else :
return False ;
else :
if ((S[i] < S[prev]) and (S[i] < S[ next ])):
return True ;
else :
return False ;
return True ;
def totalUniqueNumber(L, R):
if (R < = 0 ):
return 0 ;
cur = 1 ;
count = 0 ;
while ((cur * cur) < = R):
num = cur * cur;
if (num > = L and check(num)):
count + = 1 ;
cur + = 1 ;
return count;
if __name__ = = '__main__' :
L = 1 ;
R = 64 ;
print (totalUniqueNumber(L, R));
|
C#
using System;
using System.Collections;
class GFG
{
static bool check( int N)
{
string S = N.ToString();
for ( int i = 0; i < S.Length; i++) {
if (i == 0) {
int next = i + 1;
if (next < S.Length) {
if (S[i] >= S[next]) {
return false ;
}
}
}
else if (i == S.Length - 1) {
int prev = i - 1;
if (prev >= 0) {
if ((i & 1) == 1) {
if (S[i] <= S[prev]) {
return false ;
}
}
else {
if (S[i] >= S[prev]) {
return false ;
}
}
}
}
else {
int prev = i - 1;
int next = i + 1;
if ((i & 1) == 1) {
if ((S[i] > S[prev])
&& (S[i] > S[next])) {
}
else {
return false ;
}
}
else {
if ((S[i] < S[prev])
&& (S[i] < S[next])) {
}
else {
return false ;
}
}
}
}
return true ;
}
static int totalUniqueNumber( int L, int R)
{
if (R <= 0) {
return 0;
}
int cur = 1;
int count = 0;
while ((cur * cur) <= R) {
int num = cur * cur;
if (num >= L && check(num)) {
count++;
}
cur++;
}
return count;
}
public static void Main()
{
int L = 1, R = 64;
Console.Write(totalUniqueNumber(L, R));
}
}
|
Javascript
<script>
function check(N)
{
let S = N.toString();
for (let i = 0; i < S.length; i++) {
if (i == 0) {
let next = i + 1;
if (next < S.length) {
if (S[i] >= S[next]) {
return false ;
}
}
}
else if (i == S.length - 1) {
let prev = i - 1;
if (prev >= 0) {
if (i & 1) {
if (S[i] <= S[prev]) {
return false ;
}
}
else {
if (S[i] >= S[prev]) {
return false ;
}
}
}
}
else {
let prev = i - 1;
let next = i + 1;
if (i & 1) {
if ((S[i] > S[prev])
&& (S[i] > S[next])) {
}
else {
return false ;
}
}
else {
if ((S[i] < S[prev])
&& (S[i] < S[next])) {
}
else {
return false ;
}
}
}
}
return true ;
}
function totalUniqueNumber(L, R) {
if (R <= 0) {
return 0;
}
let cur = 1;
let count = 0;
while ((cur * cur) <= R) {
let num = cur * cur;
if (num >= L && check(num)) {
count++;
}
cur++;
}
return count;
}
let L = 1, R = 64;
document.write(totalUniqueNumber(L, R));
</script>
|
Time Complexity: O(sqrt(N))
Auxiliary Space: O(1)
Like Article
Suggest improvement
Share your thoughts in the comments
Please Login to comment...