Skip to content

Instantly share code, notes, and snippets.

@ajaypro
Last active February 14, 2020 06:57
Show Gist options
  • Select an option

  • Save ajaypro/dd608297061954f8132a6cdc08023f2a to your computer and use it in GitHub Desktop.

Select an option

Save ajaypro/dd608297061954f8132a6cdc08023f2a to your computer and use it in GitHub Desktop.
Problem solving Techiques code snippets
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