![]() |
A race is going on in which several stones are placed on a road. A bucket is placed at the starting point of the race, which is 5 units away from the first stone. The other stones are 3 units apart from each other and lie straight in a line one after another. I.e., the distance between 1st and 2nd stone is 3 units, between 3rd and 4th stone, it is also 3 units and so on. The competitor starts from the bucket, picks up the nearest stone, goes back and puts that stone into the bucket, then runs again to collect the next nearest stone, runs back, and puts it in the bucket. This way the process is continued till all the stones have been put into the bucket. Examples: Input : n = 3 Output : Distance = 48 Explanation = 2*5 + 2(5 + 3) + 2(5 + 3 + 3) = 10 + 16 + 22 = 48 Input : n = 5 Output : Distance = 110 Explanation = 2*5 + 2(5 + 3) + 2(5 + 3 + 3) + 2(5 + 3 + 3 + 3) + 2(5 + 3 + 3 + 3 + 3) = 10 + 16 + 22 + 28 + 34 = 110
Observing the pattern:
Below is the implementation of the approach: C++
Javascript
Output: 110 Time Complexity: O(1) Auxiliary Space: O(1) |
Reffered: https://www.geeksforgeeks.org
Mathematical |
Type: | Geek |
Category: | Coding |
Sub Category: | Tutorial |
Uploaded by: | Admin |
Views: | 17 |