Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- //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.
- using System;
- using System.Collections.Generic;
- class LargestNumberK
- {
- static void Main()
- {
- int n = int.Parse(Console.ReadLine());
- int k = int.Parse(Console.ReadLine());
- int[] arr = new int[n];
- string inputArrayOne = Console.ReadLine();
- string[] inputArrayTwo = inputArrayOne.Split(' ');
- for (int i = 0; i < arr.Length; i++)
- {
- arr[i] = int.Parse(inputArrayTwo[i]);
- }
- Array.Sort(arr);
- Array.BinarySearch(arr,k);
- int elementIndex = Array.BinarySearch(arr, k);
- if (arr[0] > k)
- {
- Console.WriteLine("There isn't such a number!!!");
- }
- else
- {
- if (elementIndex < 0)
- {
- elementIndex = ~(elementIndex) - 1;
- Console.WriteLine(arr[elementIndex]);
- }
- else
- {
- Console.WriteLine(arr[elementIndex]);
- }
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement