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; }
public int[] post4(int[] nums) {
ReplyDeleteint 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;
}
for(int i=nums.length-1; i>=0; i--){
ReplyDeleteif(nums[i] == 4){
return Arrays.copyOfRange(nums, i+1, nums.length);
}
}
return new int[0];
public int[] post4(int[] nums) {
ReplyDeleteint 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;
}
public int[] post4(int[] nums) {
ReplyDeleteint 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;
}
What if i wanted to only return the even numbers that come after 4?
ReplyDeletejuz put one more if condition nums[i]%2==0
DeleteWhat if I wanted to return only even numbers after 4?
ReplyDeletepublic int[] post4(int[] nums) {
ReplyDeleteint 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);
public int[] post4(int[] nums)
ReplyDelete{ 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;
}
Its a solution with 1 cycle:
ReplyDeletepublic 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;
}
int[] post4 = {};
ReplyDeleteint 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;
public int[] post4(int[] nums) {
ReplyDeleteint[] 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;
}
def post4(n):
ReplyDeleteL = []
for i in range(len(n)):
if 4 in n[i:len(n)]:
pass
else:
L.append(n[i])
return L
public int[] post4(int[] nums) {
ReplyDeleteint 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;
}
public int[] post4(int[] nums) {
ReplyDeleteint[] 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;
}
public int[] post4(int[] nums) {
ReplyDeletefor(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;
}
public int[] post4(int[] nums) {
ReplyDeleteint 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;
}
// with 1 reversed loop:
ReplyDeletepublic int[] post4(int[] nums) {
int[] result = new int[0];
boolean found_4 = false;
int first_index_4 = 0; // initialize
for (int i = nums.length - 1; i >= first_index_4; i--) {
if (nums[i] == 4 && found_4 == false) {
result = new int[nums.length - 1 - i];
found_4 = true;
first_index_4 = i;
i = nums.length - 1;
}
if (found_4 && i - first_index_4 - 1 >= 0) {
result[i - first_index_4 - 1] = nums[i];
}
}
return result;
}