English | 简体中文 | 繁體中文 | Русский язык | Français | Español | Português | Deutsch | 日本語 | 한국어 | Italiano | بالعربية

C of choosing three numbers randomly in AP ++Program probability

Given an array with the number "n", the task is to find the probability that three randomly selected numbers appear in AP.

Example

Input-: arr[] = { 2,3,4,7,1,2,3 }
Output-: The probability that three random numbers are in A.P. is: 0.107692
Input-: arr[] = { 1, 2, 3, 4, 5 }
Output-: The probability that three random numbers are in A.P. is: 0.151515

The following program uses the following methods-

  • Input positive integer array

  • Calculate the size of the array

  • Apply the following formula to find the probability that three random numbers appear in AP

    3 n /(4(n * n) – 1)

  • Print Result

Algorithm

Start
Step 1-> Function to calculate the probability that three random numbers are in AP
   double probab(int n)
      return (3.0 * n) / (4.0 * (n * n) - 1)
Step 2->In main()
   declare an array of elements as int arr[] = { 2,3,4,7,1,2,3 }
   calculate the size of an array as int size = sizeof(arr)/sizeof(arr[0])
   call the function to calculate probability as probab(size)
Stop

Example

#include <bits/stdc++.h>
using namespace std;
//calculate the probability that three random numbers are in AP
double probab(int n) {
    return (3.0 * n) / (4.0 * (n * n) - 1);
}
int main() {
    int arr[] = { 2,3,4,7,1,2,3 };
    int size = sizeof(arr);/sizeof(arr[0]);
    cout << "probability that three random numbers are in A.P. is: " << probab(size);
    return 0;
}

Output Result

The probability that three random numbers are in arithmetic progression (A.P.) is: 0.107692