Last active
February 14, 2020 06:57
-
-
Save ajaypro/dd608297061954f8132a6cdc08023f2a to your computer and use it in GitHub Desktop.
Problem solving Techiques code snippets
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Moving to last element in array and moving towards left till we reach an even element | |
| int[] arr = {1, 2, 3, 4, 5}; | |
| int i = arr.length -1; | |
| while(i>=0 && arr[i] % 2 == 1) | |
| i--; | |
| ------------------------------------------------ | |
| removing element from array with given index | |
| int arr2[] = new int[arr.length -1]; | |
| /** | |
| * Copy the elements from starting till index from original array to the other array till index positio | |
| * to delete | |
| * | |
| */ | |
| System.arraycopy(arr, 0, arr2, 0, index); | |
| /** | |
| * Copy elements from arr1 to arr2 after index position (index +1) to other array. | |
| * now filling the remaining index positions of array such so we need (arr.length - index -1) gives | |
| * remaining index position to fill in. | |
| */ | |
| System.arraycopy(arr, index+1, arr2, index, arr.length -index -1); | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment