title: Find longest sub-array with no repetitions
body: <p>The task is to find the longest contiguous sub-array with all elements distinct.</p>
  
  <p><strong>Example</strong> <strong>Input</strong> {4, 3, 1, 3, 2, 1, 0} <strong>Output</strong> {3, 2, 1, 0}</p>
  
  <p><strong>Algorithm</strong></p>
  
  <ol>
  <li>Extract first sub Array (here 431)</li>
  <li>Extract second sub Array (here 31)</li>
  <li>Compare number of elements and keep the array with the biggest number (keep 431)</li>
  <li>Return to 2</li>
  </ol>
  
  <p><strong>Problem</strong> The output is incorrect </p>
  
  <pre class="lang-c prettyprint-override"><code>/* Free old array and replace it by the new array
   * If we only want to free old array and replace it by a new array
   * Function will free old array and replace it by a new array with size equal to maximum size it can have 
   * Maximum size is the size of the input array
   */
  int* newArray(int* oldArray,int* newArray, int sizeArray, int sizeFArray)
  {
      if (newArray == NULL) {
          int* temp = malloc(sizeFArray * sizeof(int));
          if (temp == NULL)
              exit(1);
  
          return temp;    
      } else {
          memcpy(oldArray, newArray, sizeArray);
          return oldArray;
      }
      printf("Error");
      exit(1);
  }
  //int isAvailable(int* array , int size, int number) checks if number is available in array (return 0 if true, 1 if false)
  //printArray(int* array, int size) is a simple function to print an array
  void subArray(int* inputArray, int sizeInputArray) 
  {
      int* candidate = malloc(sizeInputArray * sizeof(int));
      if (candidate == NULL)
          exit(1);
      int sizeCandidate = 0;
      int* newCandidate = malloc(sizeInputArray * sizeof(int));
      if (newCandidate == NULL)
          exit(1);
      int sizeNewCandidate = 0;
  
      //We will first fill the candidate
      while (sizeCandidate &lt; sizeInputArray &amp;&amp; isAvailable(candidate, sizeCandidate, *(inputArray + sizeCandidate)) != 0) {
          *(candidate + sizeCandidate) = *(inputArray + sizeCandidate);
          sizeCandidate++;    
      }
      int index = 1;
      //Check all potential new candidates
      //If new candidate holds more elements than the current candidate
      //Current candidate will be replaced by new candidate
      //Else we will redo the process and check using the next candidate if availble
      for (int i = 1; i &lt; sizeInputArray; i++) {
          if(isAvailable(newCandidate, sizeNewCandidate, *(inputArray + i)) == 0) { 
              if (sizeNewCandidate &gt; sizeCandidate) {
                  candidate = newArray(candidate, newCandidate, sizeNewCandidate, sizeInputArray);
                  newCandidate = newArray(newCandidate, NULL, 0, 0);
                  sizeCandidate = sizeNewCandidate;
                  sizeNewCandidate = 0;
                  i = ++index;
              } else {
                  newCandidate = newArray(newCandidate, NULL, 0, sizeInputArray);
                  sizeNewCandidate = 0;
                  i = ++index;
              }
          } else {
              *(newCandidate + sizeNewCandidate) = *(inputArray + i);
              sizeNewCandidate++;
          }
      }
      printArray(candidate, sizeCandidate);
  }
  </code></pre>