- Trending Categories
- Data Structure
- Networking
- RDBMS
- Operating System
- Java
- MS Excel
- iOS
- HTML
- CSS
- Android
- Python
- C Programming
- C++
- C#
- MongoDB
- MySQL
- Javascript
- PHP
- Physics
- Chemistry
- Biology
- Mathematics
- English
- Economics
- Psychology
- Social Studies
- Fashion Studies
- Legal Studies

- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who

# JavaScript Program for Print all triplets in sorted array that form AP

AP is the arithmetic progression in which the difference between two consecutive elements is always the same. We will print all the triplet in a sorted array that form AP using three approaches: Naive approach, binary search method and two-pointer approach.

## Introduction to Problem

In this problem we are given by a sorted array meaning all the elements are in the increasing form. We have to find the three elements which are part of the array and form an AP. For example −

Given array: 1 5 2 4 3

From the given array we have two triplets: 1 2 3 and 5 4 3 as the difference between the adjacent elements is equal. Also, as it is written that we have to find the triplets only so we will not find any longer sequences.

Let us move to the approaches to find the triplets −

## Approach

### Naive Approach

In this approach we are just moving over the array using a loop and for each iteration we will run another array for the numbers that are greater as compared to our current index. Then we will again implement a nested array inside the first nested array to find the element which can form AP. Let’s see the code −

### Example

// function to find all the triplets function findAP(arr){ var n = arr.length // traversing over the array for(var i = 0; i< n;i++){ for(var j = i+1;j<n;j++){ for(var k = j+1; k < n; k++){ if(arr[j] - arr[i] == arr[k] - arr[j]) { console.log("Triplet is: " + arr[i] + " " + arr[j] + " " + arr[k]); } } } } } // defining the array and calling the function arr = [1, 5, 2, 4, 3] findAP(arr)

The time complexity of the above code O(), where N is the size of the array.

The space complexity of the above code O(1), as we are not using any extra space.

### Follow Up to Naive Approach

In the previous approach, when we have two elements then we can find the third one as we have the common difference, so to find the third element instead of using linear search we can use the binary search and reduce the time complexity of the above code −

### Example

// function for binary search var binarySearch = function (arr, x, start, end) { if (start > end) return false; var mid=Math.floor((start + end)/2); if (arr[mid]===x) return true; if(arr[mid] > x) return binarySearch(arr, x, start, mid-1); else return binarySearch(arr, x, mid+1, end); } // function to find all the tripletes function findAP(arr){ var n = arr.length // traversing over the array for(var i = 0; i< n;i++){ for(var j = i+1;j<n;j++){ // third element will be var third = 2*arr[j]-arr[i]; if(binarySearch(arr,third,j+1,n)){ console.log("Triplet is: " + arr[i] + " " + arr[j] + " " + third); } } } } // defining the array and calling the function arr = [1, 5, 2, 4, 3] findAP(arr)

The time complexity of the above code O(), where N is the size of the array.

The space complexity of the above code O(1), as we are not using any extra space.

## Efficient Approach

In this approach we are going to use two pointers and find the elements which are at the same difference from the current position. Let us see the code −

### Example

// function to find all the triplets function findAP(arr){ var n = arr.length // traversing over the array for(var i = 1; i< n;i++) { var bptr = i-1 var fptr = i+1 while(bptr >= 0 && fptr < n) { if(arr[i] - arr[bptr] == arr[fptr] - arr[i]){ console.log("Triplet is: " + arr[bptr] + " " + arr[i] + " " + arr[fptr]); bptr--; fptr++; } else if(arr[i] - arr[bptr] > arr[fptr] - arr[i]){ fptr++; } else{ bptr--; } } } } // defining the array and calling the function arr = [1, 4, 7, 10, 13, 16] findAP(arr)

The time complexity of the above code is O(N*N) where N is the size of the given array and space complexity of the above approach is the O(1) as we are not using any extra space.

**Note** − First two methods are efficient for any type of array that is sorted or unsorted, but the last method is only applicable for the sorted arrays and if the array is not sorted we can make it one and still this method will be best among all others.

## Conclusion

In this tutorial, we have implemented a JavaScript program to print all the triplets in a given sorted array that form an AP. Ap is the arithmetic progression in which the difference between two consecutive elements is always the same. We have seen three approaches: Naive approach with O(N*N*N) time complexity, binary search method with O(N*N*log(N)) time complexity, and two pointer approach.