Sum of all Perfect Cubes lying in the range [L, R] for Q queries
Last Updated :
21 Dec, 2021
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 cubes lying in this range.
Examples:
Input: Q = 2, arr[][] = {{4, 9}, {4, 44}}
Output: 8 35
From 4 to 9: only 8 is the perfect cube. Therefore, 8 is the ans
From 4 to 44: 8, and 27 are the perfect cubes. Therefore, 8 + 27 = 35
Input: Q = 4, arr[][] = {{ 1, 10 }, { 1, 100 }, { 2, 25 }, { 4, 50 }}
Output: 9 100 8 35
Approach: The idea is to use a prefix sum array.
- The sum all cubes are precomputed and stored in an array pref[] so that every query can be answered in O(1) time.
- Every ith index in the pref[] array represents the sum of perfect cubes from 1 to that number.
- Therefore, the sum of perfect cubes from the given range ‘L’ to ‘R’ can be found as from the prefix sum array pref[].
Below is the implementation of the above approach:
C++
#include <bits/stdc++.h>
#define ll int
using namespace std;
long long pref[100010];
int isPerfectCube( long long int x)
{
long double cr = round(cbrt(x));
return (cr * cr * cr == x) ? x : 0;
}
void compute()
{
for ( int i = 1; i <= 100000; ++i) {
pref[i] = pref[i - 1]
+ isPerfectCube(i);
}
}
void printSum( int L, int R)
{
int sum = pref[R] - pref[L - 1];
cout << sum << " " ;
}
int main()
{
compute();
int Q = 4;
int arr[][2] = { { 1, 10 },
{ 1, 100 },
{ 2, 25 },
{ 4, 50 } };
for ( int i = 0; i < Q; i++) {
printSum(arr[i][0], arr[i][1]);
}
return 0;
}
|
Java
import java.util.*;
import java.lang.*;
import java.io.*;
class GFG
{
public static int []pref= new int [ 100010 ];
static int isPerfectCube( int x)
{
double cr = Math.round(Math.cbrt(x));
if (cr*cr*cr==( double )x) return x;
return 0 ;
}
static void compute()
{
for ( int i = 1 ; i <= 100000 ; ++i) {
pref[i] = pref[i - 1 ]+ isPerfectCube(i);
}
}
static void printSum( int L, int R)
{
long sum = pref[R] - pref[L - 1 ];
System.out.print(sum+ " " );
}
public static void main (String[] args)
{
compute();
int Q = 4 ;
int [][] arr = { { 1 , 10 },
{ 1 , 100 },
{ 2 , 25 },
{ 4 , 50 } };
for ( int i = 0 ; i < Q; i++) {
printSum(arr[i][ 0 ], arr[i][ 1 ]);
}
}
}
|
Python3
pref = [ 0 ] * 100010 ;
def isPerfectCube(x) :
cr = round (x * * ( 1 / 3 ));
rslt = x if (cr * cr * cr = = x) else 0 ;
return rslt;
def compute() :
for i in range ( 1 , 100001 ) :
pref[i] = pref[i - 1 ] + isPerfectCube(i);
def printSum(L, R) :
sum = pref[R] - pref[L - 1 ];
print ( sum ,end = " " );
if __name__ = = "__main__" :
compute();
Q = 4 ;
arr = [ [ 1 , 10 ],
[ 1 , 100 ],
[ 2 , 25 ],
[ 4 , 50 ] ];
for i in range (Q) :
printSum(arr[i][ 0 ], arr[i][ 1 ]);
|
C#
using System;
class GFG {
public static long []pref= new long [100010];
static long isPerfectCube( long x)
{
double cr = Math.Round(MathF.Cbrt(x));
if (cr*cr*cr==( double )x) return x;
return 0;
}
static void compute()
{
for ( long i = 1; i <= 100000; ++i) {
pref[i] = pref[i - 1]
+ isPerfectCube(i);
}
}
static void printSum( int L, int R)
{
long sum = pref[R] - pref[L - 1];
Console.Write(sum+ " " );
}
public static void Main()
{
compute();
int Q = 4;
int [,] arr = new int [,]{ { 1, 10 },
{ 1, 100 },
{ 2, 25 },
{ 4, 50 } };
for ( int i = 0; i < Q; i++) {
printSum(arr[i,0], arr[i,1]);
}
}
}
|
Javascript
<script>
var pref=Array(100010).fill(0);
function isPerfectCube(x)
{
var cr = Math.round(Math.cbrt(x));
return (cr * cr * cr == x) ? x : 0;
}
function compute()
{
for ( var i = 1; i <= 100000; ++i) {
pref[i] = pref[i - 1]
+ isPerfectCube(i);
}
}
function printSum(L, R)
{
var sum = pref[R] - pref[L - 1];
document.write(sum + " " );
}
compute();
var Q = 4;
var arr = [ [ 1, 10 ],
[ 1, 100 ],
[ 2, 25 ],
[ 4, 50 ] ];
for ( var i = 0; i < Q; i++) {
printSum(arr[i][0], arr[i][1]);
}
</script>
|
Time Complexity: O(100000)
Auxiliary Space: O(100010)
Like Article
Suggest improvement
Share your thoughts in the comments
Please Login to comment...