Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System;
- class SearchForK
- {
- /*Write a program, that reads from the console an array of N integers and an integer K,
- sorts the array and using the method Array.BinSearch()
- finds the largest number in the array which is ≤ K. */
- static void Main()
- {
- int n = int.Parse(Console.ReadLine());
- int k = int.Parse(Console.ReadLine());
- int[] a = new int[n];
- //read the array
- for (int i = 0; i < n; i++)
- {
- a[i] = int.Parse(Console.ReadLine());
- }
- //sort
- Array.Sort(a);
- //print the sorted array
- for (int i = 0; i < n; i++)
- {
- Console.WriteLine(a[i]);
- }
- //find the index of k
- int index = Array.BinarySearch(a, k);
- //if (index < 0) Console.WriteLine("There is no number that is less than or equal to k.");
- if (index < 0) Console.WriteLine(a[~index - 1]);
- else if (index == 0) Console.WriteLine(a[index]);
- else Console.WriteLine(a[index - 1]);
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement