Given N factors on a aircraft, the place every level has a direct path connecting it to a distinct level, the duty is to rely the whole variety of distinctive direct paths between the factors.
Observe: The worth of N will at all times be larger than 2.
Examples:
Enter: N = 4
Output: 6
Rationalization: Consider 4 factors as a 4 sided polygon. There’ll 4 direct paths (sides of the polygon) in addition to 2 diagonals (diagonals of the polygon). Therefore the reply will likely be 6 direct paths.Enter: N = 3
Output: 3
Rationalization: Consider 3 factors as a 3 sided polygon. There’ll 3 direct paths (sides of the polygon) in addition to 0 diagonals (diagonals of the polygon). Therefore the reply will likely be 3 direct paths.
Method: The given drawback will be solved utilizing an commentary that for any N-sided there are (variety of sides + variety of diagonals) direct paths. For any N-sided polygon, there are N sides and N*(N – 3)/2 diagonals. Due to this fact, the whole variety of direct paths is given by N + (N * (N – 3))/2.
Under is the implementation of the above strategy:
C++
|
Time Complexity: O(1)
Auxiliary Area: O(1)
Consideration reader! Don’t cease studying now. Pay money for all of the necessary mathematical ideas for aggressive programming with the Important Maths for CP Course at a student-friendly worth. To finish your preparation from studying a language to DS Algo and lots of extra, please refer Full Interview Preparation Course.