Click here to Skip to main content
15,796,738 members
Please Sign up or sign in to vote.
1.00/5 (2 votes)
See more:
> Code Question 2 Your team at company is working on a system that
> divides applications to a mixed cluster of computing devices. Each
> application is identified by an integer ID, requires a fixed non-zero
> amount of memory to execute, and is defined to be either a foreground
> or background application. IDs are guaranteed to be unique within
> their own application type, but not across types.
> Each device should be assigned two applications at once, one
> foreground application and one background application. Devices have
> limited amounts of memory and cannot execute applications that require
> more memory than the available memory. The goal of the system is to
> maximize the total utilization of the memory of a given device. A
> foreground/background application pair is considered to be optimal if
> there does not exist another pair that uses more memory than this
> pair, and also has a total less than or equal to the total memory of
> the device. For example, if the device has 10MB memory, a
> foreground/background pair using a sum total of 9MB memory would be
> optimal if there does not exist a pair that uses a sum total of 10 MB,
> but would not be optimal if such a pair did exist.
> Write an algorithm to find the sets of foreground and background
> application pairs that optimally utilize the given device for a given
> list of foreground applications and a given list of background
> applications.
> Input The input to the function/method consists of three arguments:
> deviceCapacity, an integer representing the maximum capacity of the
> given device; foregroundAppList, a list of pairs of integers where the
> first integer represents the unique ID of a foreground application and
> the second integer represents the amount of memory required by this
> application; backgroundAppList, a list of pairs of integers where the
> first integer represents the unique ID of a background application and
> the second integer represents the amount of memory required by this
> application.
> Output Return a list of pairs of integers representing the pairs of
> IDs of foreground and background applications that optimally utilize
> the given device [foregroundAppID,backgroundAppID]. If no pair is
> possible, return a list with empty pair - not just an empty list.


Example 1:
deviceCapacity = 7
foregroundAppList = [[1,2],[2,4],[3,6]]
backgroundAppList = [[1,2]]


> Explanation: There are only three combinations, [1,1], [2,1], and
> [3,1], which use a total of 4, 6, and 8 MB memory, respectively. Since
> 6 is the largest use that does not exceed 7, [2,1] is the only optimal
> pair.

deviceCapacity = 10
foregroundAppList = [[1, 3], [2, 5], [3, 7], [4, 10]]
backgroundAppList = [[1, 2], [2, 3], [3, 4], [4, 5]]

[[2, 4], [3, 2]]

> Explanation: There are two pairs of foreground and background
> applications possible that optimally utilizes the given device.
> Application 2 from foregroundAppList uses 5 memory units, and
> application 4 from backgroundAppList also uses 5 memory units.
> Combined, they add up to 10 units of memory. Similarly, application 3
> from foregroundAppList uses 7 memory units, and application 2 from
> backgroundAppList uses 3 memory units. These also add up to 10 units
> of memory. Therefore, the pairs of foreground and background
> applications that optimally utilize the device are [2, 4] and [3, 2].

deviceCapacity = 16
foregroundAppList = [[2, 7], [3, 14]]
backgroundAppList = [[2, 10], [3, 14]]


> Explanation: There exist no combination. So, the output is a list with
> empty pair.

What I have tried:

using System.CodeDom.Compiler;
    using System.Collections.Generic;
    using System.Collections;
    using System.ComponentModel;
    using System.Diagnostics.CodeAnalysis;
    using System.Globalization;
    using System.IO;
    using System.Linq;
    using System.Reflection;
    using System.Runtime.Serialization;
    using System.Text.RegularExpressions;
    using System.Text;
    using System;
    class Result
         * Complete the 'getServedBuildings' function below.
         * The function is expected to return an INTEGER.
         * The function accepts following parameters:
         *  1. INTEGER_ARRAY buildingCount
         *  2. INTEGER_ARRAY routerLocation
         *  3. INTEGER_ARRAY routerRange
        public static int getServedBuildings(List<int> buildingCount, List<int> routerLocation, List<int> routerRange)
     List<int> building = buildingCount();
     for(int i =0 ; i>=building.length;i++)
     int[] array1 = new int[] { 1, 3, 5, 7, 9 };
         return array1(); 
    class Solution
        public static void Main(string[] args)
            TextWriter textWriter = new StreamWriter(@System.Environment.GetEnvironmentVariable("OUTPUT_PATH"), true);
            int buildingCountCount = Convert.ToInt32(Console.ReadLine().Trim());
            List<int> buildingCount = new List<int>();
            for (int i = 0; i < buildingCountCount; i++)
                int buildingCountItem = Convert.ToInt32(Console.ReadLine().Trim());
            int routerLocationCount = Convert.ToInt32(Console.ReadLine().Trim());
            List<int> routerLocation = new List<int>();
            for (int i = 0; i < routerLocationCount; i++)
                int routerLocationItem = Convert.ToInt32(Console.ReadLine().Trim());
            int routerRangeCount = Convert.ToInt32(Console.ReadLine().Trim());
            List<int> routerRange = new List<int>();
            for (int i = 0; i < routerRangeCount; i++)
                int routerRangeItem = Convert.ToInt32(Console.ReadLine().Trim());
            int result = Result.getServedBuildings(buildingCount, routerLocation, routerRange);
Updated 3-Aug-22 6:10am
Richard Deeming 3-Aug-22 8:21am    
Nobody here is going to do your homework for you.
Maciej Los 3-Aug-22 13:35pm    
This is a code-request instead of question!

1 solution

While we are more than willing to help those that are stuck, that doesn't mean that we are here to do it all for you! We can't do all the work, you are either getting paid for this, or it's part of your grades and it wouldn't be at all fair for us to do it all for you.

So we need you to do the work, and we will help you when you get stuck. That doesn't mean we will give you a step by step solution you can hand in!
Start by explaining where you are at the moment, and what the next step in the process is. Then tell us what you have tried to get that next step working, and what happened when you did.

If you are having problems getting started at all, then this may help: How to Write Code to Solve a Problem, A Beginner's Guide[^]
Share this answer

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900