Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- //* Write a program that reads an array of integers and removes from it a minimal number of elements in such way that the remaining array is sorted in increasing order. Print the remaining sorted array. Example:
- // {6, 1, 4, 3, 0, 3, 6, 4, 5} {1, 3, 3, 4, 5}
- using System;
- using System.Collections.Generic;
- class IncreasingElements
- {
- static void Main()
- {
- int[] array = { 7,1,8,0,9,2 };
- List<int> sorted = new List<int>();
- sorted.Add(int.MinValue);
- for (int i = 0, j = i + 1; i < array.Length; i += 2, j += 2)
- {
- if (i==array.Length-1 && array[i]>=sorted[sorted.Count-1]) //to work for arrays with odd number of elements
- {
- sorted.Add(array[i]);
- break;
- }
- else if (i==array.Length-1)
- {
- break;
- }
- if (array[j]-array[i]==1 || array[i]<0) //to work for folowing numbers and negative numbers
- {
- sorted.Add(array[i]);
- sorted.Add(array[j]);
- continue;
- }
- if (Math.Min(array[i], array[j]) == array[i] && sorted[sorted.Count-1]<=array[i]) //if i is min and i is bigger than the last number in the list
- {
- sorted.Add(array[i]);
- }
- else if (array[i] < sorted[sorted.Count - 1] && array[j] >= sorted[sorted.Count - 1]) //if i is smaller than the last number in the list and j is greater than the last number in the list
- {
- sorted.Add(array[j]);
- continue;
- }
- if (Math.Min(array[i], array[j]) == array[j] && sorted[sorted.Count - 1] <= array[j]) //if j is min and j is bigger than the last number in the list
- {
- sorted.Add(array[j]);
- }
- else if (array[j] < sorted[sorted.Count - 1] && array[i] >= sorted[sorted.Count - 1])//if j is smaller than the last number in the list and i is greater than the last number in the list
- {
- sorted.Add(array[i]);
- }
- }
- foreach (var item in sorted)
- {
- if (item == int.MinValue)
- {
- continue;
- }
- Console.Write(item);
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment