Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- //4. Write a program that finds the maximal sequence of equal elements in an array
- //Example: {2, 1, 1, 2, 3, 3, 2, 2, 2, 1} -> {2, 2, 2}.
- // should cover all cases ...
- using System;
- class Program
- {
- static void Main()
- {
- int[] arr =
- //{ 1, 7, 3, 3, 5, 5 }; /*ok if(max != 0 && indexValue[i]!=0)*/
- { 1, 1, 3, 3, 5, 5 };// this check was hard
- //{ 1,2,3,4,5,6 };/*ok if (max == 0 )*/
- //{ 1, 1, 1,1,2, 2, 2, 2, 3, 3, 3,3 };/*ok*/
- //{2, 1, 1, 2, 3, 3, 2, 2, 2, 1} ;
- int[] counterMax = new int[arr.Length];// how many times element met in array
- int counterForArrCount = 0;// count repeated element
- int[] indexValue = new int[arr.Length];// indexator for repeated elements in arr[]
- for (int i = 0; i < arr.Length - 1; i++)// check all elements witout the last one
- {
- if (arr[i] == arr[i + 1])// here we check as well the last one
- {
- counterForArrCount++;
- counterMax[i] = counterForArrCount;//how many time met this element
- }
- else
- {
- counterForArrCount = 0; // back to zero
- counterMax[i] = counterForArrCount;
- } //no need separate check for last element -
- }
- int max = 0;// find max repeated elm
- for (int i = 0; i < counterMax.Length; i++)
- {
- if (counterMax[i] > max)
- {
- max = counterMax[i];
- }
- }// indexing if max repeated lenght is met more then 1
- for (int i = 0; i < counterMax.Length; i++) // smesto 0->1
- {
- if (counterMax[0] == max)//special check for case: { 1,1,3,3,5,5 };
- {
- indexValue[1] = 1;
- }
- if (counterMax[i] == max)
- {
- indexValue[i] = i;
- }
- }// print out
- for (int i = 0; i < indexValue.Length; i++)
- { // print just longest
- if (indexValue[i] > 0 && max != 0)
- {// get which walue to print prom original arr[]
- int getValue = indexValue[i];
- Console.Write("{");// just fancy
- for (int j = 0; j <= max; j++)
- {
- if (j < max)
- {
- Console.Write("{0}, ", arr[getValue]);
- }
- else if (j == max)
- {
- Console.Write("{0}", arr[getValue]);
- }
- }
- Console.WriteLine("}");// just fancy
- }
- if (max == 0) // special check for case { 1,2,3,4,5,6 }
- {
- int getValue = indexValue[i];
- Console.Write("{");// just fancy
- for (int j = 0; j <= max; j++)
- {
- if (j < max)
- {
- Console.Write("{0}, ", arr[getValue]);
- }
- else if (j == max)
- {
- Console.WriteLine("{0}", arr[getValue]);
- }
- }
- Console.WriteLine("}");// just fancy
- }
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement