title: How can I reduce the time complexity of an algorithm in c++?
body: <p>The following code takes in an integer <em>t</em> and then takes in 3 more integers <em>t</em> times and returns the maximum number of times you can subtract 1 from two different integers at the same time, whereas the program stops when there is only 1 integer above 0 remaining. I have solved the problem, but I want to reduce the time complexity of the code and I don't know how.</p>
  
  <pre><code>#include &lt;bits/stdc++.h&gt;
  
  using namespace std;
  
  int main() {
  long long t, r, g, b, arr[1000], count = 0;
  bool isMax=true;
  cin &gt;&gt; t;
  
  for (long long i = 0; i &lt; t; i++) {
      cin &gt;&gt; r &gt;&gt; g &gt;&gt; b;
      arr[0] = r;
      arr[1] = g;
      arr[2] = b;
      for (long long j = 0; j &lt; 3; j++) {
          for (long long k = 0; k &lt; 2; k++) {
              if (arr[k] &gt; arr[k + 1]) {
                  long long a = arr[k];
                  long long b = arr[k + 1];
                  arr[k] = b;
                  arr[k + 1] = a;
              }
          }
      }
      count = 0;
      if (arr[2] == 1) {
          cout &lt;&lt; 1 &lt;&lt; endl;
      } else if (arr[0] + arr[1] &lt;= arr[2]) {
          cout &lt;&lt; arr[0] + arr[1] &lt;&lt; endl;
      } else {
          while (arr[0] &gt; 0) {
              while (isMax &amp;&amp; arr[0] &gt; 0) {
                  arr[2]--;
                  arr[0]--;
                  count++;
                  if (arr[2] &lt; arr[1]) {
                      isMax = false;
                  }
              }
              while (!isMax &amp;&amp; arr[0] &gt; 0) {
                  arr[1]--;
                  arr[0]--;
                  count++;
                  if (arr[1] &lt; arr[2]) {
                      isMax = true;
                  }
              }
          }
          while (arr[2] &gt; 0 &amp;&amp; arr[1] &gt; 0) {
              arr[2]--;
              arr[1]--;
              count++;
          }
          cout &lt;&lt; count &lt;&lt; endl;
      }
  }
  }
  </code></pre>
  
  <p>How can I get the same output without using all these loops that increase the time complexity?</p>
  
  <p>Edit: I don't want my code re-written for me, this is homework and all I want are tips and help so I can reduce the time complexity, which I don't know how to do.</p>
  
  <p>Edit 2: In my algorithm, I sort the 3 numbers in ascending order, then I use a while loop to check if the smallest number (s/arr[0]) is > 0. After that, I use two more while loops to alternate between the largest and medium-size numbers (l/arr[2] and m/arr[1] respectively) and decrement from both variables s and l or m (alternating). When s becomes 0, that will mean I can just decrement l and m till one of them equals 0, and then I print the count variable (I increment count every time I decrement two of the variables).</p>