Interviews45 answers
I have a array like "{12,13,24,45,65,27,37,43,41,50,75,68}" Write a method or logic to get 3rd largest value in the array in only Single Loop is use.
45 Answers
static void Main(string[] args)
{
int[] a = { 12, 13, 24, 45, 65, 27, 37, 43, 41, 50, 75, 68, 98, 78 };
List largest = new List();
int l1 = 0, l2 = 0, l3 = 0;
for (int i = 0; i < a.Length; i++)
{
if (a[i] > l1)
{
l3 = l2;
l2 = l1;
l1 = a[i];
}
else if (a[i] > l2)
{
l3 = l2;
l2 = a[i];
}
else if (a[i] > l3)
{
l3 = a[i];
}
}
//display
Array.Sort(a);
foreach (var item in a)
{
Console.WriteLine(item);
}
Console.WriteLine("-----------");
Console.WriteLine(l3);
Console.Read();
}
Dima DPosted Jun 27, 2019, 3:33 PM EDT
public static void Main(string[] args)
{
int[] a = { 12, 13, 24, 45, 65, 27, 37, 43, 41, 50, 75, 68, 98, 78 };
Array.Reverse(a);
Console.WriteLine("3rd largest no : " a[2]);
}
kajal ranePosted Apr 9, 2019, 1:09 AM EDT
Array.Sort(arr);
Array.Reverse(arr);
Console.WriteLine(arr[2]);
Keshav ChauhanPosted Dec 30, 2016, 9:27 AM EST
write code for bubble sort to get highest array value then
write arr[2];
k vigPosted Dec 13, 2016, 3:21 AM EST
write code for bubble sort to get highest array value then
write arr[2];
k vigPosted Dec 13, 2016, 3:20 AM EST
public void getarray()
{
int[] a = { 12,13,24,45,65,27,37,43,41,50,75,68 };
Array.Sort(a);
Array.Reverse(a);
Response.Write(a[2]);
}
Awadhesh JhaPosted Dec 12, 2016, 8:44 PM EST
int[] Numbers = { 12, 13, 24, 45, 65, 27, 37, 43, 41, 50, 75, 68 };
int Result = Numbers.OrderByDescending(x => x).Skip(2).First();
Kanniyappan KrishPosted Oct 26, 2016, 4:13 AM EDT
Thanks for sharing valuable question.
Joginder BangerPosted Oct 3, 2016, 8:14 AM EDT
You can also achieve using Linq query
int a=arry.OrderBy(x => x).Skip(2).Take(1).SingleOrDefault();
Umesh KumarPosted Aug 19, 2016, 9:21 AM EDT
Array.Soft(a);
Response.Write(a[2]);
Ashish SrivastavaPosted Jul 8, 2016, 9:51 AM EDT
Dear sir manu shree for second largest print
Console.WriteLine(a[a.Length-2]); Console.Read();
Ashish MishraPosted Dec 24, 2015, 12:32 AM EST
int[] a = { 12, 13, 24, 45, 65, 27, 37, 43, 41, 50, 75, 68, 98, 78 };
Array.Sort(a);
int maxIndex = a[a.Length-3];
ChiranjeeviPosted Dec 17, 2015, 7:09 AM EST
List numbers = new List() { 12, 13, 24, 45, 65, 27, 37, 43, 41, 50, 75, 68 };
var highest = (from n in numbers
orderby n
select n).Skip(4).Take(1);
foreach (var n in highest)
Console.WriteLine("Value: " + n);
Shivshanker CheralPosted Nov 24, 2015, 1:12 AM EST
C by l
supraja kambamPosted Nov 13, 2015, 8:47 PM EST
int first = 0;
int second = 0;
int third = 0;
for (int i = 0; i < arr.Length; i++)
{
if (arr[i] > first)
{
third = second;
second = first;
first = arr[i];
}
if (arr[i] < first && arr[i] > second)
{
third = second;
second = arr[i];
}
if (arr[i] < first && arr[i] < second && arr[i] > third)
{
third = arr[i];
}
}
Console.WriteLine(string.Format("Largest:{0} , SecondLargest:{1} , ThirdLargest:{2}", first, second, third));
vipin kvPosted Oct 6, 2015, 3:12 AM EDT
@Swetha's Answer will not work here.we need to add Array.Reverse()
as follows.
static void Main(string[] args)
{
int[] a = { 12, 13, 24, 45, 65, 27, 37, 43, 41, 50, 75, 68, 98, 78 };
Array.Sort(a);
Array.Reverse(a);
Console.WriteLine(a[2]);
Console.Read();
}
Salahuddin FarooquiPosted Aug 12, 2015, 11:49 PM EDT
Swetha Gandla and Abhijit Kakade
if you aill execute your code the output will 3rd smallest value first you have to reverse the array
or print Array_Name[N-2].
Rohit RanaPosted Jul 29, 2015, 9:08 PM EDT
/*You can find value without using any loop
Suppose the size of your loop is N.
call*/
Array.Sort(Array_Name);
Console.WriteLine(Array_Name[N-2]);
Rohit RanaPosted Jul 29, 2015, 9:04 PM EDT
static void Main(string[] args)
{
int[] a = { 12, 13, 24, 45, 65, 27, 37, 43, 41, 50, 75, 682, 982, 782 };
Array.Sort(a);
Array.Reverse(a);
Console.WriteLine(a[2]);
Console.Read();
}
Charmy GeorgePosted Jul 28, 2015, 2:20 AM EDT
static void Main(string[] args)
{
int[] a = { 12, 13, 24, 45, 65, 27, 37, 43, 41, 50, 75, 682, 982, 782 };
Array.Sort(a);
Array.Reverse(a);
Console.WriteLine(a[2]);
Console.Read();
}
Charmy GeorgePosted Jul 28, 2015, 2:20 AM EDT
static void Main(string[]args)
{
int[] a = { 12, 13, 24, 45, 65, 27, 37, 43, 41, 50, 75, 68, 98, 78 };
Array.Sort(a);
Console.WriteLine(a[2]);
Console.Read();
}
Amrutanshu PanigrahiPosted Jun 22, 2015, 2:41 AM EDT
static void Main(string[] args)
{
int[] arrayToSort = { 12,13,24,45,65,27,37,43,41,50,75,68 };
Array.Sort(arrayToSort );
Console.WriteLine(arrayToSort [2]);
Console.Read();
}
Amrutanshu PanigrahiPosted Jun 22, 2015, 2:40 AM EDT
static void Main(string[] args)
{
int[] arrayToSort = { 12,13,24,45,65,27,37,43,41,50,75,68 };
Array.Sort(arrayToSort );
Console.WriteLine(arrayToSort [2]);
Console.Read();
}
Amrutanshu PanigrahiPosted Jun 22, 2015, 2:39 AM EDT
static void Main(string[] args)
{
int[] arrayToSort = { 12,13,24,45,65,27,37,43,41,50,75,68 };
Array.Sort(arrayToSort );
Console.WriteLine(arrayToSort [2]);
Console.Read();
}
Amrutanshu PanigrahiPosted Jun 22, 2015, 2:39 AM EDT
static void Main(string[] args)
{
int[] arrayToSort = { 12,13,24,45,65,27,37,43,41,50,75,68 };
Array.Sort(arrayToSort );
Console.WriteLine(arrayToSort [2]);
Console.Read();
}
Amrutanshu PanigrahiPosted Jun 22, 2015, 2:39 AM EDT
efe
Abhishek RanjanPosted Jun 14, 2015, 2:16 AM EDT
static void Main()
{
Console.WriteLine("Array initialization");
int[] arr = { 12, 13, 24, 45, 65, 27, 37, 43, 41, 50, 75, 68 };
Console.WriteLine("array reversing");
Array.Reverse(arr);
foreach (int i in arr)
Console.WriteLine(i);
Console.WriteLine("Third Largest Value is :" + arr[2]);
}
veera reddyPosted Jun 6, 2015, 10:05 AM EDT
static void Main(string[] args)
{
int[] arrayToSort = { 12,13,24,45,65,27,37,43,41,50,75,68 };
Array.Sort(arrayToSort );
Console.WriteLine(arrayToSort [2]);
Console.Read();
}
because array start from 0 index and you want 3rd largest.
Upendra Pratap ShahiPosted Jun 4, 2015, 11:43 PM EDT
int[] Ar = { 12, 13, 24, 45, 65, 27, 37, 43, 41, 50, 75, 68, 98, 78 };
Array.Sort(Ar);
Console.WriteLine(Ar[Ar.Length-3]);
Rahul PrajapatPosted May 27, 2015, 4:51 PM EDT
List aList = new List;
aList.add(array);
aList.sort();
foreach(int a in aList)
{
int i = a[array.length-3];
}
console.writeline(i);
Safayat ZisanPosted Apr 22, 2015, 10:26 AM EDT
int[] a = { 12, 13, 24, 45, 65, 27, 37, 43, 41, 50, 75, 68, 98, 78 };
Array.Sort(a);
Array.Reverse(a)
a[2]
Lalit RaghavPosted Apr 9, 2015, 1:52 AM EDT
int[] Ar = { 12, 13, 24, 45, 65, 27, 37, 43, 41, 50, 75, 68, 98, 78 };
Array.Sort(Ar);
Console.WriteLine(Ar[Ar.Length-3]);
Pankaj Kumar ChoudharyPosted Mar 23, 2015, 7:29 PM EDT
int[] Ar = { 12, 13, 24, 45, 65, 27, 37, 43, 41, 50, 75, 68, 98, 78 };
Array.Sort(Ar);
Console.WriteLine(Ar[Ar.Length-3]);
Pankaj Kumar ChoudharyPosted Mar 23, 2015, 7:29 PM EDT
int[] arr = {12,20,31,4,50,63,43,22,18 };
Array.Reverse(arr);
int thirdhighestvalue = arr[2];
Ben BenPosted Mar 19, 2015, 2:16 AM EDT
Note that almost all answers below (except Harish Sady's) use the built in C# .net functions to first 'sort' the array.
Sorting the array will take O(n log n) time.
Instead, simply use 'three' passes of Bubble Sort to get the 3rd largest number in O(n) time.
Vaibhav SinghPosted Mar 11, 2015, 8:01 AM EDT
class Program
{
static void Main(string[] args)
{
int []array = { 12, 13, 24, 45, 65, 27, 37, 43, 41, 50, 75, 68 };
var maxthreevalue= array.AsEnumerable().OrderByDescending(o=>o).Take(3).ToArray();
foreach (var display in maxthreevalue)
{
Console.WriteLine(display);
}
}
}
Abrar Ahmad AnsariPosted Mar 10, 2015, 2:16 AM EDT
""
Deepti AbrolPosted Feb 25, 2015, 10:30 PM EST
List list =new List() { 12, 13, 24, 45, 65, 27, 37, 43, 41, 50, 75, 68 };
list.Sort();
Console.WriteLine(" Get 3rd Larget value is "+ list[list.Count-3]);
Suresh MogudalaPosted Jan 13, 2015, 9:34 PM EST
int[] items = { 12,13,24,45,65,27,37,43,41,50,75,68};
int result= items.OrderByDescending(x => x).Skip(2).First();
Console.WriteLine("Third largest value is: {0} ", result);
Kanniyappan KrishPosted Jan 12, 2015, 8:40 PM EST
static void Main(string[] args)
{
int[] arr = { 12, 13, 24, 45, 65, 27, 37, 43, 41, 50, 75, 68 };
var thirdHighest = (from first in ((from third in arr orderby third descending select third).Take(3)) orderby first ascending select first).Take(1);
Console.WriteLine("3rd largest value:{0}",thirdHighest.ToArray()[0]);
Console.ReadKey();
}
Venkatesh MPosted Jan 12, 2015, 12:34 AM EST
int[] values = { 12, 13, 24, 45, 65, 27, 37, 43, 41, 50, 75, 68 };
Array.Sort(values);
print(values[values.Count()-3].ToString());
Nilesh PrajapatiPosted Jan 12, 2015, 12:30 AM EST
static void Main(string[] args)
{
int[] a = { 12, 13, 24, 45, 65, 27, 37, 43, 41, 50, 75, 68, 98, 78 };
Array.Sort(a);
Console.WriteLine(a[2]);
Console.Read();
}
Manu SreePosted Dec 21, 2014, 6:26 PM EST
Hi Jitendra,
Don't need loop here.. if its array follow below steps.
1) Array.Sort(Arrayname);
2) Print -- ArrayName[2]
check this and let me know.
Abhijit KakadePosted Dec 21, 2014, 12:28 AM EST
static void Main(string[] args)
{
int[] a = {12, 13, 24, 45, 65, 27, 37, 43, 41, 50, 75, 68, 98, 78 };
List a_list = new List { };
for(int i = 0; i < a.Length; i++)
{
a_list.Add(a[i]);
}
// find and remove the first two largest numbers in array
int greatest_int = a_list.Max();
a_list.Remove(greatest_int);
greatest_int = a_list.Max();
a_list.Remove(greatest_int);
//print the result
Console.WriteLine("Third largest number in array: " + a_list.Max());
Console.ReadLine();
}
Keith FoglePosted Dec 20, 2014, 4:50 AM EST
static void Main(string[] args)
{
int[] a = {12, 13, 24, 45, 65, 27, 37, 43, 41, 50, 75, 68,98,78 };
int a1 = 0, a2 = 0, a3 = 0,a4=0;
foreach (int item in a)
{
if (item > a1)
{
a3 = a2;
a2 = a1;
a1 = item;
}
if (item < a1 && item > a2)
{
a3 = a2;
a2 = item;
}
if (item < a1 && item < a2 && item > a3)
{
a3 = item;
}
}
Console.WriteLine(a1 "---------" a2 "--------" a3 "--------" a4);
Console.ReadLine();
}
------------------ you will get your answer in "a3"-------------------------
Harieswaran DPosted Dec 18, 2014, 12:47 AM EST















