title: Space complexity- various cases functions involving arrays
body: <p>here i have written some different cases of functions that have array as input i need help to determine if reasoning behind my answers is correct (i have put question marks on the cases im having difficulty with)</p>
  
  <p><strong>case 1:</strong></p>
  
  <pre><code>algo(arr[],n){
    //does nothing
  }
  </code></pre>
  
  <p>Space = (0) + 0 = O(1)      </p>
  
  <p>Auxiliary Space = 0 = O(1)</p>
  
  <p>(reason: storage allocated for inputs havent been worked on and no temporary storage declared inside the function)</p>
  
  <p><strong>case 2:</strong></p>
  
  <pre><code>algo(arr[],n){
     int i;
  }
  </code></pre>
  
  <p>Space = (0) + 1 = O(1)    </p>
  
  <p>Auxiliary Space = 1 = O(1)</p>
  
  <p>(reason: storage allocated for inputs havent been worked on and 1 temporary storage for variable i is needed) </p>
  
  <p><strong>case 3:</strong></p>
  
  <pre><code>algo(arr[],n){
     int i;
     for(i=0; i&lt;n ; i++){
      //does nothing
     }
  }
  </code></pre>
  
  <p>Space = (1) + 1 = O(1)     </p>
  
  <p>Auxiliary Space = 1 = O(1)</p>
  
  <p>(reason: storage for variable n is being worked on and 1 temp storage needed for variable i is needed)</p>
  
  <p><strong>case 4:</strong></p>
  
  <p>where arr[] is an array of size L where L > n</p>
  
  <pre><code>algo(arr[],n){
     int i;
     for(i=0;i&lt;n;i++){
      arr[i] = i;
     }
  }
  </code></pre>
  
  <p>Space = (1 + n) + 1 = O(n)  </p>
  
  <p>Auxiliary Space = 1 = O(1)</p>
  
  <p>(reason: 1 storage for variable n and n storages for arr[0]....arr[n-1] is being worked on and 1 temp storage for variable i is needed)</p>
  
  <p><strong>case 5</strong>:??</p>
  
  <p>where arr[] is an array of size L where L > n</p>
  
  <pre><code>algo(arr[],n){               
     arr[n]=n;
  }
  </code></pre>
  
  <p>Space = (1 + 1) + 0 = O(1)    </p>
  
  <p>Auxiliary Space = 0 = O(1)</p>
  
  <p>(reason: 1 storage for variable n is being worked on and only 1 storage will always be worked on in arr[] regardless of the value of n, no temporary storage was declared)</p>
  
  <p><strong>case 6</strong>:??</p>
  
  <p>where arr[] is an array of size L where L > i</p>
  
  <pre><code>algo(arr[],n){
     int i = 1;
    arr[i]=i;
  }
  </code></pre>
  
  <p>Space = (1) + 1 = O(1) </p>
  
  <p>Auxiliary Space = 1 = O(1)</p>
  
  <p>(reason: only 1 storage will always be worked on in arr[] which is always in arr[i] and 1 temp storage needed for i)</p>
  
  <p><strong>case 7</strong>:??</p>
  
  <p>where arr[] is an array of size L where L > n</p>
  
  <pre><code>algo(arr[],n){
     int i;
     for(i=0;i&lt;n;i++){
      // does nothing
     }
     arr[n] = n;
  
  }
  </code></pre>
  
  <p>Space = (1 + 1) + 1 = O(1)</p>
  
  <p>Auxiliary Space = 1 = O(1)</p>
  
  <p>(reason: 1 storage for variable n is being worked on and only 1 storage will always be worked on in arr[] regardless of the value of n, 1 temporary storage was declared for variable i)</p>
  
  <p><strong>case 8</strong>:??</p>
  
  <p>where arr[] is an array of size L where L > i</p>
  
  <pre><code>algo(arr[],n){
     int i;
     for(i=0;i&lt;n;i++){
      // does nothing
     }
     i = 1;
     arr[i] = i;
  
  }
  </code></pre>
  
  <p>Space = (1 + 1) + 1 = O(1)</p>
  
  <p>Auxiliary Space = 1 = O(1)</p>
  
  <p>(reason: only 1 storage will always be worked on in arr[] which is always in arr[i], 1 storage for variable n being worked on,  and 1 temp storage needed for variable i)</p>
  
  <p><strong>case 9</strong>:</p>
  
  <p>where arr1[] is an array of size m</p>
  
  <pre><code>algo(arr1[],n){
     int arr2[n];
  }
  </code></pre>
  
  <p>Space = (1) + n = O(n)</p>
  
  <p>Auxiliary Space = n = O(n)</p>
  
  <p>(reason: 1 storage for variable n being worked on, n temporary storage needed for arr2[]) </p>
  
  <p><strong>case 10</strong>:??</p>
  
  <p>where arr1[] is an array of size L </p>
  
  <pre><code>algo(arr1[],n){
     int m=1;
     int arr2[m];
  }
  </code></pre>
  
  <p>Space = (0) + 1 + m = O(m)</p>
  
  <p>Auxiliary Space = 1 + m = O(m)</p>
  
  <p>(reason: 1 temporary storage needed for variable m and m temporary storage needed for arr2[])</p>
  
  <p>or</p>
  
  <p>Space = (0) + 1 + 1 = O(1)</p>
  
  <p>Auxiliary Space = 1 + 1 = O(1)</p>
  
  <p>(reason: 1 temporary storage needed for variable m and since m is a constant the space required for arr2[] always going to be some constant c)</p>
  
  <p><strong>case 11</strong>:??</p>
  
  <pre><code>algo(arr1[],n){
     int i;
     int m=10;
     int arr2[m];
  
     for(i=0;i&lt;m;i++){
       arr2[i]= i;
     }   
  
  }
  </code></pre>
  
  <p>Space = (0) + 1 + 1 + 1 = O(1)</p>
  
  <p>Auxiliary Space = 1 + 1 + 1 = O(1)</p>
  
  <p>(reason: 1 temporary storage needed for variable m, 1 temp storage for variable i and since m is a constant the space required for arr2[] always going to be some constant c)</p>
  
  <p><strong>case 12</strong>:</p>
  
  <p>where arr1[] is an array of size L where L > n and then arr2[] is an array of size K where K > m</p>
  
  <pre><code>algo(arr1[],n, arr2[], m){
     int i;
  
     for(i=0;i&lt;n;i++){
       arr1[i]= i;
     }   
  
     for(i=0;i&lt;m;i++){
       arr2[i]= i;
     }   
  
  }
  </code></pre>
  
  <p>Space = (1 + n + 1 + m) + 1 = O(n + m)</p>
  
  <p>Auxiliary Space = 1 = O(1)</p>
  
  <p>(reason: 2 storage for var n and m being worked on, n storage for arr1, m storage for arr2 and 1 storage needed for var i)</p>
  
  <p><strong>case 13</strong>:??</p>
  
  <p>where arr1[] is an array of size L and L > n</p>
  
  <pre><code>algo(arr1[],n){
     int i;
     int m=10;
     int arr2[m];
  
     for(i=0;i&lt;n;i++){
       arr1[i]= i;
     }   
  
     for(i=0;i&lt;m;i++){
       arr2[i]= i;
     }   
  
  }
  </code></pre>
  
  <p>Space = (1 + n) + 1 + 1 + 1 = O(n)</p>
  
  <p>Auxiliary Space = 1 + 1 + 1 = O(1)</p>
  
  <p>(reason: 2 spaces needed for variable i and m, constant m space needed for arr2, 1 space being worked on for var n and n space being worked on for arr1) </p>
  
  <p><strong>case 14</strong>:</p>
  
  <pre><code> algo(arr1[],n){
  
     int arr2[10];
  
   }
  </code></pre>
  
  <p>Space = (0) + 1 = O(1)</p>
  
  <p>Auxiliary Space = 1 = O(1)</p>
  
  <p>(reason: 10 spaces is always required to declare arr2 whenever the function is called)</p>