An array that deletes a number every two numbers, goes to the end of the array, and continues to go through the bad, resulting in the subscript of the last element removed

Source: Internet
Author: User
Tags array length

  1. Public class Deleteeverytwo {
  2. public static void Main (string[] args) {
  3. int index = GETLASTDELETEDINDEX (8);
  4. System.out.println ("The Last index deleted is" + index);
  5. }
  6. /** 
  7. *
  8. * @param a
  9. * Array length
  10. * @return The original subscript of the last deleted number
  11. */
  12. public static int getlastdeletedindex (int len) {
  13. If (len <= 0) { //return-1 if the array length does not meet the requirements
  14. return-1;
  15. }
  16. int[] arr = new Int[len];
  17. for (int i = 0; i < len; i++) { //Initialize the value of each element as the current subscript
  18. Arr[i] = len;
  19. }
  20. final int delflag = len + 1; //Remove flag bit
  21. int currentsize = len; //Record the current valid length of the array (that is, the number of elements that are not set to the delete flag), and finally to 0
  22. final int STEP = 2; //Step
  23. int count = 0; //Step count
  24. int lastdelindex = 0; //Record the subscript of the last deleted element
  25. int i = 0; //Cycle subscript
  26. While (currentsize! = 0) {
  27. if (arr[i]! = Delflag) { //interpret whether the current element equals the delete flag
  28. if (count++ = = Step) { //When the step count satisfies the step size
  29. Arr[i] = Delflag; //Set element to delete flag bit
  30. Lastdelindex = i; //Record the subscript of the place
  31. currentsize--; //valid array length minus 1
  32. Count = 0; //Step count zeroing
  33. System.out.println ("Deleted index is" + i% len);
  34. }
  35. }
  36. i = (i + 1)% Len; //subscript take -off to achieve cyclic subscript
  37. }
  38. return lastdelindex;
  39. }
  40. }

An array that deletes a number every two numbers, goes to the end of the array, and continues to go through the bad, resulting in the subscript of the last element removed

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.