Article Categories
- All Categories
-
Data Structure
-
Networking
-
RDBMS
-
Operating System
-
Java
-
MS Excel
-
iOS
-
HTML
-
CSS
-
Android
-
Python
-
C Programming
-
C++
-
C#
-
MongoDB
-
MySQL
-
Javascript
-
PHP
-
Economics & Finance
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 triplets 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 a sorted array meaning all the elements are in increasing order. We have to find 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 triplets like [2, 3, 4] where the common difference is 1. In an arithmetic progression, if we have three numbers a, b, c, then b - a = c - b, or equivalently, 2*b = a + c.
Naive Approach
In this approach we use three nested loops to check all possible triplets. For each combination of three elements, we verify if they form an arithmetic progression.
Example
// function to find all the triplets
function findAP(arr) {
var n = arr.length;
console.log("Naive Approach Results:");
// 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
var arr = [1, 2, 3, 4, 5];
findAP(arr);
Naive Approach Results: Triplet is: 1 2 3 Triplet is: 1 3 5 Triplet is: 2 3 4 Triplet is: 3 4 5
Time Complexity: O(N³), where N is the size of the array.
Space Complexity: O(1), as we are not using any extra space.
Binary Search Approach
When we have two elements, we can calculate the third element using the AP property. Instead of using linear search to find the third element, we can use binary search to reduce time complexity.
Example
// function for binary search
function binarySearch(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 triplets
function findAP(arr) {
var n = arr.length;
console.log("Binary Search Approach Results:");
// traversing over the array
for(var i = 0; i < n; i++) {
for(var j = i + 1; j < n; j++) {
// third element will be: if a, b, c are in AP then c = 2*b - a
var third = 2 * arr[j] - arr[i];
if(binarySearch(arr, third, j + 1, n - 1)) {
console.log("Triplet is: " + arr[i] + " " + arr[j] + " " + third);
}
}
}
}
// defining the array and calling the function
var arr = [1, 2, 3, 4, 5];
findAP(arr);
Binary Search Approach Results: Triplet is: 1 2 3 Triplet is: 1 3 5 Triplet is: 2 3 4 Triplet is: 3 4 5
Time Complexity: O(N² log N), where N is the size of the array.
Space Complexity: O(log N), due to recursion stack in binary search.
Two Pointer Approach
This is the most efficient approach for sorted arrays. For each middle element, we use two pointers - one moving backward and one moving forward - to find triplets that form an AP.
Example
// function to find all the triplets
function findAP(arr) {
var n = arr.length;
console.log("Two Pointer Approach Results:");
// traversing over the array (middle element)
for(var i = 1; i < n - 1; i++) {
var left = i - 1;
var right = i + 1;
while(left >= 0 && right < n) {
// Check if arr[left], arr[i], arr[right] form AP
if(arr[i] - arr[left] == arr[right] - arr[i]) {
console.log("Triplet is: " + arr[left] + " " + arr[i] + " " + arr[right]);
left--;
right++;
}
else if(arr[i] - arr[left] > arr[right] - arr[i]) {
// Need larger right element
right++;
}
else {
// Need smaller left element
left--;
}
}
}
}
// defining the array and calling the function
var arr = [1, 2, 3, 4, 5];
findAP(arr);
Two Pointer Approach Results: Triplet is: 1 2 3 Triplet is: 2 3 4 Triplet is: 3 4 5 Triplet is: 1 3 5
Time Complexity: O(N²), where N is the size of the array.
Space Complexity: O(1), as we are not using any extra space.
Comparison
| Approach | Time Complexity | Space Complexity | Best For |
|---|---|---|---|
| Naive | O(N³) | O(1) | Small arrays, any order |
| Binary Search | O(N² log N) | O(log N) | Medium arrays, any order |
| Two Pointer | O(N²) | O(1) | Large sorted arrays |
Key Points
- The first two methods work for both sorted and unsorted arrays
- The two-pointer method requires a sorted array but is most efficient
- For unsorted arrays, sort first then use the two-pointer approach
- In AP, three numbers a, b, c satisfy: 2*b = a + c
Conclusion
We implemented three JavaScript approaches to find triplets forming arithmetic progressions. The two-pointer method offers the best performance for sorted arrays with O(N²) complexity, while naive and binary search approaches work for any array order.
