Skip to content

Instantly share code, notes, and snippets.

@atiq-cs
Created October 31, 2017 20:02
Show Gist options
  • Save atiq-cs/652cdcc2df7df88fd44e1885f7a9edd4 to your computer and use it in GitHub Desktop.
Save atiq-cs/652cdcc2df7df88fd44e1885f7a9edd4 to your computer and use it in GitHub Desktop.
Find second smallest from the given collection
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