1.
Introduction
2.
Problem statement
3.
Approaches
4.
Code
5.
Complexity analysis
6.
FAQs
7.
Key Takeaways
Last Updated: Mar 27, 2024

# Count of Numbers in Range [L, R] having Sum of Digits of its Square Equal to Square of Sum of Digits

Malay Gain
1 upvote

## Introduction

Finding the sum of digits of a number can be done easily. Here we will be discussing a problem where the concept of finding the sum of digits will be mainly used. The concept of the modulo operator is required here. First, we will understand the problem statement clearly. Then solution approach will be described along with its C++ implementation.

## Problem statement

You are given a range of integers [L, R]. Form the given range of integers, you need to find how many integers have the sum of digits of its square equal to the square of the sum of digits.

Input

``L=1, R=20``

Output

``8``

Explanation

``````For the range [1, 20], the numbers that are satisfying the above condition are 1, 2, 3, 10, 11, 12, 13, 20.
So, the count of such numbers is 8.``````

Note: Please try to solve the problem first and then see the below solution approach.

Also see, Euclid GCD Algorithm

## Approaches

We will take the brute force approach to solve this problem.

• We will iterate over all integers of the range [L, R].
• For each integer, we will check if it is having the sum of digits of its square equal to the square of the sum of digits.
• If the condition is satisfied, we will increment the count of such numbers.
• To find the sum of digits of a number we will define a separate function sumofDigits( ) that will use the modulo operator to find digits of the number.

Output

## Complexity analysis

The Time Complexity of the above approach is O((R - L)*logR).

Space complexity is O(1).

## FAQs

1. What is modulo operator?
It is an arithmetic operator which is used to find the remainder of any integer division.

2. What is the left shift(<<) operator in C++?
The left shift operator takes two integers and shifts bits of the first integer towards the left by no number of places equal to the second integer.
E.g  (5<<1) = 10=(1010)2

## Key Takeaways

This article covered how to count numbers in the range [L, R] having the sum of digits of its square equal to the square of the sum of digits.
Check out this problem - Subarray Sum Divisible By K

Check out the Coding Ninjas Studio library for getting a better hold of the data structures and algorithms.

Side by side, you can also practice a wide variety of coding questions commonly asked in interviews in Coding Ninjas Studio. Along with coding questions, you can also find the interview experience of scholars working in renowned product-based companies here.

Happy learning!

Live masterclass