- Data Structures & Algorithms
- DSA - Home
- DSA - Overview
- DSA - Environment Setup
- Algorithm
- DSA - Algorithms Basics
- DSA - Asymptotic Analysis
- DSA - Greedy Algorithms
- DSA - Divide and Conquer
- DSA - Dynamic Programming
- Data Structures
- DSA - Data Structure Basics
- DSA - Data Structures and Types
- DSA - Array Data Structure
- Linked Lists
- DSA - Linked List Basics
- DSA - Doubly Linked List
- DSA - Circular Linked List
- Stack & Queue
- DSA - Stack
- DSA - Expression Parsing
- DSA - Queue
- Searching Techniques
- DSA - Linear Search
- DSA - Binary Search
- DSA - Interpolation Search
- DSA - Hash Table
- Sorting Techniques
- DSA - Sorting Algorithms
- DSA - Bubble Sort
- DSA - Insertion Sort
- DSA - Selection Sort
- DSA - Merge Sort
- DSA - Shell Sort
- DSA - Quick Sort
- Graph Data Structure
- DSA - Graph Data Structure
- DSA - Depth First Traversal
- DSA - Breadth First Traversal
- Tree Data Structure
- DSA - Tree Data Structure
- DSA - Tree Traversal
- DSA - Binary Search Tree
- DSA - AVL Tree
- DSA - Red Black Trees
- DSA - B Trees
- DSA - B+ Trees
- DSA - Splay Trees
- DSA - Spanning Tree
- DSA - Tries
- DSA - Heap
- Recursion
- DSA - Recursion Basics
- DSA - Tower of Hanoi
- DSA - Fibonacci Series
- DSA Useful Resources
- DSA - Questions and Answers
- DSA - Quick Guide
- DSA - Useful Resources
- DSA - Discussion

# Check if the n-th term is odd or even in a Fibonacci like sequence

Our task in this problem is to check if the n-th term of a fibonacci like sequence is odd or even. A fibonacci sequence is a type of sequence in mathematics where each number in the sequence is the sum of the preceding two numbers.

A nth term of the fibonacci sequence can be represented as −

$$\mathrm{Fn\:=\:F_{n-1}\:+\:F_{n-2}}$$

The first few numbers of the fibonacci sequence are **0, 1, 1, 2, 3, 5, 8, 13, 21, 34….**.

The first two numbers of the sequence are 0 and 1. The next numbers are the sum of the preceding two numbers as we can see in the sequence.

Similarly, we will be given a fibonacci like sequence in this problem where each number in the sequence will be equal to the sum of the preceding two numbers.

We will be given the first two terms of the fibonacci like sequence, let’s say and and a positive number N as input. We need to figure out that if the will be odd or even in this problem.

The Nth term of the fibonacci like sequence can be given by , which is the same as for the N-th fibonacci number because it follows the same pattern as the fibonacci sequence does.

Let’s understand the problem better with a few examples

**INPUT** : $\mathrm{x_0}$=2 , $\mathrm{x_1}$=4 , N=5

**OUTPUT** : EVEN

**Explanation** − Since the first two terms of the sequence are given i.e 2 and 4 in the input. To calculate N-th term i.e. 5th term of the sequence we need to calculate all the terms until 5. So the next terms of this sequence can be found using the formula for N-th term.

$$\mathrm{x_2\:=\:x_1\:+\:x_0\:=\:2\:+\:4\:=\:6}$$

$$\mathrm{x_3\:=\:x_2\:+\:x_1\:=\:6\:+\:4\:=\:10}$$

$$\mathrm{x_4\:=\:x_3\:+\:x_2\:=\:10\:+\:6\:=\:16}$$

$$\mathrm{x_5\:=\:x_4\:+\:x_3\:=\:16\:+\:10\:=\:26}$$

Since the 5th term of the sequence is 26 which is an even number

**INPUT** : $\mathrm{x_0}$=3 , $\mathrm{x_1}$=7 , N=4

**OUTPUT** : ODD

**Explanation** − The next terms of the fibonacci like sequence until 4 using formula for N-th fibonacci number are −

$$\mathrm{x_2\:=\:x_1\:+\:x_0\:=\:7\:+\:3\:=\:10}$$

$$\mathrm{x_3\:=\:x_2\:+\:x_1\:=\:10\:+\:7\:=\:17}$$

$$\mathrm{x_4\:=\:x_3\:+\:x_2\:=\:17\:+\:10\:=\:27}$$

The 4th number of the above sequence whose first two numbers are 3 and 7 is 27 which is an odd number.

Following the same, we need to find out if the N-th term of the fibonacci-like sequence is an even or odd number where we will be given the first two numbers of the sequence and the value of N as input.

Below are the approaches that we can follow to solve the above problem.

## Approach

### Approach-1 (using an array)

This is the most basic and the simplest approach to solve this problem. We will be finding every number of the sequence the same way as we did in the example inputs until N and store them in an array. We will then check for the N-th number if it is an even number or an odd and print the output accordingly.

The step-by-step illustration of the approach that we are going to follow −

We will create a function to check the N-th term of the sequence.

Create an array of maximum length using the MAX function in C++.

Store every number of the sequence in the array until N using a for loop.

Check for the N-th term in an array. If it is an even number, print even else print odd.

### Example

The implementation of the approach in C++ −

#include <iostream> #include <bits/stdc++.h> using namespace std; //function to find out if the N-th term of the sequence is an even or odd number void evenOrodd(int x, int y, int N){ int arr[N+1]={0}; //create an array of size 200 //store value of x and y at 0th and 1st index of array arr[0]=x; arr[1]=y; for(int i=2;i<=N;i++){ //iterate through array until N and update corresponding //value of the sequence at ith index arr[i]=arr[i-1]+arr[i-2]; //using the formula } cout<<N<<"th term of the fibonacci-like sequence is : "; //to check if it is an even or odd if(arr[N]%2==0){ cout<<"Even"<<endl; } else { cout<<"Odd"<<endl; } } int main(){ int x=3, y=8, N=10; evenOrodd(x,y,N); x=7, y=13, N=6; evenOrodd(x,y,N); return 0; }

### Output

10th term of the fibonacci-like sequence is : Even 6th term of the fibonacci-like sequence is : Odd

**Time Complexity : O(N),** since we iterate over the array till N.

**Space Complexity : O(N),** since we create an array of size N+1.

### Approach-2 (observing the pattern)

In this approach, we will try to learn the pattern behind the N-th term of the fibonacci like sequence. Since every term of the sequence is the sum of the preceding two terms, the N-th term will be even or odd depends on the preceding two terms. And their nature will depend on their preceding two terms. Therefore, the nature of the Nth term that it will be an even number or odd number will ultimately depend upon the first two terms.

There will be four cases in this approach −

1. Both the initial two terms are even.

2. Both the initial two terms are odd.

3. First term is even and the second term is odd.

4. First term is odd and the second term is even.

**Case 1**− In first case both the terms are even i.e. $\mathrm{x_0}$ and $\mathrm{x_1}$.In this case any number in the sequence will be an even number only as the sum of two even numbers always gives an even number.Example, if $\mathrm{x_0}$=2 and $\mathrm{x_1}$=4 then the sequence will be 2, 4, 6, 10, 16, 26, 42….

**Case 2**− For this case, both the initial terms will be odd numbers.The sum of two odd numbers will always give an even number. So the $\mathrm{x_2}$ will always be even in this case. Further $\mathrm{x_3}$ will be an odd number as sum of an even and an odd number will always give an odd number.Let’s understand the pattern with an example: $\mathrm{x_0}$=1 and $\mathrm{x_1}$=3. So the next terms of the sequence will be, $\mathrm{x_2}$=4, $\mathrm{x_3}$=7, $\mathrm{x_4}$=11, $\mathrm{x_5}$=18, $\mathrm{x_6}$=29, $\mathrm{x_7}$=47, $\mathrm{x_8}$=76…..Looking at the pattern, we can observe that an even number is continuously repeating at N=2,5,8 which can be expressed in the form 3*a-1, for any positive value of a.So in this case, $\mathrm{x_N}$ will be even N can be expressed as (3*a-1) for any positive value of a else $\mathrm{x_N}$ will be an odd number for all other cases.

**Case 3**− In this case, the first term will be even and the second term will be odd. The sum of any even and odd number always gives an odd number. The next term will even as the preceding two terms are odd numbers. Let’s try to understand the pattern of this case with an example, $\mathrm{x_0}$=2 and $\mathrm{x_1}$=3. The next few terms of the sequence will be,$\mathrm{x_2}$=5, $\mathrm{x_3}$=8, $\mathrm{x_4}$=13, $\mathrm{x_5}$=21, $\mathrm{x_6}$=34, $\mathrm{x_7}$=55, $\mathrm{x_8}$=89, $\mathrm{x_9}$=144……

We can see that the even term is only repeating at the multiple of 3. So $\mathrm{x_N}$ will be an even number only if N is multiple of 3 else it will be an odd number.

**Case 4**− This one is for the case when the first term will be an odd number and the second term will be even.Let’s study the pattern with an example, $\mathrm{x_0}$=1 and $\mathrm{x_1}$=2. The first few numbers in the sequence will be,$\mathrm{x_2}$=3, $\mathrm{x_3}$=5, $\mathrm{x_4}$=8, $\mathrm{x_5}$=13, $\mathrm{x_6}$=21, $\mathrm{x_7}$=34, $\mathrm{x_8}$=55, $\mathrm{x_9}$=89, $\mathrm{x_10}$=144……

Observing the pattern, we can conclude that an even number is continuously repeating at N=4,7,10…. These can be expressed in the form 3*a+1 for any positive value of a or 0. We can say that 𝑥𝑁 will be an even number of the sequence if N is of form (3*a+1) else for all other values of N, the number will be an odd number.

### Example

The implementation of the approach in C++ −

#include <iostream> #include <bits/stdc++.h> using namespace std; //function to check if N-th term of fibonacci-like sequence is even or odd void evenOrodd(int x,int y,int N){ if(N==0){ if(x%2==0) //to check if it is even cout<<"N-th term is : Even"<<endl; else cout<<"N-th term is : Odd"<<endl; return; } if(N==1){ if(y%2==0) cout<<"N-th term is : Even"<<endl; else cout<<"N-th term is : Odd"<<endl; return; } if(x%2==0){ //if x is even if(y%2==0){ //if y is even cout<<"N-th term is : Even"<<endl; } else{ if(N%3==0) //if one term is even and other is odd cout<<"N-th term is : Even"<<endl; else cout<<"N-th term is : Odd"<<endl; } return; } else { //if x is odd if(y%2!=0){ //if y is odd too if((N+1)%3==0) cout<<"N-th term is : Even"<<endl; else cout<<"N-th term is : Odd"<<endl; } else{ //if y is an even number if((N-1)%3==0) cout<<"N-th term is : Even"<<endl; else cout<<"N-th term is : Odd"<<endl; } return; } } int main(){ int x=6,y=10,N=7; evenOrodd(x,y,N); x=13, y=16, N=5; evenOrodd(x,y,N); return 0; }

### Output

N-th term is : Even N-th term is : Odd

**Time Complexity : O(1) ,** since constant time is taken to check.

**Space Complexity : O(1) ,** no extra space is taken.

## Conclusion

In this article we learned to check if the N-th term of the fibonacci like sequence is an even number or an odd number. We tried to solve this problem using two different approaches, where in the first approach we use an array to find the sequence up to N and check the N-th term while in the second approach we tried to learn the pattern followed in the sequence. The second approach is the most efficient approach among both as it takes constant time. I hope this article helps you to learn the concepts to solve this problem and you find this article helpful.

- Related Articles
- Fibonacci like sequence in JavaScript
- Check if count of divisors is even or odd in Python
- How to Check if a Number is Odd or Even using Python?
- If $(m + 1)$th term of an A.P. is twice the $(n + 1)$th term, prove that $(3m + 1)$th term is twice the $(m + n + 1)$th term.
- Python Program for Check if the count of divisors is even or odd
- Check whether product of 'n' numbers is even or odd in Python
- C Program to Check if count of divisors is even or odd?
- Java Program to check if count of divisors is even or odd
- If $m$ times the $m^{th}$ term of an AP is equal to $n$ times its $n^{th}$ term. find the $( m+n)^{th}$ term of the AP.
- Find first term, common difference and $5^{th}$ term of the sequence which have the following $n^{th}$ term: $3n+7$.
- If the $n^{th}$ term of an AP is $\frac{3+n}{4}$, then find its $8^{th}$ term.
- Java Program to Check Whether a Number is Even or Odd
- Haskell Program to Check Whether a Number is Even or Odd
- PHP program to check if the total number of divisors of a number is even or odd
- C++ Program to Check Whether Number is Even or Odd