Consider a sequence a0, a1, …, an, where ai = ai-1 + ai-2. Given a0, a1, and a positive integer n. The task is to find whether an is odd or even.
Note that the given sequence is like Fibonacci with the difference that the first two terms can be anything instead of 0 or 1.
Examples :
Input : a0 = 2, a1 = 4, n =3
Output : Even
a2 = 6, a3 = 10
And a3 is even.
Input : a0 = 1, a1 = 9, n = 2
Output : Even
Method 1: The idea is to find the sequence using the array and check if nth element is even or odd.
C++
#include <bits/stdc++.h>
using namespace std;
#define MAX 100
bool findNature( int a, int b, int n)
{
int seq[MAX] = { 0 };
seq[0] = a;
seq[1] = b;
for ( int i = 2; i <= n; i++)
seq[i] = seq[i - 1] + seq[i - 2];
return (seq[n] & 1);
}
int main()
{
int a = 2, b = 4;
int n = 3;
(findNature(a, b, n) ? (cout << "Odd"
<< " " )
: (cout << "Even"
<< " " ));
return 0;
}
|
Java
class GFG
{
public static int findNature( int a,
int b, int n)
{
int [] seq = new int [ 100 ];
seq[ 0 ] = a;
seq[ 1 ] = b;
for ( int i = 2 ; i <= n; i++)
seq[i] = seq[i - 1 ] + seq[i - 2 ];
if ((seq[n] & 1 ) != 0 )
return 1 ;
else
return 0 ;
}
public static void main(String[] args)
{
int a = 2 , b = 4 ;
int n = 3 ;
if (findNature(a, b, n) == 1 )
System.out.println( "Odd " );
else
System.out.println( "Even " );
}
}
|
Python3
MAX = 100 ;
def findNature(a, b, n):
seq = [ 0 ] * MAX ;
seq[ 0 ] = a;
seq[ 1 ] = b;
for i in range ( 2 , n + 1 ):
seq[i] = seq[i - 1 ] + seq[i - 2 ];
return (seq[n] & 1 );
a = 2 ;
b = 4 ;
n = 3 ;
if (findNature(a, b, n)):
print ( "Odd" );
else :
print ( "Even" );
|
C#
using System;
class GFG
{
public static int findNature( int a,
int b, int n)
{
int [] seq = new int [100];
seq[0] = a;
seq[1] = b;
for ( int i = 2; i <= n; i++)
seq[i] = seq[i - 1] +
seq[i - 2];
if ((seq[n] & 1)!=0)
return 1;
else
return 0;
}
public static void Main()
{
int a = 2, b = 4;
int n = 3;
if (findNature(a, b, n) == 1)
Console.Write( "Odd " );
else
Console.Write( "Even " );
}
}
|
PHP
<?php
$MAX = 100;
function findNature( $a , $b , $n )
{
global $MAX ;
$seq = array_fill (0, $MAX , 0);
$seq [0] = $a ;
$seq [1] = $b ;
for ( $i = 2; $i <= $n ; $i ++)
$seq [ $i ] = $seq [ $i - 1] +
$seq [ $i - 2];
return ( $seq [ $n ] & 1);
}
$a = 2;
$b = 4;
$n = 3;
if (findNature( $a , $b , $n ))
echo "Odd" ;
else
echo "Even" ;
?>
|
Javascript
<script>
var MAX = 100;
function findNature(a, b, n)
{
var seq = Array(MAX).fill(0);
seq[0] = a;
seq[1] = b;
for ( var i = 2; i <= n; i++)
seq[i] = seq[i - 1] + seq[i - 2];
return (seq[n] & 1);
}
var a = 2, b = 4;
var n = 3;
(findNature(a, b, n) ? (document.write( "Odd"
+ " " ))
: (document.write( "Even"
+ " " )));
</script>
|
Method 2 (efficient) :
Observe, the nature (odd or even) of the nth term depends on the previous terms, and the nature of the previous term depends on their previous terms and finally depends on the initial value i.e a0 and a1.
So, we have four possible scenarios for a0 and a1:
Case 1: When a0 an a1 is even
In this case each of the value in the sequence will be even only.
Case 2: When a0 an a1 is odd
In this case, observe a2 is even, a3 is odd, a4 is odd and so on. So, we can say ai is even if i is of form 3*k – 1, else odd.
Case 3: When a0 is even and a1 is odd
In this case, observe a2 is odd, a3 is even, a4 and a5 is odd, a6 is even and so on. So, we can say, ai is even if i is multiple of 3, else odd
Case 4: When a0 is odd and a1 is even
In this case, observe a2 and a3 is odd, a4 is even, a5 and a6 is odd, a7 is even and so on. So, we can say, ai is even if i is of the form 3*k + 1, k >= 0, else odd.
Below is the implementation of this approach:
C++
#include <bits/stdc++.h>
using namespace std;
bool findNature( int a, int b, int n)
{
if (n == 0)
return (a & 1);
if (n == 1)
return (b & 1);
if (!(a & 1)) {
if (!(b & 1))
return false ;
else
return (n % 3 != 0);
}
else {
if (!(b & 1))
return ((n - 1) % 3 != 0);
else
return ((n + 1) % 3 != 0);
}
}
int main()
{
int a = 2, b = 4;
int n = 3;
(findNature(a, b, n) ? (cout << "Odd"
<< " " )
: (cout << "Even"
<< " " ));
return 0;
}
|
Java
class GFG{
static boolean findNature( int a, int b, int n)
{
if (n == 0 )
return (a & 1 )== 1 ? true : false ;
if (n == 1 )
return (b & 1 )== 1 ? true : false ;
if ((a & 1 )== 0 ) {
if ((b & 1 )== 0 )
return false ;
else
return (n % 3 != 0 );
}
else {
if ((b & 1 )== 0 )
return ((n - 1 ) % 3 != 0 );
else
return ((n + 1 ) % 3 != 0 );
}
}
public static void main(String[] args)
{
int a = 2 , b = 4 ;
int n = 3 ;
if (findNature(a, b, n))
System.out.println( "Odd" );
else
System.out.println( "Even" );
}
}
|
Python3
def findNature(a, b, n):
if (n = = 0 ):
return (a & 1 );
if (n = = 1 ):
return (b & 1 );
if ((a & 1 ) = = 0 ):
if ((b & 1 ) = = 0 ):
return False ;
else :
return True if (n % 3 ! = 0 ) else False ;
else :
if ((b & 1 ) = = 0 ):
return True if ((n - 1 ) % 3 ! = 0 ) else False ;
else :
return True if ((n + 1 ) % 3 ! = 0 ) else False ;
a = 2 ;
b = 4 ;
n = 3 ;
if (findNature(a, b, n) = = True ):
print ( "Odd" , end = " " );
else :
print ( "Even" , end = " " );
|
C#
class GFG{
static bool findNature( int a, int b, int n)
{
if (n == 0)
return (a & 1)==1? true : false ;
if (n == 1)
return (b & 1)==1? true : false ;
if ((a & 1)==0) {
if ((b & 1)==0)
return false ;
else
return (n % 3 != 0);
}
else {
if ((b & 1)==0)
return ((n - 1) % 3 != 0);
else
return ((n + 1) % 3 != 0);
}
}
static void Main()
{
int a = 2, b = 4;
int n = 3;
if (findNature(a, b, n))
System.Console.WriteLine( "Odd" );
else
System.Console.WriteLine( "Even" );
}
}
|
PHP
<?php
function findNature( $a , $b , $n )
{
if ( $n == 0)
return ( $a & 1);
if ( $n == 1)
return ( $b & 1);
if (!( $a & 1))
{
if (!( $b & 1))
return false;
else
return ( $n % 3 != 0);
}
else
{
if (!( $b & 1))
return (( $n - 1) % 3 != 0);
else
return (( $n + 1) % 3 != 0);
}
}
$a = 2; $b = 4;
$n = 3;
if (findNature( $a , $b , $n ) == true)
echo "Odd" , " " ;
else
echo "Even" , " " ;
?>
|
Javascript
<script>
function findNature(a, b, n)
{
if (n == 0)
return (a & 1)==1? true : false ;
if (n == 1)
return (b & 1)==1? true : false ;
if ((a & 1)==0) {
if ((b & 1)==0)
return false ;
else
return (n % 3 != 0);
}
else {
if ((b & 1)==0)
return ((n - 1) % 3 != 0);
else
return ((n + 1) % 3 != 0);
}
}
let a = 2, b = 4;
let n = 3;
if (findNature(a, b, n))
document.write( "Odd" );
else
document.write( "Even" );
</script>
|
Time Complexity: O(1)
Auxiliary Space: O(1)
Feeling lost in the world of random DSA topics, wasting time without progress? It’s time for a change! Join our DSA course, where we’ll guide you on an exciting journey to master DSA efficiently and on schedule.
Ready to dive in? Explore our Free Demo Content and join our DSA course, trusted by over 100,000 neveropen!