
- Javascript Basics Tutorial
- Javascript - Home
- Javascript - Overview
- Javascript - Syntax
- Javascript - Enabling
- Javascript - Placement
- Javascript - Variables
- Javascript - Operators
- Javascript - If...Else
- Javascript - Switch Case
- Javascript - While Loop
- Javascript - For Loop
- Javascript - For...in
- Javascript - Loop Control
- Javascript - Functions
- Javascript - Events
- Javascript - Cookies
- Javascript - Page Redirect
- Javascript - Dialog Boxes
- Javascript - Void Keyword
- Javascript - Page Printing
- JavaScript Objects
- Javascript - Objects
- Javascript - Number
- Javascript - Boolean
- Javascript - Strings
- Javascript - Arrays
- Javascript - Date
- Javascript - Math
- Javascript - RegExp
- Javascript - HTML DOM
- JavaScript Advanced
- Javascript - Error Handling
- Javascript - Validations
- Javascript - Animation
- Javascript - Multimedia
- Javascript - Debugging
- Javascript - Image Map
- Javascript - Browsers
- JavaScript Useful Resources
- Javascript - Questions And Answers
- Javascript - Quick Guide
- Javascript - Functions
- Javascript - Resources
Counting possible APs within an array in JavaScript
Arithmetic Progression
Arithmetic Progression (AP) is a sequence of numbers such that the difference of any two consecutive numbers is a constant value (aka common difference).
For instance, 1, 2, 3, 4, 5, 6,… is an AP, which has a common difference equal to 1 (2 -1).
Problem
We are required to write a JavaScript function that takes in an array of integers, arr, as the first and the only argument.
The task of our function is to return the number of arithmetic progressions of size 3 that are possible from that list. In each progression, the differences between the elements must be the same. We are guaranteed that the input array will be sorted in increasing order. For example, if the input to the function is
For example, if the input to the function is −
Input
const arr = [1, 2, 3, 5, 7, 9];
Output
const output = 5;
Output Explanation
Because the desired APs are −
[1, 2, 3], [1, 3, 5], [1, 5, 9], [3, 5, 7] and [5, 7, 9]
Example
Following is the code −
const arr = [1, 2, 3, 5, 7, 9]; const countAP = (arr = []) => { let i, j, k; let { length: len } = arr; let count = 0; for (i = 0; i < len - 2; i++){ for (k = i + 2; k < len; k++){ let temp = arr[i] + arr[k]; let div = temp / 2; if ((div * 2) == temp){ for (j = i + 1; j < k; j++){ if (arr[j] == div){ count += 1; } } } } } return count; }; console.log(countAP(arr));
Output
5
- Related Articles
- Counting all possible palindromic subsequence within a string in JavaScript
- Counting unique elements in an array in JavaScript
- Counting below / par elements from an array - JavaScript
- Counting number of triangle sides in an array in JavaScript
- Number of vowels within an array in JavaScript
- Finding confusing number within an array in JavaScript
- Find average of each array within an array in JavaScript
- Find average of each array within an array JavaScript
- Chunking array within array in JavaScript
- Grouping an Array and Counting items creating new array based on Groups in JavaScript
- Finding the third maximum number within an array in JavaScript
- Finding all possible combinations from an array in JavaScript
- Finding all possible subsets of an array in JavaScript
- Counting how many times an item appears in a multidimensional array in JavaScript
- Counting prime numbers that reduce to 1 within a range using JavaScript
