Advertisement
Guest User

Untitled

a guest
Dec 29th, 2013
62
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 1.24 KB | None | 0 0
  1. //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
  2. //in the array which is ≤ K.
  3. using System;
  4. using System.Collections.Generic;
  5.  
  6. class LargestNumberK
  7. {
  8.     static void Main()
  9.     {
  10.         int n = int.Parse(Console.ReadLine());
  11.         int k = int.Parse(Console.ReadLine());
  12.  
  13.         int[] arr = new int[n];
  14.  
  15.         string inputArrayOne = Console.ReadLine();
  16.         string[] inputArrayTwo = inputArrayOne.Split(' ');
  17.        
  18.         for (int i = 0; i < arr.Length; i++)
  19.         {
  20.             arr[i] = int.Parse(inputArrayTwo[i]);
  21.         }
  22.        
  23.         Array.Sort(arr);
  24.         Array.BinarySearch(arr,k);
  25.  
  26.         int elementIndex = Array.BinarySearch(arr, k);
  27.  
  28.         if (arr[0] > k)
  29.         {
  30.             Console.WriteLine("There isn't such a number!!!");
  31.         }
  32.         else
  33.         {
  34.             if (elementIndex < 0)
  35.             {
  36.                 elementIndex = ~(elementIndex) - 1;
  37.                 Console.WriteLine(arr[elementIndex]);
  38.             }
  39.             else
  40.             {
  41.                 Console.WriteLine(arr[elementIndex]);
  42.             }
  43.         }                      
  44.     }
  45. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement