Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 9124487
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 17, 20262026-06-17T06:34:57+00:00 2026-06-17T06:34:57+00:00

I am trying to solve my task using a List and I know I

  • 0

I am trying to solve my task using a List and I know I am very close to solving it but I am stuck now. Something is not ok in the code and I can not figure out what it is. Could you please take a look and help:

        /*
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 RemoveMinimalElements
{

    static void Main()
    {
        int n;
        n = int.Parse(Console.ReadLine());
        List<int> arr = new List<int>();
        List<int> sorted = new List<int>();
        int maxSubsetLenght = 0;

        for (int i = 0; i < n; i++)
        {
            arr.Add(int.Parse(Console.ReadLine()));
        }


        for (int i = 1; i <= (int)Math.Pow(2, n) - 1; i++)
        {
            int tempSubsetLenght = 0;
            string tempString = "";
            List<int> temp = new List<int>();


            for (int j = 1; j <= n; j++)
            {
                int andMask = i & (1 << j);
                int bit = andMask >> j;

                if (bit == 1)
                {
                    temp.Add(arr[n - 1 - j]);
                    tempSubsetLenght++;
                }
                if (tempSubsetLenght > maxSubsetLenght)
                {
                    maxSubsetLenght = tempSubsetLenght;

                    for(int k =1; k < temp.Count; k ++)
                    {
                        if (temp[k] >= temp[k - 1])
                        {
                            sorted = temp;
                        }
                    }
                }
            }
        }

        for (int i = sorted.Count - 1; i > 0; i--)
        {
            Console.WriteLine(sorted[i]);
        }
    }
}
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-17T06:34:58+00:00Added an answer on June 17, 2026 at 6:34 am

    I don’t know if an exhaustive search is suitable for your case, but will this work for you?

    static void Main(string[] args)
            {
                int[] input = new[] { 6, 1, 4, 3, 0, 3, 6, 4, 5 };
                int[] expectedOutput = new[] { 1, 3, 3, 4, 5 };
    
                int[] solution = TryGetSolution(input);
    
                Console.WriteLine("Input: " + FormatNumbers(input));
                Console.WriteLine("Expected Output: " + FormatNumbers(expectedOutput));
                Console.WriteLine("Output: " + FormatNumbers(solution));
                Console.ReadLine();
            }
    
            private static string FormatNumbers(int[] numbers)
            {
                return string.Join(", ", numbers);
            }
    
            private static int[] TryGetSolution(int[] input)
            {
                return TryWithoutAnyItem(input);
            }
    
            private static int[] TryWithoutAnyItem(int[] items)
            {
                return Enumerable.Range(0, items.Length)
                                 .Select(i => TryWithoutItem(items, i))
                                 .Where(solution => solution != null)
                                 .OrderByDescending(solution => solution.Length)
                                 .FirstOrDefault();
            }
    
            private static int[] TryWithoutItem(int[] items, int withoutIndex)
            {
                if (IsSorted(items)) return items;
                var removed = items.Take(withoutIndex).Concat(items.Skip(withoutIndex + 1));
                return TryWithoutAnyItem(removed.ToArray());
            }
    
            private static bool IsSorted(IEnumerable<int> items)
            {
                return items.Zip(items.Skip(1), (a, b) => a.CompareTo(b)).All(c => c <= 0);
            }
        }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Am trying to solve a labyrinth by DFS, using adj List to represent the
Recently I am trying to solve a problem using Foldr. The task is following:
I'm a junior programmer, and I'm trying to solve a task. Using c# .net
I'm trying to solve the following problem in Redis. I have a list that
I have a task - add round corners to HtmlPanelGrid. Now I am trying
I'm trying to solve next task: Given a set of items, each with a
I am trying to solve the following problem but cannot find an elegant solution.
I have a problem with a task which i am trying to solve. I
I know that we can load classes dynamically by using custom class loaders. But
I'm trying to solve a task that I have to do that is to

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.