Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8234681
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 7, 20262026-06-07T18:33:56+00:00 2026-06-07T18:33:56+00:00

I have source array, and I want to generate new array from the source

  • 0

I have source array, and I want to generate new array from the source array by removing a specified number of elements from the source array, I want the elements in the new array to cover as much as possible elements from the source array (the new elements are uniformly distributed over the source array) and keeping the first and last elements the same (if any).

I tried this :

public static void printArr(float[] arr)
    {
        for (int i = 0; i < arr.length; i++)
            System.out.println("arr[" + i + "]=" + arr[i]);

    }
public static float[] removeElements(float[] inputArr , int numberOfElementToDelete)
    {
       float [] new_arr = new float[inputArr.length - numberOfElementToDelete];
        int f = (inputArr.length  ) / numberOfElementToDelete;
        System.out.println("f=" + f);
        if(f == 1)
        {
            f = 2;
            System.out.println("f=" + f);
        }

       int j = 1 ;
        for (int i = 1; i < inputArr.length ; i++)
        {
            if( (i + 1) % f != 0)
            {

                System.out.println("i=" + i + "   j= " + j);
                if(j < new_arr.length)
                {
                    new_arr[j] = inputArr[i];
                    j++;
                }

            }

        }

        new_arr[0] = inputArr[0];
        new_arr[new_arr.length - 1] = inputArr[inputArr.length - 1];
        return new_arr;
    }
public static void main(String[] args)
    {

        float [] a = {1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16};
        a = removeElements(a, 6);
        printArr(a);
    }

I have made a test for(removeElements(a, 5) and removeElements(a, 4) and removeElements(a, 3)) but removeElements(a, 6); gave :

 arr[0]=1.0
arr[1]=3.0
arr[2]=5.0
arr[3]=7.0
arr[4]=9.0
arr[5]=11.0
arr[6]=13.0
arr[7]=15.0
arr[8]=0.0
arr[9]=16.0

the problem is (arr[8]=0.0) it must take a value ..
How to solve this? is there any code that can remove a specified number of elements (and keep the elements distributed over the source array without generating zero in some elements)?

EDIT :

examples :
removeElements(a, 1) ==> remove one element from the middle (7) {1,2,3,4,5,6,7,9,10,11,12,13,14,15,16}

removeElements(a, 2) ==> remove two elements at indexes (4,19) or (5,10) or (4,10) (no problem)

removeElements(a, 3) ==> remove three elements at indexes (4,9,14) or (4,10, 15) or(no problem also)

removeElements(a, 4) ==> remove four elements at indexes (3,7,11 , 15) or ( 3 ,7,11,14) for example ..
what I want is if I draw the values in the source array on (chart on Excel for example) and I draw the values from the new array , I must get the same line (or close to it).

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-07T18:33:57+00:00Added an answer on June 7, 2026 at 6:33 pm

    I think the main problem in your code is that you are binding the selection to

    (inputArr.length  ) / numberOfElementToDelete
    

    This way you are not considering the first and the last elements that you don’t want to remove.

    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.

    So, you can do something like this (pseudocode):

        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 < 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];
    

    You should check for the particular cases like originalArray of length 1 or removal of all the elements but I think it should work.

    EDIT
    Here is a Java implementation (written on the fly so I didn’t check for nulls etc.)

    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 < 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 < originalLength-1; i++){
                counter += fraction;            
                if(((int)counter) == threshold ||
                    //condition added to cope with x.99999999999999999... cases 
                   (i == originalLength-2 && newArrayIndex == newLength-2)){
                    newArray[newArrayIndex] = originalArray[i];
                    threshold++;
                    newArrayIndex++;
                }           
             }
             newArray[0] = originalArray[0];
             newArray[newArray.length-1] = originalArray[originalArray.length-1];
             return newArray;
        }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a byte array of size 4 byte[] source = new byte[4]; Now
I have a large int[] array and a much smaller int[] array. I want
I have a tableview and an array source. When I init array in viewDidLoad
I have an array of strings of the form: @source = ( something,something2,third ,something,something3
I have a multidimensional array of arrays (source below), where my keys and values
I have source XMLfiles that come in with multiple root elements and there is
I have three source files in a folder. I simply want to compile them
I have an array with tree data (by parent id). I want to convert
I have an array of dictionaries loaded from a plist (below) called arrayHistory. <plist
I have html source code which I want to filter out one or more

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.