Problem:

Given a non-empty array of ints, return a new array containing the elements from the original array that come after the last 4 in the original array. The original array will contain at least one 4. Note that it is valid in java to create an array of length 0.

post4({2, 4, 1, 2}) → {1, 2}
post4({4, 1, 4, 2}) → {2}
post4({4, 4, 1, 2, 3}) → {1, 2, 3}

Solution:

public int[] post4(int[] nums) {
for (int i = nums.length-1; i >= 0; i--) {
if (nums[i] == 4) {
int[] foo;
foo = new int[nums.length-i-1];

for (int j = 0; j < foo.length; j++) {
foo[j] = nums[i+j+1];
}
return foo;
}
}

int[] bar;
bar = new int[0];
return bar;
}

1. public int[] post4(int[] nums) {
int result_length = 0;
int nums_length = nums.length;
for(int count=nums_length-1; count>0; count--){
if(nums[count]==4){
break;
}else{
result_length++;
}
}
int[] result = new int[result_length];
int nums_char_at_index = nums_length - result_length;
for(int count=0; count<result_length; count++){
result[count] = nums[nums_char_at_index];
nums_char_at_index++;
}
return result;
}

2. for(int i=nums.length-1; i>=0; i--){
if(nums[i] == 4){
return Arrays.copyOfRange(nums, i+1, nums.length);
}
}

return new int[0];

3. public int[] post4(int[] nums) {
int len = nums.length -1;
int index = 0;
for(int i=nums.length - 1; i >= 0;i--)
{
if(nums[i] == 4)
{
index = i;
break;
}
}
int[] arr = new int[len-index];
for(int i = index+1, j=0; i < nums.length; i++, j++)
{
arr[j] = nums[i];
}
return arr;
}

4. public int[] post4(int[] nums) {

int count = 0;
for(int i=0; i<nums.length; i++){
if(nums[i] == 4){
count = i;
}
}
int newCount = 0;
int[] newArray = new int[nums.length-1-count];
for(int j=count+1; j<nums.length; j++){
newArray[newCount] = nums[j];
newCount++;
}
return newArray;
}

5. What if i wanted to only return the even numbers that come after 4?

1. juz put one more if condition nums[i]%2==0

6. What if I wanted to return only even numbers after 4?

7. public int[] post4(int[] nums) {

int index = 0;
boolean got4 = false;

for (int i = nums.length - 1; i >= 0; i--) {

if (!got4 && nums[i] == 4) {
got4 = true;
index = i + 1;

}

}

return Arrays.copyOfRange(nums, index, nums.length);

8. public int[] post4(int[] nums)
{ int arr1[]={};
for(int i=nums.length-1;i>=0;i--)
{ if(nums[i]==4)
{ int len=nums.length-1-i;
int arr[]=new int[len];
for(int j=0;j<arr.length;j++)
{ arr[j]=nums[j+i+1];
}
return arr;
}
}return arr1;
}

9. Its a solution with 1 cycle:

public static int[] post4(int[] nums) {
int arr[] = new int[0];
boolean found = false;

int j = 0;
for (int i = nums.length-1; i >= 0; i--) {
if (!found && nums[i] == 4) {
found = true;
arr = new int[j];
i = nums.length - 1;
}
if(!found)
j++;
if (found && j > 0) {
j--;
arr[j] = nums[i];
}
}
return arr;
}

10. int[] post4 = {};
int index = 0;

for (int i = nums.length - 1; i >= 0; i--) {

if(nums[i] == 4) {
index = i;
post4 = new int[ (nums.length - 1) - index ];
break;
}
}

for(int i = 0; i < post4.length; i++) {

post4[i] = nums[index + 1];
index++;
}

return post4;

11. public int[] post4(int[] nums) {
int[] result;
int count = 0;

for (int i = nums.length - 1; nums[i] != 4 ; i--) {
count++;
}
result = new int[count];

for (int i = nums.length - count, j = 0; i < nums.length; i++, j++) {
result[j] = nums[i];
}
return result;
}

12. def post4(n):
L = []
for i in range(len(n)):
if 4 in n[i:len(n)]:
pass
else:
L.append(n[i])
return L

13. public int[] post4(int[] nums) {

int count = 0;
int i = nums.length-1;
int index = 0;

while(nums[i] != 4){
count++;
i--;
}

int [] num = new int[count];

for(int j = i + 1; j < nums.length ; j++){

num[index] = nums[j];
index++;
}

return num;

}

14. public int[] post4(int[] nums) {
int[] temp=new int[nums.length];
int k=0;
boolean found=false;
for(int i=0;i<nums.length;i++){
if(nums[i]==4){
k=0;
found=true;

}
else if(found==true&&nums[i]!=4){

temp[k]=nums[i];
k++;
}
}

int[] temp2=new int[k];
for(int i=0;i<k;i++)
temp2[i]=temp[i];

return temp2;
}

15. public int[] post4(int[] nums) {
for(int i= nums.length-1; i>=0; i--) {
if(nums[i]==4) {
int arr[]=new int[nums.length-(i+1)];
for(int j=0; j<arr.length;j++) {
arr[j]=nums[i+j+1];
}return arr;
}
}

return nums;
}

16. public int[] post4(int[] nums) {

int index = 0;

for(int i=nums.length-1 ; i>0 ; i--){
if(nums[i]==4){
index=i;
break;
}
}

int a[] = new int[nums.length-(index+1)];

for(int i=0 ; i<a.length ; i++){
a[i] = nums[index+1];
index++;

}

return a;
}