Given Q queries in the form of 2D array arr[][] whose every row consists of two numbers L and R which signifies the range [L, R], the task is to find the sum of all perfect squares lying in this range.
Examples:
Input: Q = 2, arr[][] = {{4, 9}, {4, 16}}
Output: 13 29
Explanation:
From 4 to 9: only 4 and 9 are perfect squares. Therefore, 4 + 9 = 13.
From 4 to 16: 4, 9 and 16 are the perfect squares. Therefore, 4 + 9 + 16 = 29.Input: Q = 4, arr[][] = {{1, 10}, {1, 100}, {2, 25}, {4, 50}}
Output: 14 385 54 139
Approach: The idea is to use a prefix sum array. The sum all squares are precomputed and stored in an array pref[] so that every query can be answered in O(1) time. Every ‘i’th index in the pref[] array represents the sum of perfect squares from 1 to that number. Therefore, the sum of perfect squares from the given range ‘L’ to ‘R’ can be found as follows:
sum = pref[R] - pref[L - 1]
Below is the implementation of the above approach:
CPP
// C++ program to find the sum of all // perfect squares in the given range #include <bits/stdc++.h> #define ll int using namespace std; // Array to precompute the sum of squares // from 1 to 100010 so that for every // query, the answer can be returned in O(1). long long pref[100010]; // Function to check if a number is // a perfect square or not int isPerfectSquare(long long int x) { // Find floating point value of // square root of x. long double sr = sqrt(x); // If square root is an integer return ((sr - floor(sr)) == 0) ? x : 0; } // Function to precompute the perfect // squares upto 100000. void compute() { for (int i = 1; i <= 100000; ++i) { pref[i] = pref[i - 1] + isPerfectSquare(i); } } // Function to print the sum for each query void printSum(int L, int R) { int sum = pref[R] - pref[L - 1]; cout << sum << " "; } // Driver code int main() { // To calculate the precompute function compute(); int Q = 4; int arr[][2] = { { 1, 10 }, { 1, 100 }, { 2, 25 }, { 4, 50 } }; // Calling the printSum function // for every query for (int i = 0; i < Q; i++) { printSum(arr[i][0], arr[i][1]); } return 0; } |
Java
// Java program to find the sum of all // perfect squares in the given range class GFG { // Array to precompute the sum of squares // from 1 to 100010 so that for every // query, the answer can be returned in O(1). static int []pref = new int[100010]; // Function to check if a number is // a perfect square or not static int isPerfectSquare(int x) { // Find floating point value of // square root of x. double sr = Math.sqrt(x); // If square root is an integer return ((sr - Math.floor(sr)) == 0) ? x : 0; } // Function to precompute the perfect // squares upto 100000. static void compute() { for (int i = 1; i <= 100000; ++i) { pref[i] = pref[i - 1] + isPerfectSquare(i); } } // Function to print the sum for each query static void printSum(int L, int R) { int sum = pref[R] - pref[L - 1]; System.out.print(sum+ " "); } // Driver code public static void main(String[] args) { // To calculate the precompute function compute(); int Q = 4; int arr[][] = { { 1, 10 }, { 1, 100 }, { 2, 25 }, { 4, 50 } }; // Calling the printSum function // for every query for (int i = 0; i < Q; i++) { printSum(arr[i][0], arr[i][1]); } } } // This code is contributed by PrinciRaj1992 |
Python3
# Python3 program to find the sum of all # perfect squares in the given range from math import sqrt, floor # Array to precompute the sum of squares # from 1 to 100010 so that for every # query, the answer can be returned in O(1). pref = [0]*100010; # Function to check if a number is # a perfect square or not def isPerfectSquare(x) : # Find floating point value of # square root of x. sr = sqrt(x); # If square root is an integer rslt = x if (sr - floor(sr) == 0) else 0; return rslt; # Function to precompute the perfect # squares upto 100000. def compute() : for i in range(1 , 100001) : pref[i] = pref[i - 1] + isPerfectSquare(i); # Function to print the sum for each query def printSum( L, R) : sum = pref[R] - pref[L - 1]; print(sum ,end= " "); # Driver code if __name__ == "__main__" : # To calculate the precompute function compute(); Q = 4; arr = [ [ 1, 10 ], [ 1, 100 ], [ 2, 25 ], [ 4, 50 ] ]; # Calling the printSum function # for every query for i in range(Q) : printSum(arr[i][0], arr[i][1]); # This code is contributed by AnkitRai01 |
C#
// C# program to find the sum of all // perfect squares in the given range using System; class GFG { // Array to precompute the sum of squares // from 1 to 100010 so that for every // query, the answer can be returned in O(1). static int []pref = new int[100010]; // Function to check if a number is // a perfect square or not static int isPerfectSquare(int x) { // Find floating point value of // square root of x. double sr = Math.Sqrt(x); // If square root is an integer return ((sr - Math.Floor(sr)) == 0) ? x : 0; } // Function to precompute the perfect // squares upto 100000. static void compute() { for (int i = 1; i <= 100000; ++i) { pref[i] = pref[i - 1] + isPerfectSquare(i); } } // Function to print the sum for each query static void printSum(int L, int R) { int sum = pref[R] - pref[L - 1]; Console.Write(sum+ " "); } // Driver code public static void Main(String[] args) { // To calculate the precompute function compute(); int Q = 4; int [,]arr = { { 1, 10 }, { 1, 100 }, { 2, 25 }, { 4, 50 } }; // Calling the printSum function // for every query for (int i = 0; i < Q; i++) { printSum(arr[i, 0], arr[i, 1]); } } } // This code is contributed by PrinciRaj1992 |
14 385 54 139
Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the DSA Self Paced Course at a student-friendly price and become industry ready.
Recommended Posts:
- Sum of all Perfect Cubes lying in the range [L, R] for Q queries
- Sum of all Perfect numbers lying in the range [L, R]
- Sum of all palindromic numbers lying in the range [L, R] for Q queries
- Sum of all armstrong numbers lying in the range [L, R] for Q queries
- Sum of all composite numbers lying in the range [L, R] for Q queries
- Range Queries to count elements lying in a given Range : MO's Algorithm
- Sum of product of proper divisors of all Numbers lying in range [L, R]
- Print all perfect squares from the given range
- Count of primes in a given range that can be expressed as sum of perfect squares
- Array range queries over range queries
- Sum of distances between the two nearest perfect squares to all the nodes of the given linked list
- Maximum count of values of S modulo M lying in a range [L, R] after performing given operations on the array
- Largest sub-array whose all elements are perfect squares
- Largest number from the longest set of anagrams possible from all perfect squares of length K
- Pair having all other given pairs lying between its minimum and maximum
- Array range queries to find the number of perfect square elements with updates
- Check if the sum of perfect squares in an array is divisible by x
- Check if a number is a perfect square having all its digits as a perfect square
- Number of perfect squares between two given numbers
- Count number less than N which are product of perfect squares
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.

