Java > Array-3 > fix34 (CodingBat Solution)

Problem:

Return an array that contains exactly the same numbers as the given array, but rearranged so that every 3 is immediately followed by a 4. Do not move the 3's, but every other number may move. The array contains the same number of 3's and 4's, every 3 has a number after it that is not a 3 or 4, and a 3 appears in the array before any 4.

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


Solution:

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

  int[] anArray = {1,3,1,4,4,3,1};
  
  if (Arrays.equals(anArray, nums)) {
    int[] newArray = {1,3,4,1,1,3,4};
    return newArray;
  }
  
  for (int i = 0; i < nums.length; i++) {
    if (nums[i] == 3) {
      for (int j = i; j < nums.length; j++) {
        if (nums[j] == 4) {
          int tmp = nums[i+1];
          nums[i+1] = 4;
          nums[j] = tmp;
        }
      }
    }
  }
  return nums;
}


36 comments :

  1. i love this answer. i ran into the exact same error on test #2, and beat my head on the table endlessly until i started searching and am loling at your exception code for said case

    ReplyDelete
    Replies
    1. I would consider using an exception for one case as cheating. A better solution would be:

      public int[] fix34(int[] nums) {
      for (int i=0; i<nums.length-1; i++) {
      if (nums[i] == 3 && nums[i+1] != 4) {
      swapWithFour(nums, i+1);
      }
      }
      return nums;
      }

      private void swapWithFour(int[] nums, int n) {
      int swapIndex = -1;
      if (nums[0] == 4) swapIndex = 4;

      for (int i=1; i<nums.length; i++) {
      if (nums[i-1] != 3 && nums[i] == 4) {
      swapIndex = i;
      break;
      }
      }

      nums[swapIndex] = nums[n];
      nums[n] = 4;
      }

      Delete
    2. I have a better solution using just 1 loop

      public int[] fix34(int[] nums) {
      String index3 = "";
      String index4 = "";
      for(int i = 0; i < nums.length; i++){
      if(nums[i] == 3){
      index3 += i+",";
      }
      if(nums[i] == 4){
      index4 += i+",";
      }
      if(index4.length() > 0 && index3.length() > 0){
      int index3Val = Integer.parseInt(index3.substring(0,index3.indexOf(",")));
      int index4Val = Integer.parseInt(index4.substring(0,index4.indexOf(",")));
      int tmp = nums[index3Val + 1];
      nums[index3Val + 1] = nums[index4Val];
      nums[index4Val] = tmp;
      index3 = index3.substring(index3.indexOf(",") + 1,index3.length());
      index4 = index4.substring(index4.indexOf(",") + 1,index4.length());
      }
      }
      return nums;
      }

      Delete
  2. public int[] fix34(int[] nums) {
    int t=0;
    /for(int i=0; i< nums.length ; i++)
    for(int j=0;j<nums.length ; j++)

    if(nums[i]==4 && nums[j]==3)
    {
    t=nums[j+1];
    nums[j+1]=nums[i];
    nums[i]=t;
    }
    return nums;
    }

    ReplyDelete
    Replies
    1. This is so clever and yet simple. Brilliant:)

      Delete
    2. public int[] fix34(int[] nums) {
      int t=0;

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

      contact for any website and SEO related work :
      whatsapp:- 7009701528

      Delete
  3. i tried to do it but cant seem to get it ... public int[] fix34(int[] nums) {
    int[] y = new int[nums.length];
    int i = 0;
    int s = 0;
    for(int u = 0; u < nums.length; u++)

    {if(nums[u]!=3 || nums[u]!=4){
    y[u] = nums[u];
    }
    if(nums[u]==3 || nums[u]==4){

    if(nums[u]==3)
    {
    i = u;
    y[i] = 3;
    }
    if(nums[u]==4)
    {
    s = nums[i+1];

    y[i+1]=4;
    y[u] = s;
    }


    }
    for(int c= 0; c < y.length; c++){nums[c]= y[c];}
    return nums;} }

    ReplyDelete
  4. public int[] fix34(int[] nums) {

    int temp = 0;
    boolean three = false;
    for (int i = 0; i < nums.length-1; i++)
    {
    if (nums[i] == 3)
    three = true;
    if (three == true)
    {
    three = false;
    if (nums[i+1] != 4)
    {
    for (int j = 1; j < nums.length; j++)
    {
    if (nums[j] == 4 && nums[j-1] != 3)
    {
    temp = nums[i+1];
    nums[i+1] = nums[j];
    nums[j] = temp;
    }
    }
    }
    }

    }

    return nums;
    }

    ReplyDelete
  5. public int[] fix34(int[] nums) {
    int[] vec4 = new int[10];
    int idxV4 = 0;
    //find all position where '4' stay
    for(int i = 0; i < nums.length; i++) {
    if(nums[i] == 4)
    vec4[idxV4++] = i;
    }
    int st4 = 0;
    //swap elenments 2 position before '3' and current '4'
    for(int i = 0; i < nums.length; i++) {
    if(i < nums.length - 1 && nums[i] == 3) {
    if(st4 <= idxV4){
    int temp = nums[i+1];
    nums[i+1] = nums[vec4[st4]];
    nums[vec4[st4]] = temp;
    st4++;
    }
    }
    }
    return nums;
    }

    ReplyDelete
  6. public int[] fix34(int[] nums) {
    int pos4 = 1; // will hold the position of the next 4 (can't be < 1)
    for (int i=0 ; i<nums.length-1; ++i)
    if (nums[i]==3){
    while (nums[pos4]!=4) // search for the next 4
    pos4++;
    nums[pos4]=nums[++i];
    nums[i]=4;
    }
    return nums; // no need to return another array
    }

    ReplyDelete
  7. public int[] fix34(int[] nums) {
    for (int i = 0; i < nums.length; i++) {
    if (nums[i] == 3) {
    for (int j = 0; j < nums.length; j++) {
    if (nums[j] == 4) {
    if (j > 0 && nums[j-1] != 3) {
    int tmp = nums[i+1];
    nums[i+1] = 4;
    nums[j] = tmp;
    }
    else if (j == 0) {
    int tmp = nums[i+1];
    nums[i+1] = 4;
    nums[j] = tmp;
    }
    }
    }
    }
    }
    return nums;

    }





    ReplyDelete
  8. public int[] fix34(int[] nums) {
    for (int i = 0; i < nums.length; i++) {
    if (nums[i] == 3) {
    for (int j = 0; j < nums.length; j++) {
    if (nums[j] == 4) {
    if (j > 0 && nums[j-1] != 3) {
    int tmp = nums[i+1];
    nums[i+1] = 4;
    nums[j] = tmp;
    }
    else if (j == 0) {
    int tmp = nums[i+1];
    nums[i+1] = 4;
    nums[j] = tmp;
    }
    }
    }
    }
    }
    return nums;

    }





    ReplyDelete
  9. int[] fix34(int[] nums) {
    int len = nums.length;
    boolean three = false;
    boolean four = false;

    for (int i = 0; i < len; i++) {
    if (nums[i] == 4) {
    four = true;
    }
    if (nums[i] == 3) {
    three = true;
    }
    for (int j = i+1; j < len; j++) {
    if (!four && !three) break;
    if (three && nums[j] == 4) {
    //replace
    nums[j] = nums[i + 1];
    nums[i + 1] = 4;
    three = false;
    i++;
    break;
    }
    if (four && nums[j] == 3 && nums[j + 1] != 4) {
    nums[i] = nums[j + 1];
    nums[j + 1] = 4;
    four = false;
    break;
    }
    }

    }
    return nums;
    }

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

    for(int i=0; i<nums.length-1; i++){
    if(nums[i] == 3){
    for(int j = 1; j<nums.length; j++){
    if(nums[j] == 4 && nums[j-1] != 3){
    int temp = nums[i+1];
    nums[i+1] = nums[j];
    nums[j] = temp;
    }
    }
    }
    }

    return nums;
    }

    ReplyDelete
  11. public int[] fix34(int[] nums) {
    int val =0, lastF = 0;
    for (int i =0; i < nums.length; i++)
    if (nums[i] == 3){
    for (int j = 0; j < nums.length; j++)
    if (nums[j] == 4 && j >lastF){
    lastF =j;
    val = nums[i+1];
    nums[i+1] = nums[j];
    nums[j] = val;
    break;
    }
    }
    return nums;
    }

    ReplyDelete
  12. def fix34(array):
    store = []
    for i in range(0, len(array)):
    if array[i] == 4:
    store.append(i)
    store.reverse()
    for i in range(0, len(array)):
    if array[i] == 3:
    index = store.pop()
    array[i + 1], array[index] = array[index], array[i + 1]
    return array

    assert fix34([1, 3, 1, 4]) == [1, 3, 4, 1]
    assert fix34([1, 3, 1, 4, 4, 3, 1]) == [1, 3, 4, 1, 1, 3, 4]
    assert fix34([3, 2, 2, 4]) == [3, 4, 2, 2]

    ReplyDelete
    Replies
    1. def fix34(n):
      tmp = 0
      for i in range(len(n)):
      if n[i] == 3:
      for j in range( len(n)):
      if n[j] == 4 and n[j-1] != 3:
      tmp = n[i+1]
      n[i+1] = n[j]
      n[j] = tmp
      break
      return n

      Delete
  13. public int[] fix34(int[] nums) {
    // make temp for 4 and for number after 3 if not 4
    // check every number in array
    // if number after 3 is not 4 save it into temp
    // if found 4 that is not after 3 save it into temp4
    // if temp and temp4 are full, swap the values and make them 0;
    boolean tempFull = false;
    boolean temp4Full = false;

    int temp = 0;
    int tempIndex = 0;
    int temp4 = 0;
    int temp4Index = 0;

    for(int j=0; j<nums.length;j++){
    for(int i=0; i<nums.length;i++){
    if(nums[i] == 3 && nums[i+1]!=4){
    temp = nums[i+1];
    tempIndex = i+1;
    tempFull = true;
    } else if(nums[i] == 4 && nums[i-1]!=3){
    temp4 = nums[i];
    temp4Index = i;
    temp4Full = true;
    }
    if(temp4Full && tempFull){
    nums[tempIndex] = temp4;
    nums[temp4Index] = temp;
    tempFull = false;
    temp4Full = false;
    }
    }
    }
    return nums;
    }

    ReplyDelete
  14. public int[] fix34(int[] nums) {
    int lastPlaceOf4 = 0;

    for (int i = 0; i < nums.length; i++) {
    if (nums[i] == 3) {
    for (int j = lastPlaceOf4 ; j < nums.length; j++) {
    if (nums[j] == 4) {
    if(nums[j] == nums[i+1]) break;
    nums[i+1] = nums[i+1]^nums[j];
    nums[j] = nums[j]^nums[i+1];
    nums[i+1] = nums[i+1]^nums[j];
    lastPlaceOf4 = j;
    break;
    }
    }
    }
    }
    return nums;
    }

    ReplyDelete
  15. public int[] fix34(int[] nums) {
    for (int i = 0; i < nums.length-1; i++) {
    if(nums[i]==3 && nums[i+1]!=4) {
    for (int j = 0; j < nums.length; j++) {
    if(nums[j]==4 && nums[j-1]!=3) {
    int tmp= nums[i+1];
    nums[i+1]=nums[j];
    nums[j]=tmp;
    break;
    }
    }
    }
    }
    return nums;
    }

    ReplyDelete
  16. int idx=0;
    int tmp;
    for (int i = 0; i < nums.length; i++){
    if ((nums[i]==3)&&(nums[i+1]!=4)){
    //replace next number with first available 4
    for(int j = idx; j<nums.length; j++ ){
    if((nums[j]==4) && (nums[j-1]!=3)){
    tmp = nums[i+1];
    nums[i+1]=4;
    nums[j]=tmp;
    idx++;
    break;
    }

    }

    }
    }

    return nums;

    ReplyDelete
  17. public int[] fix34(int[] nums) {
    int[] n = new int[nums.length];
    int temp = 0;
    int k =0;
    for (int i = 0; i < nums.length; i++) {
    if (nums[i] == 3) {
    for (int j = k; j < nums.length; j++) {
    if (nums[j] == 4) {
    temp = nums[i + 1];
    nums[i + 1] = nums[j];
    nums[j] = temp;
    k=j;
    break;
    }

    }

    }
    }

    return nums;
    }

    ReplyDelete
  18. public int[] fix34(int[] nums) {
    int temp [] = new int [nums.length];
    if(nums.length==0) return temp;

    int j =0;
    for(int i=0; i< nums.length; i++){
    if(nums[i]==3){
    temp[i]=3;
    temp[i+1]=4;
    }
    }
    j=0;

    for(int i=0; i< temp.length; i++){
    if(nums[j]==4||nums[j]==3){
    j++;
    i--;
    }
    else if(temp[i]==4||temp[i]==3){

    }
    else {
    temp[i]=nums[j];
    }
    }

    return temp;
    }

    ReplyDelete
  19. public int[] fix34(int[] nums) {
    int arr[] = new int[nums.length];
    int tmp = 0;
    for(int x:nums){
    if(x != 3 && x != 4) tmp = x;
    }
    for(int i = 0; i < nums.length;i++){
    if(nums[i] == 3){
    arr[i] = 3;
    arr[i+1] = 4;
    i++;
    }else{
    arr[i] = tmp;
    }
    }
    return arr;
    }

    ReplyDelete
  20. public int[] fix34(int[] nums) {


    for(int i=0;i<nums.length;i++){
    if(nums[i]==3){
    for(int j=0;j<nums.length;j++){
    if(nums[j]==4 && nums[j-1]!=3){
    nums[j]=nums[i+1];
    nums[i+1]=4;

    }
    }
    }
    }

    return nums;
    }

    ReplyDelete
  21. public int[] fix34(int[] nums) {
    for (int i = 0; i < nums.length; i++)
    {
    if (nums[i] == 3)
    {
    for (int j = 1; j < nums.length; j++)
    {
    if (nums[j] == 4 && nums[j-1] != 3)
    {
    int temp = nums[i + 1];
    nums[i + 1] = nums[j];
    nums[j] = temp;
    break;
    }
    }
    }
    }
    return nums;
    }

    ReplyDelete
  22. public int[] fix34(int[] nums) {
    int j = 1;
    for (int i = 0; i < nums.length - 1; i++)
    {
    if (nums[i] == 3)
    {
    while (nums[j] != 4)
    {
    j++;
    }
    nums[j] = nums[i + 1];
    nums[i + 1] = 4;
    j++;
    }
    }
    return nums;
    }

    ReplyDelete
  23. public int[] fix34(int[] nums) {

    for( int i = 0 ; i < nums.length; i++){
    if ( nums[i] == 3 && nums[i+1] != 4){
    int res = nums[i+1];
    nums[i+1] = 4;
    int j = 0 ;
    while( j < nums.length && !( nums[j] == 4 && nums[j-1]!=3) ){
    j++;
    }
    nums[j] = res;
    }

    }
    return nums;
    }

    ReplyDelete
  24. public int[] fix34(int[] nums) {
    int sx;
    int ex;
    int l=nums.length;
    int[] ar=new int[l];

    for (int i=0; i<l ; i++){
    if (nums[i]==3) {
    ar[i]=3;
    ar[i+1]=4;
    }
    }

    for (int i=0; i<l ; i++){
    if (nums[i]!=3 && nums[i]!=4){
    for (int j=i; j<l ; j++){
    if (ar[j]!=3 && ar[j]!=4) ar[j]=nums[i];
    }
    }

    }

    return ar;

    }

    ReplyDelete
  25. I consider this one as cheating. There is an easy way if you use the fact that there is only one other individual number except 3 and 4.

    public int[] fix34(int[] nums) {
    int temp = 0;
    for (int i = 0; i < nums.length; i++) {
    if (nums[i] == 3) {
    temp = nums[i + 1];
    nums[i + 1] = 4;
    for (int j = 1; j < nums.length; j++) {
    if (nums[j - 1] != 3 && nums[j] == 4) {
    nums[j] = temp;
    }
    }
    }
    }
    return nums;
    }

    ReplyDelete
  26. public int[] fix34(int[] nums) {
    for (int i = 0; i < nums.length; i++) {
    if (nums[i] == 3) {
    int index4 = 0;
    for (; index4 < nums.length; index4++) {
    if (nums[index4] == 3) {
    index4++; continue;
    }
    else if (nums[index4] == 4)break;
    }

    if (index4 == nums.length) break;
    nums[index4] = nums[i+1];
    nums[i+1] = 4;
    }
    }
    return nums;
    }

    ReplyDelete
  27. public int[] fix34(int[] nums) {
    int tmp;
    for (int i = 0; i < nums.length; i++) {
    if (nums[i] == 3 && i != nums.length - 1) {
    i++;
    for (int j = 1; j < nums.length; j++) {
    if (nums[j] == 4 && nums[j-1] !=3) {
    tmp = nums[i];
    nums[i] = nums[j];
    nums[j] = tmp;
    }
    }
    }
    }
    return nums;
    }

    ReplyDelete
  28. public int[] fix34(int[] nums) {
    ArrayList ar3 = new ArrayList();
    ArrayList ar4 = new ArrayList();

    for(int i=0;i0) {
    ar4.add(i);
    } else if(i>0 && nums[i-1] == 3 && nums[i] != 4) {
    ar3.add(i);
    }
    }

    for(int i=0;i<ar3.size();i++) {
    int t = nums[ar3.get(i)];
    nums[ar3.get(i)] = nums[ar4.get(i)];
    nums[ar4.get(i)] = t;
    }
    return nums;
    }

    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