Sum of all Non-Fibonacci numbers in a range for Q queries
Given Q queries containing ranges in the form of [L, R], the task is to find the sum of all non-fibonacci numbers for each range in the given queries.
Examples:
Input: arr[][] = {{1, 5}, {6, 10}}
Output: 4 32
Explanation:
Query 1: In the range [1, 5], only 4 is a non-fibonacci number.
Query 2: In the range [6, 10], 6, 7, 9 and 10 are the non-fibonacci numbers.
Therefore, 6 + 7 + 9 + 10 = 32.
Input: arr[][] = {{10, 20}, {20, 50}}
Output: 152 10792
Approach: The idea is to use a prefix sum array. The sum of all non-fibonacci numbers is precomputed and stored in an array. So that every query can be answered in O(1) time. Every index of the array stores the sum of all non-fibonacci numbers from 1 to that index. So for finding the sum of all non-fibonacci numbers in a range can be computed as:
Let the precomputed array is stored in pref[] array
sum = pref[R] - pref[L - 1]
Below is the implementation of the above approach:
C++
#include <bits/stdc++.h>
#define ll int
using namespace std;
long long pref[100010];
bool isPerfectSquare( int x)
{
int s = sqrt (x);
return (s * s == x);
}
int isNonFibonacci( int n)
{
if (isPerfectSquare(5 * n * n + 4)
|| isPerfectSquare(5 * n * n - 4))
return 0;
else
return n;
}
void compute()
{
for ( int i = 1; i <= 100000; ++i) {
pref[i] = pref[i - 1]
+ isNonFibonacci(i);
}
}
void printSum( int L, int R)
{
int sum = pref[R] - pref[L - 1];
cout << sum << " " ;
}
int main()
{
compute();
int Q = 2;
int arr[][2] = { { 1, 5 },
{ 6, 10 } };
for ( int i = 0; i < Q; i++) {
printSum(arr[i][0], arr[i][1]);
}
return 0;
}
|
Java
import java.util.*;
class GFG
{
static long pref[] = new long [ 100010 ];
static boolean isPerfectSquare( int x)
{
int s =( int )Math.sqrt(x);
return (s * s == x);
}
static int isNonFibonacci( int n)
{
if (isPerfectSquare( 5 * n * n + 4 )
|| isPerfectSquare( 5 * n * n - 4 ))
return 0 ;
else
return n;
}
static void compute()
{
for ( int i = 1 ; i <= 100000 ; ++i) {
pref[i] = pref[i - 1 ]
+ isNonFibonacci(i);
}
}
static void printSum( int L, int R)
{
int sum = ( int )(pref[R] - pref[L - 1 ]);
System.out.print(sum + " " );
}
public static void main(String []args)
{
compute();
int Q = 2 ;
int arr[][] = { { 1 , 5 },
{ 6 , 10 } };
for ( int i = 0 ; i < Q; i++) {
printSum(arr[i][ 0 ], arr[i][ 1 ]);
}
}
}
|
Python3
from math import sqrt
pref = [ 0 ] * 100010
def isPerfectSquare(x):
s = int (sqrt(x))
if (s * s = = x):
return True
return False
def isNonFibonacci(n):
x = 5 * n * n
if (isPerfectSquare(x + 4 ) or isPerfectSquare(x - 4 )):
return 0
else :
return n
def compute():
for i in range ( 1 , 100001 ):
pref[i] = pref[i - 1 ] + isNonFibonacci(i)
def printSum(L, R):
sum = pref[R] - pref[L - 1 ]
print ( sum , end = " " )
compute()
Q = 2
arr = [[ 1 , 5 ],[ 6 , 10 ]]
for i in range (Q):
printSum(arr[i][ 0 ], arr[i][ 1 ])
|
C#
using System;
class GFG
{
static long []pref = new long [100010];
static bool isPerfectSquare( int x)
{
int s =( int )Math.Sqrt(x);
return (s * s == x);
}
static int isNonFibonacci( int n)
{
if (isPerfectSquare(5 * n * n + 4)
|| isPerfectSquare(5 * n * n - 4))
return 0;
else
return n;
}
static void compute()
{
for ( int i = 1; i <= 100000; ++i) {
pref[i] = pref[i - 1]
+ isNonFibonacci(i);
}
}
static void printSum( int L, int R)
{
int sum = ( int )(pref[R] - pref[L - 1]);
Console.Write(sum + " " );
}
public static void Main(String []args)
{
compute();
int Q = 2;
int [,]arr = { { 1, 5 },
{ 6, 10 } };
for ( int i = 0; i < Q; i++) {
printSum(arr[i,0], arr[i,1]);
}
}
}
|
Javascript
<script>
var pref = Array(100010).fill(0);
function isPerfectSquare(x)
{
var s = parseInt(Math.sqrt(x));
return (s * s == x);
}
function isNonFibonacci(n)
{
if (isPerfectSquare(5 * n * n + 4)
|| isPerfectSquare(5 * n * n - 4))
return 0;
else
return n;
}
function compute()
{
for ( var i = 1; i <= 100000; ++i) {
pref[i] = pref[i - 1]
+ isNonFibonacci(i);
}
}
function printSum(L, R)
{
var sum = pref[R] - pref[L - 1];
document.write(sum + " " );
}
compute();
var Q = 2;
var arr = [ [ 1, 5 ],
[ 6, 10 ] ];
for ( var i = 0; i < Q; i++) {
printSum(arr[i][0], arr[i][1]);
}
</script>
|
Performance Analysis:
- Time Complexity: As in the above approach, There is pre-computation which takes O(N) time and to answer each query it takes O(1) time.
- Auxiliary Space Complexity: As in the above approach, There is extra space used to precompute the sum of all non-fibonacci number. Hence the auxiliary space complexity will be O(N).
Last Updated :
10 Jan, 2022
Like Article
Save Article
Share your thoughts in the comments
Please Login to comment...