VelizarAvramov

09. Extract Middle Elements

Nov 25th, 2018
115
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 1.26 KB | None | 0 0
  1. using System;
  2. using System.Linq;
  3.  
  4. namespace _09._Extract_Middle_Elements
  5. {
  6.     class ExtractMidleElements
  7.     {
  8.         static void Main(string[] args)
  9.         {
  10.             int[] n = Console.ReadLine().Split().Select(int.Parse).ToArray();
  11.  
  12.             int[] result = GetMiddle(n);
  13.             Console.WriteLine(string.Join(", ", result));      
  14.         }
  15.  
  16.         private static int[] GetMiddle(int[] n) // private static void => Console.WriteLine(", ", output)
  17.         {
  18.             int[] output;
  19.             int array = n.Length;
  20.             if (array == 1)
  21.             {
  22.                 output = new int[1];
  23.                 output[0] = n[0];
  24.                 return output;
  25.             }
  26.             else if (array % 2 == 0)
  27.             {
  28.                 output = new int[2];
  29.                 output[0] = n[array / 2 - 1];
  30.                 output[1] = n[array / 2];
  31.                 return output;
  32.             }
  33.             else
  34.             {
  35.                 output = new int[3];
  36.                 output[0] = n[array / 2 - 1];
  37.                 output[1] = n[array / 2];
  38.                 output[2] = n[array / 2 + 1];
  39.                 return output;          
  40.             }
  41.             //Console.WriteLine(string.Join(", ", output));
  42.         }
  43.     }
  44. }
Advertisement
Add Comment
Please, Sign In to add comment