From 0209b07a352e7ccec09d4435c3298a3ca88b6b11 Mon Sep 17 00:00:00 2001 From: Aman Sharma Date: Wed, 17 Oct 2018 16:06:06 +0530 Subject: [PATCH] Almost Sorted solution --- almost-sorted.cs | 99 ++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 99 insertions(+) create mode 100644 almost-sorted.cs diff --git a/almost-sorted.cs b/almost-sorted.cs new file mode 100644 index 00000000..332be220 --- /dev/null +++ b/almost-sorted.cs @@ -0,0 +1,99 @@ +using System; +using System.Linq; +using System.Collections.Generic; +using System.Text; + +namespace ConsoleApplication +{ + class Program + { + public static void Main(String[] args) + { + int n = int.Parse(Console.ReadLine()); + int[] ar = Array.ConvertAll(Console.ReadLine().Trim().Split(' '), e => int.Parse(e)); + + if(isSorted(ar, n)) + { + Console.WriteLine("yes"); + return; + } + + int a = -1; + int b = -1; + + for(int i = 0; i < n - 1; i++) + { + if(ar[i] > ar[i + 1]) + { + a = i; + break; + } + } + + for(int i = n - 1; i > 0; i--) + { + if(ar[i] < ar[i - 1]) + { + b = i; + break; + } + } + + if(a != -1 && b != -1) + { + int temp = ar[a]; + ar[a] = ar[b]; + ar[b] = temp; + + if(isSorted(ar, n)) + { + Console.WriteLine("yes"); + Console.WriteLine($"swap {a + 1} {b + 1}"); + return; + } + + ar[b] = ar[a]; + ar[a] = temp; + } + + int[] nar = new int[n]; + + for(int i = 0; i < a; i++) + { + nar[i] = ar[i]; + } + + for(int i = 0; i <= b - a; i++) + { + nar[a + i] = ar[b - i]; + } + + for(int i = b + 1; i < n; i++) + { + nar[i] = ar[i]; + } + + if(isSorted(nar, n)) + { + Console.WriteLine("yes"); + Console.WriteLine($"reverse {a + 1} {b + 1}"); + return; + } + + Console.WriteLine("no"); + } + + public static bool isSorted(int[] ar, int n) + { + for(int i = 1; i < n; i++) + { + if(ar[i] < ar[i - 1]) + { + return false; + } + } + + return true; + } + } +}