title: CODE optimization over time complexity
body: <p>I need to optimize this code as per time complexity. No issues for space complexity. I am looking for a better logic to solve the purpose in minimum time.</p>
  
  <p>the question is<br>
  input n and d<br>
  n- number of days -a[n] share values at each day<br>
  d- number of profits -b[d] values of each profit  </p>
  
  <p>output<br>
  for every d profit display<br>
  1) two values<br>
  or<br>
  2) -1  </p>
  
  <p>1)two value:-    each day the value of shares may or may not change, but if it changes, the increased change in shares must be equal to profit b[d]. print index (starting from 1) of those days where the profit is exactly same as the profit and the difference between the index in minimum. </p>
  
  <p>2) -1 if not possible</p>
  
  <p>for example</p>
  
  <pre><code>input
  4 2
  1 2 3 5
  3 8
  </code></pre>
  
  <p>output</p>
  
  <pre><code>2 4
  -1
  </code></pre>
  
  <p>Code:</p>
  
  <pre><code>#include &lt;cmath&gt;
  #include &lt;cstdio&gt;
  #include &lt;vector&gt;
  #include &lt;iostream&gt;
  #include &lt;algorithm&gt;
  using namespace std; 
  
  
  int main() {
  
      double i,j,k,s=100000000,p,q,flag;
      double n,d;
      cin&gt;&gt;n&gt;&gt;d;
      vector&lt;double&gt; a(n);
      vector&lt;double&gt; b(d);
      for(i=0;i&lt;n;++i){
          cin&gt;&gt;a[i];
      }
      for(i=0;i&lt;d;++i){
          cin&gt;&gt;b[i];
      }
      for(k=0;k&lt;d;++k)
      {   flag=9;
        p=q=0;   
       for(i=0;i&lt;n-1;++i){
              for(j=i+1;j&lt;n;++j){
              if((a[j]-a[i])==b[k]){
                  if(s&gt;(j-i)){
                      s=j-i;
                      p=i+1;
                      q=j+1;
                      flag=1;
                      break;
                  }
              }
  
          }        
        }
          s=100000000;
  
      if(flag!=9)    
      cout&lt;&lt;p&lt;&lt;" "&lt;&lt;q&lt;&lt;"\n";
      else
      cout&lt;&lt;"-1\n";    
      } 
      return 0;
  }
  </code></pre>