Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System;
- using System.Linq;
- namespace _09._Extract_Middle_Elements
- {
- class ExtractMidleElements
- {
- static void Main(string[] args)
- {
- int[] n = Console.ReadLine().Split().Select(int.Parse).ToArray();
- int[] result = GetMiddle(n);
- Console.WriteLine(string.Join(", ", result));
- }
- private static int[] GetMiddle(int[] n) // private static void => Console.WriteLine(", ", output)
- {
- int[] output;
- int array = n.Length;
- if (array == 1)
- {
- output = new int[1];
- output[0] = n[0];
- return output;
- }
- else if (array % 2 == 0)
- {
- output = new int[2];
- output[0] = n[array / 2 - 1];
- output[1] = n[array / 2];
- return output;
- }
- else
- {
- output = new int[3];
- output[0] = n[array / 2 - 1];
- output[1] = n[array / 2];
- output[2] = n[array / 2 + 1];
- return output;
- }
- //Console.WriteLine(string.Join(", ", output));
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment