# Probability for three randomly chosen numbers to be in AP

Given a number **n** and an array containing 1 to (2n+1) consecutive numbers. Three elements are chosen at random. Find the probability that the elements chosen are in A.P.

**Examples**:

Input : n = 2 Output : 0.4 The array would be {1, 2, 3, 4, 5} Out of all elements, triplets which are in AP: {1, 2, 3}, {2, 3, 4}, {3, 4, 5}, {1, 3, 5} No of ways to choose elements from the array: 10 (^{5}C_{3}) So, probability = 4/10 = 0.4 Input : n = 5 Output : 0.1515

The number of ways to select any 3 numbers from (2n+1) numbers are:^{(2n + 1)} C _{3}

Now, for the numbers to be in AP:

with common difference 1—{1, 2, 3}, {2, 3, 4}, {3, 4, 5}…{2n-1, 2n, 2n+1}

with common difference 2—{1, 3, 5}, {2, 4, 6}, {3, 5, 7}…{2n-3, 2n-1, 2n+1}

with common difference n— {1, n+1, 2n+1}

Therefore, Total number of AP group of 3 numbers in (2n+1) numbers are:

(2n – 1)+(2n – 3)+(2n – 5) +…+ 3 + 1 = ** n * n** (Sum of first n odd numbers is n * n )

So, probability for 3 randomly chosen numbers in (2n + 1) consecutive numbers to be in AP = (n * n) / ^{(2n + 1)} C _{3} = **3 n / (4 (n * n) – 1)**

## C++

`// CPP program to find probability that ` `// 3 randomly chosen numbers form AP. ` `#include <bits/stdc++.h> ` `using` `namespace` `std; ` ` ` `// function to calculate probability ` `double` `procal(` `int` `n) ` `{ ` ` ` `return` `(3.0 * n) / (4.0 * (n * n) - 1); ` `} ` ` ` `// Driver code to run above function ` `int` `main() ` `{ ` ` ` `int` `a[] = { 1, 2, 3, 4, 5 }; ` ` ` `int` `n = ` `sizeof` `(a)/` `sizeof` `(a[0]); ` ` ` `cout << procal(n); ` ` ` `return` `0; ` `} ` |

*chevron_right*

*filter_none*

## Java

`// Java program to find probability that ` `// 3 randomly chosen numbers form AP. ` ` ` `class` `GFG { ` ` ` ` ` `// function to calculate probability ` ` ` `static` `double` `procal(` `int` `n) ` ` ` `{ ` ` ` `return` `(` `3.0` `* n) / (` `4.0` `* (n * n) - ` `1` `); ` ` ` `} ` ` ` ` ` `// Driver code to run above function ` ` ` `public` `static` `void` `main(String arg[]) ` ` ` `{ ` ` ` `int` `a[] = { ` `1` `, ` `2` `, ` `3` `, ` `4` `, ` `5` `}; ` ` ` `int` `n = a.length; ` ` ` `System.out.print(Math.round(procal(n) * ` `1000000.0` `) / ` `1000000.0` `); ` ` ` `} ` `} ` ` ` `// This code is contributed by Anant Agarwal. ` |

*chevron_right*

*filter_none*

## Python3

`# Python3 program to find probability that ` `# 3 randomly chosen numbers form AP. ` ` ` `# Function to calculate probability ` `def` `procal(n): ` ` ` ` ` `return` `(` `3.0` `*` `n) ` `/` `(` `4.0` `*` `(n ` `*` `n) ` `-` `1` `) ` ` ` `# Driver code ` `a ` `=` `[` `1` `, ` `2` `, ` `3` `, ` `4` `, ` `5` `] ` `n ` `=` `len` `(a) ` `print` `(` `round` `(procal(n), ` `6` `)) ` ` ` `# This code is contributed by Smitha Dinesh Semwal. ` |

*chevron_right*

*filter_none*

## C#

`// C# program to find probability that ` `// 3 randomly chosen numbers form AP. ` `using` `System; ` ` ` `class` `GFG { ` ` ` ` ` `// function to calculate probability ` ` ` `static` `double` `procal(` `int` `n) ` ` ` `{ ` ` ` `return` `(3.0 * n) / (4.0 * (n * n) - 1); ` ` ` `} ` ` ` ` ` `// Driver code ` ` ` `public` `static` `void` `Main() ` ` ` `{ ` ` ` `int` `[]a = { 1, 2, 3, 4, 5 }; ` ` ` `int` `n = a.Length; ` ` ` `Console.Write(Math.Round(procal(n) * ` ` ` `1000000.0) / 1000000.0); ` ` ` `} ` `} ` ` ` `// This code is contributed by nitin mittal ` |

*chevron_right*

*filter_none*

## PHP

`<?php ` `// PHP program to find probability that ` `// 3 randomly chosen numbers form AP. ` ` ` `// function to calculate probability ` `function` `procal(` `$n` `) ` `{ ` ` ` `return` `(3.0 * ` `$n` `) / ` ` ` `(4.0 * (` `$n` `* ` ` ` `$n` `) - 1); ` `} ` ` ` ` ` `// Driver code ` ` ` `$a` `= ` `array` `(1, 2, 3, 4, 5); ` ` ` `$n` `= sizeof(` `$a` `); ` ` ` `echo` `procal(` `$n` `); ` ` ` `// This code is contributed by aj_36 ` `?> ` |

*chevron_right*

*filter_none*

**Output:**

0.151515

Time Complexity : O(1)

## Recommended Posts:

- Check if N can be represented as sum of integers chosen from set {A, B}
- Probability of rain on N+1th day
- Aptitude | Probability | Question 1
- Aptitude | Probability | Question 6
- Aptitude | Probability | Question 7
- Aptitude | Probability | Question 10
- Aptitude | Probability | Question 9
- Aptitude | Probability | Question 8
- Probability of a key K present in array
- Probability that two persons will meet
- Aptitude | Probability | Question 1
- Aptitude | Probability | Question 2
- Probability of getting more value in third dice throw
- Aptitude | Probability | Question 3
- Aptitude | Probability | Question 4

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.