Java > Array-2 > pre4 (CodingBat Solution)

Problem:

Given a non-empty array of ints, return a new array containing the elements from the original array that come before the first 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.

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


Solution:

public int[] pre4(int[] nums) {

  for (int i = 0; i < nums.length; i++) {
    
    if (nums[i] == 4 && i > 0) {
      int[] foo;
      foo = new int[i];
      for (int j = 0; j < foo.length; j++) {
        foo[j] = nums[j];
        
      }
      if (nums[0] != 4)
        return foo;
    }
  }
  int[] bar;
  bar = new int[0];
  return bar;
}


27 comments :

  1. int[] t = new int[0];
    for (int i = 0; i < nums.length; i++)
    {
    if (nums[i] == 4)
    {
    int[] tab = new int[i];
    for (int j = 0; j < i; j++)
    {
    tab[j] = nums[j];
    }
    return tab;
    }
    }
    return t;

    ReplyDelete
    Replies
    1. at what do you initialized t

      Delete
    2. How does this work? If array t is size zero and it appears to me that t does not interact with any other variables, how is the code valid?
      (I am very new to programming btw.)

      Delete
    3. see the t array is for other test cases to follow up where the value would be 0 array as a result.

      Delete
  2. any dork would do it with two cycles, try with one

    ReplyDelete
    Replies
    1. man can you show solution with one cycle, if you can?

      Delete
  3. for(int i=0; i<nums.length; i++){
    if(nums[i] == 4){
    return Arrays.copyOfRange(nums, 0, i);
    }
    }

    int[] temparr = new int[0];
    return temparr;

    ReplyDelete
  4. public int[] pre4(int[] nums) {
    int[] t = new int[nums.length];
    for(int i = 0;i<nums.length;i++){
    if(nums[i]==4){
    if(t.length==nums.length){
    t = new int[i];
    i=0;
    }
    else return t;
    if(t.length==0)
    return t;
    }
    t[i]=nums[i];
    }
    return t;
    }

    ReplyDelete
  5. smth. like this:

    public int[] pre4(int[] nums) {

    int[] res = new int[0];
    int resLen = 0;
    boolean found = false;

    for (int i = 0; i < nums.length; i++) {
    if (!found && nums[i] == 4) {
    found = true;
    resLen = i;
    res = new int[resLen];
    i = 0;
    }

    if (found && i < resLen)
    res[i] = nums[i];

    }

    return res;
    }

    ReplyDelete
  6. public int[] pre4(int[] nums) {
    int len =0;
    for (int i=0; i<nums.length; i++) {
    if(nums[i]==4) {
    len=i;
    break;
    //this break is important, can only get the first 4.
    }
    }
    int[] array = new int[len];
    for (int i =0; i<len; i++) {
    array[i]=nums[i];
    }
    return array;
    }

    ReplyDelete
  7. public int[] pre4(int[] nums) {

    int i = 0;

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

    return Arrays.copyOf(nums,i);

    }

    ReplyDelete
  8. public int[] pre4(int[] nums)
    {
    int fourI = -1;
    int[] result = new int[nums.length];
    for(int i = 0; i < nums.length; i++)
    {
    if(nums[i] == 4 && fourI == -1)
    {
    fourI = i;
    i = -1;
    result = new int[fourI];
    }
    else if(fourI != -1 && i < fourI)
    {
    result[i] = nums[i];
    }
    }
    return result;


    }

    ReplyDelete
  9. public int[] pre4(int[] nums) {
    int[] pre4;
    int counter = 0;
    for ( int i = 0; nums[i] != 4 ; i++) {
    counter++;
    }
    pre4 = new int[counter];
    for ( int i = 0; i < counter; i++) {
    pre4[i] = nums[i];
    }
    return pre4;
    }

    ReplyDelete
  10. public int[] pre4(int[] nums) {

    String res="";
    for(int i=0;i<nums.length;i++) {
    res+=nums[i];
    }

    res=res.substring(0,res.indexOf("4"));
    int[] re=new int[res.length()];
    for(int i=0;i<res.length();i++)
    re[i]=Integer.valueOf(res.charAt(i)+"");
    return re;
    }

    ReplyDelete
  11. public int[] pre4(int[] nums) {
    int i = 0;
    while(nums[i++] != 4);
    return Arrays.copyOf(nums, i-1);
    }

    ReplyDelete
  12. public int[] pre4(int[] nums) {
    int i = 0;
    while(nums[i]!=4){
    i++;
    }
    int[] result = new int[i];
    for (int j = 0; j < i; j++) {
    result[j] = nums[j];
    }
    return result;x
    }

    ReplyDelete
  13. public int[] pre4(int[] nums) {
    for(int i=0; i<nums.length; i++){
    if(nums[i] == 4){
    return Arrays.copyOfRange(nums, 0, i);
    }
    }

    return new int[0];
    }

    ReplyDelete
  14. public int[] pre4(int[] nums) {
    ArrayList arrs=new ArrayList();
    for(int num:nums){
    if(num%4==0){
    break;
    }
    arrs.add(num);
    }
    return arrs.stream().mapToInt(Integer::intValue).toArray();
    }

    ReplyDelete
  15. public static int[] pre4(int[] nums) {
    int[] newNums = new int[0];
    for(int i=0; i<nums.length; i++){
    if(nums[i]==4){
    newNums = new int[i];
    for(int j=0; j<i; j++){
    newNums[j]=nums[j];
    }
    break;
    }
    }
    return newNums;

    }

    ReplyDelete
  16. My solution is working fine, but how to do it with one loop, as CodingBat tells to in Array2 Chapter?


    public int[] pre4(int[] nums) {
    int index4 = 0;
    for (int i = 0; i < nums.length; i++) {
    if (nums[i] == 4) {
    index4 = i;
    break;
    }
    }
    int[] pre4 = new int[index4];
    for (int i = 0; i < pre4.length; i++) {
    pre4[i] = nums[i];
    }
    return pre4;
    }

    ReplyDelete
  17. public int[] pre4(int[] nums) {
    String be4 = "";
    for(int i= 0; i< nums.length; i++){
    if(nums[i] != 4){
    be4 += String.valueOf(nums[i]);
    }else{
    break;
    }
    }
    int[] pre4 = new int[be4.length()];
    for(int i = 0; i < be4.length(); i++) {
    pre4[i] = Integer.parseInt(String.valueOf(be4.charAt(i)));
    }
    return pre4;
    }

    ReplyDelete
  18. public int[] pre4(int[] nums) {
    int n = 0, brojac=0;;
    while(n<nums.length){
    if(nums[n]!=4){
    brojac++;
    }else{
    break;
    }
    n++;
    }
    int no[] = new int[brojac];
    int i = 0;
    while(i<nums.length){
    if(nums[i]!=4){
    no[i]=nums[i];
    }else{
    break;
    }
    i++;
    }
    return no;
    }
    //i did it this way..it is easier to read it!

    ReplyDelete
  19. With Java Stream

    public static int[] pre4(int[] nums) {
    return Arrays.stream(nums).takeWhile(i -> i != 4).toArray();
    }

    ReplyDelete
  20. public int[] pre4(int[] nums) {
    int index = 0;
    while(nums[index]!=4){
    index++;
    }
    int a[] = new int[index];

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

    return a;
    }

    ReplyDelete
  21. In one loop:
    public int[] pre4(int[] nums) {

    int length = nums.length;
    int[] result = new int[0];
    boolean bool = false;

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

    if (nums[0] == 4) {
    return result;
    }

    if (nums[i] == 4) {

    result = new int[i];
    bool = true;
    length = i;
    i = 0;

    }


    if (bool) {

    result[i] = nums[i];

    }

    }

    return result;

    }

    ReplyDelete

Follow Me

If you like our content, feel free to follow me to stay updated.

Subscribe

Enter your email address:

We hate spam as much as you do.

Upload Material

Got an exam, project, tutorial video, exercise, solutions, unsolved problem, question, solution manual? We are open to any coding material. Why not upload?

Upload

Copyright © 2012 - 2014 Java Problems  --  About  --  Attribution  --  Privacy Policy  --  Terms of Use  --  Contact