Cleaning Up | CodeChef Solution

Hello coders, today we are going to solve Cleaning Up CodeChef Solution whose Problem Code is CLEANUP.

Cleaning Up

Task

After a long and successful day of preparing food for the banquet, it is time to clean up. There is a list of n jobs to do before the kitchen can be closed for the night. These jobs are indexed from 1 to n.

Most of the cooks have already left and only the Chef and his assistant are left to clean up. Thankfully, some of the cooks took care of some of the jobs before they left so only a subset of the n jobs remain. The Chef and his assistant divide up the remaining jobs in the following manner. The Chef takes the unfinished job with least index, the assistant takes the unfinished job with the second least index, the Chef takes the unfinished job with the third least index, etc. That is, if the unfinished jobs were listed in increasing order of their index then the Chef would take every other one starting with the first job in the list and the assistant would take every other one starting with the second job on in the list.

The cooks logged which jobs they finished before they left. Unfortunately, these jobs were not recorded in any particular order. Given an unsorted list of finished jobs, you are to determine which jobs the Chef must complete and which jobs his assitant must complete before closing the kitchen for the evening.

Input Format

The first line contains a single integer T ≤ 50 indicating the number of test cases to follow. Each test case consists of two lines. The first line contains two numbers n,m satisfying 0 ≤ m ≤ n ≤ 1000. Here, n is the total number of jobs that must be completed before closing and m is the number of jobs that have already been completed. The second line contains a list of m distinct integers between 1 and n. These are the indices of the jobs that have already been completed. Consecutive integers are separated by a single space.

Output Format

The output for each test case consists of two lines. The first line is a list of the indices of the jobs assigned to the Chef. The second line is a list of the indices of the jobs assigned to his assistant. Both lists must appear in increasing order of indices and consecutive integers should be separated by a single space. If either the Chef or the assistant is not assigned any jobs, then their corresponding line should be blank.

Example

Sample Input

3
6 3
2 4 1
3 2
3 2
8 2
3 8

Sample Output

3 6
5
1

1 4 6
2 5 7

Solution – Cleaning Up

C++

#include <iostream>
#include<algorithm>
using namespace std;

int main() {
    int T;
    cin>>T;
    while(T--)
    {
        int i;
        int total_no_of_jobs,jobs_completed;
        cin>>total_no_of_jobs;
        cin>>jobs_completed;
        int arr[jobs_completed];
        int x=total_no_of_jobs-jobs_completed;
        int arr1[x];
        int j=0;
        for(i=0;i<jobs_completed;i++)
        {
            cin>>arr[i];
        }
        sort(arr,arr+jobs_completed);
        int k=0;
        for(i=1;i<=total_no_of_jobs;i++)
        {
            if(i!=arr[k])
            {
                arr1[j]=i;
                j++;
              
            } 
            if(i==arr[k])
            {
                k++;
            }
            
        }
        for(i=0;i<x;i=i+2)
        {
            cout<<arr1[i]<<" ";
        }
        cout<<endl;
        for(i=1;i<x;i=i+2)
        {
            cout<<arr1[i]<<" ";
        }
        cout<<endl;
        
        
        
        
        
    }
	// your code goes here
	return 0;
}

Python

for i in range(int(input())):
    n,m=map(int,input().split())
    l1=[]
    for i in range(1,n+1):
        l1.append(i)
    l2=list(map(int,input().split()))
    l3=list(set(l1)-set(l2))
    l3.sort()
    chef=[]
    ass=[]
    for i in range(len(l3)):
        if i%2==0:
            chef.append(l3[i])
        else:
            ass.append(l3[i])
    print(*chef)
    print(*ass)

Java

/* package codechef; // don't place package name! */

import java.util.*;
import java.lang.*;
import java.io.*;

/* Name of the class has to be "Main" only if the class is public. */
class Codechef
{
	public static void main (String[] args) throws java.lang.Exception
	{
		Scanner sc = new Scanner(System.in);
        int T = sc.nextInt();
        while (T>0)
        {
            int n = sc.nextInt();
            int m = sc.nextInt();
            int[] arr = new int[n+1];
            int freq = 0;

            for (int i = 0; i < m; i++)
                arr[sc.nextInt()] = -1;

            for (int i = 1; i <= n; i++)
                if (arr[i]!=-1)
                {
                    if (freq%2==0)
                        System.out.print(i+" ");
                    freq++;
                }
            System.out.println("");
            freq=0;
            for (int i = 1; i <= n; i++)
                if (arr[i]!=-1)
                {
                    if (freq%2!=0) {
                        System.out.print(i+" ");
                    }
                    freq++;
                }

            System.out.println("");
            T--;
        }
	}
}

Disclaimer: The above Problem (Cleaning Up) is generated by CodeChef but the Solution is Provided by CodingBroz. This tutorial is only for Educational and Learning Purpose.

Leave a Comment

Your email address will not be published. Required fields are marked *