C++ code for calculation of a physics experiment

Suppose, we are performing a physics experiment. We are given n pairs of values and a threshold value k. Each the first value of the pair is added to a total value and the second value of the pair is also added to another total value. Now, we check if a total value is minimum or the (k - total) value is minimum. We do this for both the totals and then add them and print the output.

So, if the input is like n = 4, k = 20, values = {{3, 5}, {4, 3}, {2, 1}, {4, 4}}, then the output will be 14.

Steps

To solve this, we will follow these steps −

a := 0, b = 0
for initialize i := 0, when i 

Example

Let us see the following implementation to get better understanding −

#include 
using namespace std;
#define N 100
void solve(int n, int k, vector> values) {
   int a = 0, b = 0;
   for(int i = 0; i > values = {{3, 5}, {4, 3}, {2, 1}, {4, 4}};
   solve(n, k, values);
   return 0;
}

Input

4, 20, {{3, 5}, {4, 3}, {2, 1}, {4, 4}}

Output

14
Updated on: 2022-03-29T08:53:03+05:30

902 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements