Advertisement
gadjov

Homework: Max Sequence of Equal Elements 100/100

Apr 11th, 2016
1,695
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 1.56 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using System.Threading.Tasks;
  6.  
  7. namespace MaxSequenceOfEqualElement
  8. {
  9.     class MaxSequenceOfEqualElement
  10.     {
  11.         static void Main(string[] args)
  12.         {
  13.             List<int> nums = Console.ReadLine().Split(' ').Select(int.Parse).ToList();
  14.             nums.Reverse();
  15.             int[] counts = new int[nums.Max() + 1];
  16.             int equal = 1;
  17.             int temp = 1;
  18.             bool isNegative = false;
  19.             for (int i = 0; i < nums.Count; i++)
  20.             {
  21.                 int num = nums[i];
  22.                 if (num < 0)
  23.                 {
  24.                     num *= -1;
  25.                     isNegative = true;
  26.                 }
  27.                 counts[num]++;
  28.                 temp = counts.Max();
  29.                 if (counts[num] == temp)
  30.                 {
  31.                     equal = num;
  32.                 }
  33.             }
  34.             if (isNegative == true)
  35.             {
  36.                 for (int i = 0; i < counts.Max(); i++)
  37.                 {
  38.                 Console.WriteLine($"-{equal} ");
  39.                 }
  40.             }
  41.             else
  42.             {
  43.                 if (counts.Max() == 1)
  44.                 {
  45.                     Console.WriteLine($"{nums.Last()}");
  46.                 }
  47.                 else
  48.                 {
  49.                     for (int i = 0; i < counts.Max(); i++)
  50.                     {
  51.                         Console.Write($"{equal} ");
  52.                     }
  53.                 }
  54.             }
  55.         }
  56.  
  57.     }
  58. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement