Sum of nth terms of Modified Fibonacci series made by every pair of two arrays
Given two arrays A and B of same size m. You have to find the sum of nth terms of Fibonacci like series (value of every term is sum of previous two terms) formed by every element of A as first and every element of B as second.
Examples:
Input : {1, 2, 3}, {4, 5, 6}, n = 3
Output : 63
Explanation :
A[] = {1, 2, 3};
B[] = {4, 5, 6};
n = 3;
All the possible series upto 3rd terms are:
We have considered every possible pair of A
and B and generated third term using sum of
previous two terms.
1, 4, 5
1, 5, 6
1, 6, 7
2, 4, 6
2, 5, 7
2, 6, 8
3, 4, 7
3, 5, 8
3, 6, 9
sum = 5+6+7+6+7+8+7+8+9 = 63
Input : {5, 8, 10}, {6, 89, 5}
Output : 369
The naive approach is to take every pair of the array A and B and make a Fibonacci series with them.
An efficient approach is based on below idea.
Store the original Fibonacci series in an array and multiply the first term by original_fib[n-2] and second term by original_fib[n-1].
Every element of array A, as well as B, will come m times so multiply them by m.
(m * (B[i] * original_fib[n-1]) ) + (m * (A[i] * original_fib[n-2]) )
By using the efficient method it can be written as
original_fib[]={0, 1, 1, 2, 3, 5, 8};
A[] = {1, 2, 3};
B[] = {4, 5, 6};
n = 3;
for (i to m)
sum = sum + 3*(B[i]*original_fib[2]) + 3*(A[i]*original_fib[1])
Below is the implementation of above approach:
C++
#include <bits/stdc++.h>
using namespace std;
int sumNth( int A[], int B[], int m, int n)
{
int res = 0;
if (n == 1) {
for ( int i = 0; i < m; i++)
res = res + A[i];
}
else if (n == 2) {
for ( int i = 0; i < m; i++)
res = res + B[i] * m;
}
else {
int f[n];
f[0] = 0, f[1] = 1;
for ( int i = 2; i < n; i++)
f[i] = f[i - 1] + f[i - 2];
for ( int i = 0; i < m; i++) {
res = res + (m * (B[i] * f[n - 1])) +
(m * (A[i] * f[n - 2]));
}
}
return res;
}
int main()
{
int A[] = { 1, 2, 3 };
int B[] = { 4, 5, 6 };
int n = 3;
int m = sizeof (A)/ sizeof (A[0]);
cout << sumNth(A, B, m, n);
return 0;
}
|
Java
public class GFG {
static int sumNth( int A[], int B[], int m, int n)
{
int res = 0 ;
if (n == 1 ) {
for ( int i = 0 ; i < m; i++)
res = res + A[i];
}
else if (n == 2 ) {
for ( int i = 0 ; i < m; i++)
res = res + B[i] * m;
}
else {
int f[] = new int [n];
f[ 0 ] = 0 ;
f[ 1 ] = 1 ;
for ( int i = 2 ; i < n; i++)
f[i] = f[i - 1 ] + f[i - 2 ];
for ( int i = 0 ; i < m; i++) {
res = res + (m * (B[i] * f[n - 1 ])) +
(m * (A[i] * f[n - 2 ]));
}
}
return res;
}
public static void main(String args[])
{
int A[] = { 1 , 2 , 3 };
int B[] = { 4 , 5 , 6 };
int n = 3 ;
int m = A.length;
System.out.println(sumNth(A, B, m, n));
}
}
|
Python3
def sumNth(A, B, m, n):
res = 0 ;
if (n = = 1 ):
for i in range (m):
res = res + A[i];
elif (n = = 2 ):
for i in range (m):
res = res + B[i] * m;
else :
f = [ 0 ] * n;
f[ 0 ] = 0 ;
f[ 1 ] = 1 ;
for i in range ( 2 , n):
f[i] = f[i - 1 ] + f[i - 2 ];
for i in range (m):
res = (res + (m * (B[i] * f[n - 1 ])) +
(m * (A[i] * f[n - 2 ])));
return res;
A = [ 1 , 2 , 3 ];
B = [ 4 , 5 , 6 ];
n = 3 ;
m = len (A);
print (sumNth(A, B, m, n));
|
C#
using System;
class GFG
{
static int sumNth( int [] A, int [] B,
int m, int n)
{
int res = 0;
if (n == 1)
{
for ( int i = 0; i < m; i++)
res = res + A[i];
}
else if (n == 2)
{
for ( int i = 0; i < m; i++)
res = res + B[i] * m;
}
else
{
int [] f = new int [n];
f[0] = 0;
f[1] = 1;
for ( int i = 2; i < n; i++)
f[i] = f[i - 1] + f[i - 2];
for ( int i = 0; i < m; i++)
{
res = res + (m * (B[i] * f[n - 1])) +
(m * (A[i] * f[n - 2]));
}
}
return res;
}
public static void Main(String[] args)
{
int [] A = { 1, 2, 3 };
int [] B = { 4, 5, 6 };
int n = 3;
int m = A.Length;
Console.WriteLine(sumNth(A, B, m, n));
}
}
|
PHP
<?php
function sumNth(& $A , & $B , & $m , & $n )
{
$res = 0;
if ( $n == 1)
{
for ( $i = 0; $i < $m ; $i ++)
$res = $res + $A [ $i ];
}
else if ( $n == 2)
{
for ( $i = 0; $i < $m ; $i ++)
$res = $res + $B [ $i ] * $m ;
}
else
{
$f = array ();
$f [0] = 0;
$f [1] = 1;
for ( $i = 2; $i < $n ; $i ++)
$f [ $i ] = $f [ $i - 1] + $f [ $i - 2];
for ( $i = 0; $i < $m ; $i ++)
{
$res = $res + ( $m * ( $B [ $i ] * $f [ $n - 1])) +
( $m * ( $A [ $i ] * $f [ $n - 2]));
}
}
return $res ;
}
$A = array (1, 2, 3 );
$B = array (4, 5, 6 );
$n = 3;
$m = sizeof( $A );
echo (sumNth( $A , $B , $m , $n ));
?>
|
Javascript
<script>
function sumNth(A , B , m , n)
{
var res = 0;
if (n == 1)
{
for (let i = 0; i < m; i++)
res = res + A[i];
}
else if (n == 2)
{
for (let i = 0; i < m; i++)
res = res + B[i] * m;
}
else
{
var f = Array(n).fill(0);
f[0] = 0;
f[1] = 1;
for (let i = 2; i < n; i++)
f[i] = f[i - 1] + f[i - 2];
for (i = 0; i < m; i++)
{
res = res + (m * (B[i] * f[n - 1])) + (m * (A[i] * f[n - 2]));
}
}
return res;
}
var A = [ 1, 2, 3 ];
var B = [ 4, 5, 6 ];
var n = 3;
var m = A.length;
document.write(sumNth(A, B, m, n));
</script>
|
Time Complexity: O(M + N), where M and N represents the size of the given two arrays.
Auxiliary Space: O(N), where N represents the size of the given array.
Last Updated :
15 Mar, 2023
Like Article
Save Article
Share your thoughts in the comments
Please Login to comment...