Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System;
- class Program
- {
- static void Main(string[] args)
- {
- int n = int.Parse(Console.ReadLine());
- int maxNum = int.MinValue;
- int[] array = new int[n];
- for (int i = 0; i < array.Length; i++)
- {
- Console.Write("array[{0}] = ",i);
- array[i] = int.Parse(Console.ReadLine());
- }
- Console.Write("Enter K = ");
- int k = int.Parse(Console.ReadLine());
- Array.Sort(array);
- int index = Array.BinarySearch(array,k);
- for (int i = 0; i < n; i++)
- {
- if (array[i] == k)
- {
- Console.WriteLine(k);
- return; //if K exists in the array the program ends.
- }
- }
- for (int i = 0; i < n; i++) //If K doesnt exist we search the last number in the array which is < K.
- {
- if (array[i] < k)
- {
- maxNum = array[i];
- }
- }
- Console.WriteLine("K wasn't found! \nPrevious num is: {0}",maxNum);
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement