From 0209b07a352e7ccec09d4435c3298a3ca88b6b11 Mon Sep 17 00:00:00 2001 From: Aman Sharma Date: Wed, 17 Oct 2018 16:06:06 +0530 Subject: [PATCH 1/3] 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; + } + } +} From f6246d95a3bcb8b0c9c3026d1f4665acacd787be Mon Sep 17 00:00:00 2001 From: Aman Sharma Date: Wed, 17 Oct 2018 16:10:59 +0530 Subject: [PATCH 2/3] Angry Children 2 solution --- angry-children-2.cs | 57 +++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 57 insertions(+) create mode 100644 angry-children-2.cs diff --git a/angry-children-2.cs b/angry-children-2.cs new file mode 100644 index 00000000..c70cbaa4 --- /dev/null +++ b/angry-children-2.cs @@ -0,0 +1,57 @@ +using System; +using System.Linq; +using System.Collections.Generic; +using System.Text; +using System.Numerics; +using System.Diagnostics; + +namespace ConsoleApplication +{ + + class Program + { + public static void Main(String[] args) + { + int n = int.Parse(Console.ReadLine()); + int k = int.Parse(Console.ReadLine()); + decimal[] ar = new decimal[n + 1]; + for(int i = 1; i <= n; i++) + { + ar[i] = int.Parse(Console.ReadLine()); + } + decimal[] s = new decimal[n + 1]; + Array.Sort(ar); + + for(int i = 1; i < n; i++) + { + s[i] = ar[i] + s[i - 1]; + } + + int val = 1 - k; + decimal sum = 0; + for(int i = 1; i <= k; i++) + { + sum += val*ar[i]; + val += 2; + } + + decimal prev = sum; + + for(int i = k + 1; i <= n; i++) + { + decimal newSum = (k - 1) * ar[i]; + newSum += prev; + newSum += (k - 1) * ar[i - k]; + newSum -= 2 * (s[i - 1] - s[i - k]); + + if(newSum <= sum) + { + sum = newSum; + } + prev = newSum; + } + + Console.WriteLine(sum); + } + } +} \ No newline at end of file From 6190b1f6a4b66aaf9fc55b9b7e480a67d69721ca Mon Sep 17 00:00:00 2001 From: Aman Sharma Date: Wed, 17 Oct 2018 16:17:21 +0530 Subject: [PATCH 3/3] String Reduction solution --- almost-sorted.cs | 99 --------------------------------------------- angry-children-2.cs | 57 -------------------------- string-reduction.cs | 50 +++++++++++++++++++++++ 3 files changed, 50 insertions(+), 156 deletions(-) delete mode 100644 almost-sorted.cs delete mode 100644 angry-children-2.cs create mode 100644 string-reduction.cs diff --git a/almost-sorted.cs b/almost-sorted.cs deleted file mode 100644 index 332be220..00000000 --- a/almost-sorted.cs +++ /dev/null @@ -1,99 +0,0 @@ -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; - } - } -} diff --git a/angry-children-2.cs b/angry-children-2.cs deleted file mode 100644 index c70cbaa4..00000000 --- a/angry-children-2.cs +++ /dev/null @@ -1,57 +0,0 @@ -using System; -using System.Linq; -using System.Collections.Generic; -using System.Text; -using System.Numerics; -using System.Diagnostics; - -namespace ConsoleApplication -{ - - class Program - { - public static void Main(String[] args) - { - int n = int.Parse(Console.ReadLine()); - int k = int.Parse(Console.ReadLine()); - decimal[] ar = new decimal[n + 1]; - for(int i = 1; i <= n; i++) - { - ar[i] = int.Parse(Console.ReadLine()); - } - decimal[] s = new decimal[n + 1]; - Array.Sort(ar); - - for(int i = 1; i < n; i++) - { - s[i] = ar[i] + s[i - 1]; - } - - int val = 1 - k; - decimal sum = 0; - for(int i = 1; i <= k; i++) - { - sum += val*ar[i]; - val += 2; - } - - decimal prev = sum; - - for(int i = k + 1; i <= n; i++) - { - decimal newSum = (k - 1) * ar[i]; - newSum += prev; - newSum += (k - 1) * ar[i - k]; - newSum -= 2 * (s[i - 1] - s[i - k]); - - if(newSum <= sum) - { - sum = newSum; - } - prev = newSum; - } - - Console.WriteLine(sum); - } - } -} \ No newline at end of file diff --git a/string-reduction.cs b/string-reduction.cs new file mode 100644 index 00000000..671d8477 --- /dev/null +++ b/string-reduction.cs @@ -0,0 +1,50 @@ +using System; +using System.Collections.Generic; +using System.IO; +class Solution { + + static int stringReduction (string a) { + int[] ar = new int[3]; + for(int i = 0; i < a.Length; i++) + { + if(a[i] == 'a') + { + ar[0]++; + }else if(a[i] == 'b') + { + ar[1]++; + } + else + { + ar[2]++; + } + } + + if((ar[0] == 0 && ar[1] == 0) || (ar[1]== 0 && ar[2] == 0)||(ar[0] == 0 && ar[2] == 0)) + { + return a.Length; + } + + if ((ar[0] % 2 == 0 && ar[1] %2== 0) && (ar[1] %2 == 0 && ar[2] %2== 0)) + { + return 2; + } + + if ((ar[0] % 2 == 1 && ar[1] % 2 == 1) && (ar[1] % 2 == 1 && ar[2] % 2 == 1)) + { + return 2; + } + + return 1; + } + + static void Main(String[] args) { + int res; + int _t_cases = Convert.ToInt32(Console.ReadLine()); + for (int _t_i=0; _t_i<_t_cases; _t_i++) { + String _a = Console.ReadLine(); + res=stringReduction(_a); + Console.WriteLine(res); + } + } +}