Horje
Find the distance covered to collect items at equal distances

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.
Now if there are n stones lying on the ground, how much distance will be covered by the competitor in completing the whole process.
 

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:
 

Distance run by competitor to pick first stone = 2 * 5 
Distance run by competitor to pick second stone = 2(5 + 3)
Distance run by competitor to pick third stone = 2(5 + 3 + 3) 
= 2(5 + (2 * 3))
Distance run by competitor to pick fourth stone = 2(5 + 3 + 3 + 3) 
= 2(5 + (3 * 3))
Distance run by competitor to pick fifth stone = 2(5 + 3 + 3 + 3 + 3) 
= 2(5 + (4 * 3)) 


.
Distance run by competitor to pick n-th stone = 2(5 + 3 + 3 + ……. + (n-1) times ) 
= 2(5 + (n-1) *3)
So total distance run by competitor = sum of all the above distances 
= (2 * 5) + 2(5 + 3) + 2(5 + (2 * 3)) + 2(5 + (3 * 3)) + ………….. + 2(5 + (n-1) *3) 
= 2(5 + (5 + 3) + (5 + (2 * 3)) + (5 + (3 * 3)) + ………………. + (5 + (n-1) * 3) 
= 2(5 + 5 + 5 …… + n times) + (3 + (2 * 3) + (3 * 3) + ……… + (n-1) * 3) 
= 2(5n + 3(1 + 2 + 3 + ……………. + n-1)) 
= 2(5n + 3/2[(n-1)*(n-1 + 1)] ) 
= 2(5n + 3/2[(n-1)*n]) 
= 2(5n + 3/2(n2 – n)) 
= 10n + 3*n2 – 3*n 
= 3*n2 + 7*n 
= n*((3 * n) + 7)

Below is the implementation of the approach:
 

C++

<?php
//PHP program to calculate
// the distance for given problem
  
// function to calculate the
// distance
  
function  find_distance($n)
{
    return $n * ((3 * $n) + 7);
}
  
// Driver program
  
    $n = 5;
    echo  "Distance = ", find_distance($n);
      
  
  
// This code is contributed by aj_36
?>

Javascript

<script>
  
// JavaScript program to calculate the
// distance for given problem
  
// function to calculate
    // the distance
    function find_distance(n)
    {
        return n * (3 * n + 7);
    }
  
// Driver code
  
        let n = 5;
    
        document.write("Distance = ");
        document.write(find_distance(n));
  
</script>

Output: 
 

110

Time Complexity: O(1)

Auxiliary Space: O(1)




Reffered: https://www.geeksforgeeks.org


Mathematical

Related
Addition and Subtraction of Matrix using pthreads Addition and Subtraction of Matrix using pthreads
Pernicious number Pernicious number
Sum of square of first n odd numbers Sum of square of first n odd numbers
Sum of square of first n even numbers Sum of square of first n even numbers
A square matrix as sum of symmetric and skew-symmetric matrices A square matrix as sum of symmetric and skew-symmetric matrices

Type:
Geek
Category:
Coding
Sub Category:
Tutorial
Uploaded by:
Admin
Views:
17