Basic algorithms for arrays

Posted by damianjames on Tue, 04 Jan 2022 06:26:50 +0100

1. Find the subscript of the last deleted element

There is an array a[1000] to store 0-999. It is required to delete one number every two numbers, and cycle to the beginning at the end to continue running. Find the original subscript position of the last deleted number.
Take 8 numbers as an example:
{0,1,2,3,4,5,6,7}: 0 - > 1 - > 2 (deleted) - > 3 - > 4 - > 5 (deleted) - > 6 - > 7 - > 0 (deleted)...
public class Demo01 {
    public static void main(String[] args) {
        int[] array = new int[1000];
        for(int i=0; i<1000; i++){
            array[i] = i;
        }
        //Indicates the subscript corresponding to the array element
        int i = 0;
        //When the count value is 3, it is judged that this element can be deleted, and then set the count to 0, otherwise the count will increase automatically
        int count = 0;
        //Record the number of elements deleted
        int delNum = 0;
        while(true){
            if(-1 != array[i]) {
                count++;
            }
            //If count==3, set the element to - 1, indicating deletion, and increment delNum automatically
            if(count == 3) {
                array[i] = -1;
                count = 0;
                delNum ++;
            }
            //When 999 elements have been deleted and one element remains to be deleted, you can exit the loop
            if(delNum == array.length - 1){
                break;
            }
            //When the subscript is over, start from the beginning
            if(++i == array.length){
                i = 0;
            }
        }
        //Find the last element that has not been deleted
        for(int j=0; j < array.length; j++){
            if(array[j] != -1){
                System.out.println("The last element not deleted is"+array[j]+",Subscript is"+j);
            }
        }
    }
}

2. Find the subscript of the specified element from the array through binary search

Write a method to binary search the int array
public static int binarySearch(int[] arrays, int des)
Parameter 1: arrays: int array passed in
Parameter 2: des: pass in the value to find
Return value: the index of the array of binary search results. If not found, return - 1

	public static int binarySearch(int[] arrays, int des){
        int left = 0;
        int right = arrays.length-1;
        int mid = (left+right)/2;
        while(left<=right){
            if(arrays[mid] == des) {
                return mid;
            } else if (des > arrays[mid]) {
                left = mid + 1;
            } else if (des < arrays[mid]) {
                right = mid - 1;
            }
            mid = (left+right)/2;
        }
        return -1;
    }

3. Find the specified element subscript from the string array through binary search

Write a method to binary search the String array
public static int binarySearch(String[] arrays, String des)
Parameter 1: arrays: String array passed in
Parameter 2: des: pass in the value to find
Return value: the index of the array of binary search results. If not found, return - 1

	public static int binarySearch(String[] arrays, String des) {
        int left = 0;
        int right = arrays.length-1;
        int mid = (left+right)/2;
        while(left<=right) {
            if(arrays[mid].equals(des)){
                return mid;
            } else if(des.compareTo(arrays[mid])>0){
                left = mid+1;
            } else {
                right = mid-1;
            }
            mid = (left+right)/2;
        }
        return -1;
    }

4. Find all prime numbers not greater than n

Use the program to generate prime numbers. Enter the parameter n and return all prime arrays not greater than n

public static int[] primeNumbers(int n){
        int[] arr = new int[n];
        int index =0;
        for(int i=2; i<n; i++) {
            boolean flag = true;
            int sqrt = (int)Math.sqrt(i);
            for(int j=2; j<=sqrt;j++){
                if(i%j==0){
                    flag=false;
                    break;
                }
            }
            if(flag){
                arr[index++] = i;
            }
        }
        return Arrays.copyOf(arr, index);
    }

5. Put the number n1 after n2

Please write code to implement the method int[] changPosition(int n1, int n2, int[]ii), where ii array elements do not have the same value
The method is to adjust the position of the number n1 in ii to the back of the number n2 and generate a new array, such as
int[] ii={1,3,4,8,9,10,5};
ii = changPosition(1,3,ii);
The value of ii becomes: {3,1,4,8,9,10,5}

public static int[] changPosition(int n1, int n2, int[] ii){
        //indexN1 determines the subscript of the n1 value
        int indexN1 = -1;
        for(int i=0;i<ii.length;i++){
            if(n1 == ii[i]){
                indexN1 = i;
                break;
            }
        }
        int indexN2 = -1;
        for(int i=0;i<ii.length;i++){
            if(n2 == ii[i]){
                indexN2 = i;
                break;
            }
        }
        //When the array index of n1 is less than that of n2
        if(indexN1 < indexN2){
            System.arraycopy(ii, indexN1+1, ii, indexN1, indexN2-indexN1);
            ii[indexN2] = n1;
        }
        //When the array index of n1 is greater than n2
        if(indexN1 > indexN2){
            System.arraycopy(ii, indexN2+1, ii, indexN2+2, indexN1-indexN2-1);
            ii[indexN2+1] = n1;
        }
        return ii;
    }

6. Find the subscript of the element after the array is moved

An ordered array A with A length of n (n > 2) moves circularly, that is, every time the element with index n-1 is moved to
At the position of index=0, the elements from the original index=0 to n-2 move to the original index+1 position in turn, and move m times
After, get a new array and give any element in the array. Please use a faster algorithm to find the name of this element
index.
For example, the array before moving is: [0,1,2,3,4,5,6,7,8,9], and after moving for 3 times, the array becomes [7,8,9,0,1,2,3,4,5,6], search
index where element 4 is located.

public class MoveElement {

    public static void main(String[] args) {
        int[] arr = {0,1,2,3,4,5,6,7,8,9};
        int m = 0;
        m %= 10;
        for(int i=0;i<m;i++){
            move(arr);
        }
        int value = 5;
        System.out.println(indexOf(arr, m==0?10:m, value));
    }
    public static int indexOf(int[] arr, int m, int value){
        if(value == arr[0]) {
            return 0;
        }
        if(value > arr[0]) {
            return Arrays.binarySearch(arr, 1, m, value);
        }
        if(value < arr[0]) {
            return Arrays.binarySearch(arr, m, arr.length, value);
        }
        return -1;
    }

    //Move the array element back one bit and the last element to the first.
    public static void move(int[] arr){
        int tmp = arr[arr.length-1];
        System.arraycopy(arr, 0, arr, 1, arr.length-1);
        arr[0] = tmp;
    }

}

Topics: Java Algorithm