Count the Arithmetic sequences in the Array of size at least 3
Given an array arr[] of size N, the task is to find the count of all arithmetic sequences in the array.
Examples:
Input: arr = [1, 2, 3, 4]
Output: 3
Explanation:
The arithmetic sequences in arr are [1, 2, 3], [2, 3, 4] and [1, 2, 3, 4] itself.
Input: arr = [1, 3, 5, 6, 7, 8]
Output: 4
Explanation:
The arithmetic sequences in arr are [1, 3, 5], [5, 6, 7], [5, 6, 7, 8] and [6, 7, 8].
Naive approach:
- Run two loops and check for each sequence of length at least 3.
- If the sequence is an arithmetic sequence, then increment the answer by 1.
- Finally, return the count of all the arithmetic subarray of size at least 3.
Time Complexity: O(N2)
Efficient approach: We will use a dynamic programming approach to maintain a count of all arithmetic sequences till any position.
- Initialize a variable, res with 0. It will store the count of sequences.
- Initialize a variable, count with 0. It will store the size of the sequence minus 2.
- Increase the value of count if the current element forms an arithmetic sequence else make it zero.
- If the current element L[i] is making an arithmetic sequence with L[i-1] and L[i-2], then the number of arithmetic sequences till the ith iteration is given by:
res = res + count
- Finally, return the res variable.
Below is the implementation of the above approach:
C++
#include <bits/stdc++.h>
using namespace std;
int numberOfArithmeticSequences( int L[], int N)
{
if (N <= 2)
return 0;
int count = 0;
int res = 0;
for ( int i = 2; i < N; ++i) {
if (L[i] - L[i - 1] == L[i - 1] - L[i - 2]) {
++count;
}
else {
count = 0;
}
res += count;
}
return res;
}
int main()
{
int L[] = { 1, 3, 5, 6, 7, 8 };
int N = sizeof (L) / sizeof (L[0]);
cout << numberOfArithmeticSequences(L, N);
return 0;
}
|
Java
class GFG{
static int numberOfArithmeticSequences( int L[], int N)
{
if (N <= 2 )
return 0 ;
int count = 0 ;
int res = 0 ;
for ( int i = 2 ; i < N; ++i) {
if (L[i] - L[i - 1 ] == L[i - 1 ] - L[i - 2 ]) {
++count;
}
else {
count = 0 ;
}
res += count;
}
return res;
}
public static void main(String[] args)
{
int L[] = { 1 , 3 , 5 , 6 , 7 , 8 };
int N = L.length;
System.out.print(numberOfArithmeticSequences(L, N));
}
}
|
Python3
def numberOfArithmeticSequences(L, N) :
if (N < = 2 ) :
return 0
count = 0
res = 0
for i in range ( 2 ,N):
if ( (L[i] - L[i - 1 ]) = = (L[i - 1 ] - L[i - 2 ])) :
count + = 1
else :
count = 0
res + = count
return res
L = [ 1 , 3 , 5 , 6 , 7 , 8 ]
N = len (L)
print (numberOfArithmeticSequences(L, N))
|
C#
using System;
class GFG{
static int numberOfArithmeticSequences( int []L,
int N)
{
if (N <= 2)
return 0;
int count = 0;
int res = 0;
for ( int i = 2; i < N; ++i)
{
if (L[i] - L[i - 1] ==
L[i - 1] - L[i - 2])
{
++count;
}
else
{
count = 0;
}
res += count;
}
return res;
}
public static void Main(String[] args)
{
int []L = { 1, 3, 5, 6, 7, 8 };
int N = L.Length;
Console.Write(numberOfArithmeticSequences(L, N));
}
}
|
Javascript
<script>
function numberOfArithmeticSequences( L, N)
{
if (N <= 2)
return 0;
var count = 0;
var res = 0;
for ( var i = 2; i < N; ++i) {
if (L[i] - L[i - 1] == L[i - 1] - L[i - 2]) {
++count;
}
else {
count = 0;
}
res += count;
}
return res;
}
var L = [ 1, 3, 5, 6, 7, 8 ];
var N = L.length;
document.write(numberOfArithmeticSequences(L, N));
</script>
|
Time Complexity: O(N)
Auxiliary Space: O(1)
Last Updated :
16 Sep, 2022
Like Article
Save Article
Share your thoughts in the comments
Please Login to comment...