Advertisement
APXOHT

Untitled

Jan 6th, 2013
426
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 1.47 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3.  
  4. class RemainingSortedArray
  5. {
  6.     static bool isSorted(List<int> list)
  7.     {
  8.         bool isSorted = true;
  9.  
  10.         for (int i = 0; i < list.Count - 1; i++)
  11.         {
  12.             if (list[i] > list[i + 1])
  13.             {
  14.                 isSorted = false;
  15.             }
  16.         }
  17.  
  18.         return isSorted;
  19.     }
  20.  
  21.     static void PrintList(List<int> list)
  22.     {
  23.         for (int i = 0; i < list.Count; i++)
  24.         {
  25.             Console.Write(list[i] + " ");
  26.         }
  27.         Console.WriteLine();
  28.     }
  29.  
  30.     static void Main()
  31.     {
  32.         List<int> arrayOfNumbers = new List<int>() { 6, 1, 4, 3, 0, 3, 6, 4, 5 };
  33.         List<int> result = new List<int>();
  34.  
  35.         int maxi = (int)Math.Pow(2, arrayOfNumbers.Count) - 1;
  36.         int maxElements = 0;
  37.  
  38.         for (int i = 1; i <= maxi; i++)
  39.         {
  40.             List<int> temporaryList = new List<int>();
  41.             int counter = 0;
  42.  
  43.             for (int j = 1; j <= arrayOfNumbers.Count; j++)
  44.             {
  45.                 if (((i >> (j - 1)) & 1) == 1)
  46.                 {
  47.                     temporaryList.Add(arrayOfNumbers[j - 1]);
  48.                     counter++;
  49.                 }
  50.             }
  51.  
  52.             if (counter > maxElements && isSorted(temporaryList))
  53.             {
  54.                 result = temporaryList;
  55.                 maxElements = counter;
  56.             }
  57.  
  58.             counter = 0;
  59.         }
  60.  
  61.         PrintList(result);
  62.     }
  63. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement