Created
October 31, 2017 20:02
-
-
Save atiq-cs/652cdcc2df7df88fd44e1885f7a9edd4 to your computer and use it in GitHub Desktop.
Find second smallest from the given collection
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| using System; | |
| class KthSmallest { | |
| int[] A; | |
| public KthSmallest() { | |
| } | |
| // representation should be taken, process input | |
| public void TakeInput() { | |
| A = Array.ConvertAll(Console.ReadLine().Split(), int.Parse); | |
| } | |
| public int GetSecondSmallest() { | |
| int first = int.MaxValue; | |
| int second = first; | |
| for (int i = 0; i < A.Length; i++) | |
| if (first > A[i]) { | |
| second = first; | |
| first = A[i]; | |
| } | |
| else if (second > A[i]) | |
| second = A[i]; | |
| return second; | |
| } | |
| } | |
| public class Solution { | |
| public static void Main() { | |
| KthSmallest demo = new KthSmallest(); | |
| demo.TakeInput(); | |
| Console.WriteLine("Second smallest is: " + demo.GetSecondSmallest()); | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment