ek4633
ek4633

Reputation: 23

Keeping the value of control variable same after iteration - Why isn't it working?

[Works fine with [3,2,1] and [1,2,3] but for the example in the image, it keeps on running.[1]

In the second for loop I need to keep the value of 's' same if the if statement is true. So, I decreased the value of 's', so that when it is incremented in the next step, it remains same. But it doesn't work. Why?

I give two arrays, say 3,2,1 and 1,2,3. I need to change the first array to second and calculate time needed. This I do by checking the first element. If same, then move to the next, else push the first element to the end, and the rest of the elements to one position above and repeat the process. Every time I push an element to the end I increment the value of 'time'.

The code works fine with [3,2,1] and [1,2,3], but when it comes to the example in the image it keeps on running.

import java.util.*;

public class Monkon {
   public static void main(String[] args) {
      Scanner scn = new Scanner(System.in);
      System.out.println("Enter the number of test cases");
      int t = Integer.parseInt(scn.nextLine());
      int[][] p = new int[t][2];
      int[] a = new int[t];
      int[] b = new int[t];
      int time = 0;
      for (int s = 1; s <= t; s++) {
         a[s - 1] = p[s - 1][0] = scn.nextInt();
         b[s - 1] = p[s - 1][1] = scn.nextInt();
      }
      for (int s = 1; s <= t; s++) {
         int c = a[s - 1];
         if (b[s - 1] != a[s - 1]) {
            for (int y = s; y < t; y++) {
               a[y - 1] = a[y];
            }
            a[t - 1] = c;
            time++;
            s = s - 1;
         } else
            time++;
      }
      System.out.println(time);
   }
}

Upvotes: 0

Views: 121

Answers (1)

Karthik
Karthik

Reputation: 5040

The problem with your code is, it will go in to infinite loop if you can not convert your 1st array in to the second array by the operation you have defined and that is the problem you are facing.

If you consider your example like {3,2,1} and {1,2,3} it terminates well and gives output.

If you give array like {1,2,3} and {1,2,4} it will go in to infinite loop as it is not possible convert first array into second array by any number of operations.

I think you are also reading the input incorrectly. I ran your program and you can see a screenshot here.

In the input {3,3,1,2,3,1,2} the first 3 is the input for number of elements (test cases in your program terminology).

In the remaining input alternate numbers will go into same array, because you are reading the input like

 a[s - 1] = p[s - 1][0] = scn.nextInt();
 b[s - 1] = p[s - 1][1] = scn.nextInt();

in the remaining {3,1,2,3,1,2}, the numbers which are bold will go in to first array - {3,1,2} and the other numbers will go in to second array - {1,3,2}.

This input gives 5 as answer.

UPDATE

To keep the things simple, I suggest you to replace

for (int s = 1; s <= t; s++) {
     a[s - 1] = p[s - 1][0] = scn.nextInt();
     b[s - 1] = p[s - 1][1] = scn.nextInt();
}

with

 for (int s = 1; s <= t; s++) {
     a[s - 1] = p[s - 1][0] = scn.nextInt();
  }
  for (int s = 1; s <= t; s++) {
     b[s - 1] = p[s - 1][1] = scn.nextInt();
  }

Upvotes: 1

Related Questions