title: Need help to solve the hackerrank challenge
body: <p>I'm trying to solve an "Almost Sorted" challenge in hackerrank the problem is:</p>
  
  <p>Given an array with  elements, can you sort this array in ascending order using only one of the following operations?</p>
  
  <p>Swap two elements.
  Reverse one sub-segment.</p>
  
  <p>Input Format 
  The first line contains a single integer, , which indicates the size of the array. </p>
  
  <p>The next line contains  integers separated by spaces.</p>
  
  <p><strong>Sample Input #1</strong></p>
  
  <p>2<br>
  4 2  </p>
  
  <p><strong>Sample Output #1</strong></p>
  
  <p>yes<br>
  swap 1 2</p>
  
  <p><strong>Sample Input #2</strong></p>
  
  <p>3</p>
  
  <p>3 1 2</p>
  
  <p><strong>Sample Output #2</strong></p>
  
  <p>no</p>
  
  <p><strong>Sample Input #3</strong></p>
  
  <p>6</p>
  
  <p>1 5 4 3 2 6</p>
  
  <p><strong>Sample Output #3</strong></p>
  
  <p>yes</p>
  
  <p>reverse 2 5</p>
  
  <p>I tried to solve the challenge and my code is working but it seems it's to slow for big arrays. </p>
  
  <p>Kindly asking you to help me to find a better solution for mentioned problem.</p>
  
  <p>Below is my code:</p>
  
  <pre><code>import java.util.*;
  
  public class Solution
  {
      private static int[] arr;
  
      public static void main(String[] args)
      {
          Scanner in = new Scanner(System.in);
          int N = in.nextInt();
  
          arr = new int[N];
  
          for (int i = 0; i &lt; N; i++)
          {
              arr[i] = in.nextInt();
          }
  
          if (IsSorted(arr))
          {
              System.out.println("yes");
              return;
          }
  
          if(CheckSingleSwap(arr))
              return;
  
          if(CheckSingleReverse(arr))
              return;
  
          System.out.println("no");
      }
  
      private static boolean CheckSingleReverse(int[] arr)
      {
          int length = arr.length;
          int limit = length - 2;
          int current = 1;
  
          List&lt;Integer&gt; indexes = new ArrayList&lt;Integer&gt;();
  
          while (current &lt; limit)
          {
              for (int i = 0; i &lt; length; i++)
              {
                  int temp = current + i;
                  for (int j = i; j &lt;= temp &amp;&amp; temp &lt; length; j++)
                  {
                      indexes.add(j);
                  }
  
                  if (IsSorted(ReverseArrayPart(arr, indexes)))
                  {
                      System.out.println("yes");
                      System.out.println("reverse " + (indexes.get(0) + 1) + " " + (indexes.get(indexes.size() - 1) + 1));
  
                      return true;
                  }
                  indexes.clear();
              }
  
              current++;
          }
  
          return false;
      }
  
      private static int[] ReverseArrayPart(int[] arr, List&lt;Integer&gt; indexes)
      {
          int[] result = new int[arr.length];
          int[] arrayPart = new int[indexes.size()];
          int j = 0;
  
          for (int i = 0; i &lt; arr.length; i++)
          {
                  if (indexes.contains(i))
                  {
                      arrayPart[j] = arr[i];
                      j++;
                  }
  
              result[i] = arr[i];
          }
  
          for(int i = 0; i &lt; arrayPart.length / 2; i++)
          {
              int temp = arrayPart[i];
              arrayPart[i] = arrayPart[arrayPart.length - i - 1];
              arrayPart[arrayPart.length - i - 1] = temp;
          }
  
          j = 0;
          for (int i = 0; i &lt; result.length; i++)
          {
                  if (indexes.contains(i))
                  {
                      result[i] = arrayPart[j];
                      j++;
                  }
          }
  
          return result;
      }
  
      private static boolean CheckSingleSwap(int[] arr)
      {
          int count = 0;
          int[] B = Arrays.copyOf(arr, arr.length);
          Arrays.sort(B);
          List&lt;Integer&gt; indexes = new ArrayList&lt;Integer&gt;();
  
          for(int i = 0; i &lt; arr.length; i++)
          {
              if(arr[i] != B[i])
              {
                  count++;
                  indexes.add(i+1);
              }
          }
  
          if(count &gt; 2)
              return false;
  
          System.out.println("yes");
          System.out.println("swap " + indexes.get(0) + " " + indexes.get(1));
  
          return true;
      }
  
      private static boolean IsSorted(int[] arr)
      {
          int length = arr.length;
  
          for (int i = 0; i &lt; length - 1; i++)
          {
              if (arr[i] &gt; arr[i + 1])
              {
                  return false;
              }
          }
  
          return true;
      }
  }
  </code></pre>