Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>I think the main problem in your code is that you are binding the selection to </p> <pre><code>(inputArr.length ) / numberOfElementToDelete </code></pre> <p>This way you are not considering the first and the last elements that you don't want to remove.</p> <p>An example: if you have an array of 16 elements and you want to delete 6 elements it means that the final array will have 10 elements but, since the first and the last are fixed, you'll have to select 8 elements out of the remaining 14. This means you'll have to select 8/14 (0,57) elements from the array (not considering the first and the last). This means that you can initialize a counter to zero, scan the array starting from the second and sum the value of the fraction to the counter, when the value of the counter reach a new integer number (ex. at the third element the counter will reach 1,14) you'll have an element to pick and put to the new array.</p> <p>So, you can do something like this (pseudocode):</p> <pre><code> int newLength = originalLength - toDelete; int toChoose = newLength - 2; double fraction = toChoose / (originalLength -2) double counter = 0; int threshold = 1; int newArrayIndex = 1; for(int i = 1; i &lt; originalLength-1; i++){ **counter += fraction;** if(integerValueOf(counter) == threshold){ newArray[newArrayIndex] = originalArray[i]; threshold++; **newArrayIndex++;** } } newArray[0] = originalArray[0]; newArray[newArray.length-1] = originalArray[originalArray.length-1]; </code></pre> <p>You should check for the particular cases like originalArray of length 1 or removal of all the elements but I think it should work.</p> <p><strong>EDIT</strong> Here is a Java implementation (written on the fly so I didn't check for nulls etc.)</p> <pre><code>public class Test { public static void main(String[] args){ int[] testArray = {1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16}; int[] newArray = remove(testArray, 6); for(int i = 0; i &lt; newArray.length; i++){ System.out.print(newArray[i]+" "); } } public static int[] remove(int[] originalArray, int toDelete){ if(toDelete == originalArray.length){ //avoid the removal of all the elements, save at least first and last toDelete = originalArray.length-2; } int originalLength = originalArray.length; int newLength = originalLength - toDelete; int toChoose = newLength - 2; int[] newArray = new int[newLength]; double fraction = ((double)toChoose) / ((double)originalLength -2); double counter = 0; int threshold = 1; int newArrayIndex = 1; for(int i = 1; i &lt; originalLength-1; i++){ counter += fraction; if(((int)counter) == threshold || //condition added to cope with x.99999999999999999... cases (i == originalLength-2 &amp;&amp; newArrayIndex == newLength-2)){ newArray[newArrayIndex] = originalArray[i]; threshold++; newArrayIndex++; } } newArray[0] = originalArray[0]; newArray[newArray.length-1] = originalArray[originalArray.length-1]; return newArray; } } </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload