Q42 Completing tasks | Practice | GeeksforGeeks

PHOTO EMBED

Sat Jan 28 2023 09:09:02 GMT+0000 (Coordinated Universal Time)

Saved by @Ayush_dabas07

//{ Driver Code Starts

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

public class Main {

    public static void main(String[] args) throws Exception {
        BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
        int tc = Integer.parseInt(br.readLine().trim());
        while (tc-- > 0) {
            String[] inputLine;
            inputLine = br.readLine().trim().split(" ");
            int n = Integer.parseInt(inputLine[0]);
            int m = Integer.parseInt(inputLine[1]);
            int[] arr = new int[m];
            inputLine = br.readLine().trim().split(" ");
            for (int i = 0; i < m; i++) {
                arr[i] = Integer.parseInt(inputLine[i]);
            }

            Tasks ans = new Solution().findTasks(arr, m, n);
            for (Integer x : ans.tanya) {
                System.out.print(x + " ");
            }
            System.out.println();
            for (Integer x : ans.manya) {
                System.out.print(x + " ");
            }
            System.out.println();
        }
    }
}

class Tasks {
    ArrayList<Integer> tanya, manya;

    Tasks() {
        tanya = new ArrayList<>();
        manya = new ArrayList<>();
    }
}


// } Driver Code Ends


// check Tasks class in driver

class Solution {
    Tasks findTasks(int[] arr, int m, int n) {
        if(arr.length == 0) return new Tasks();
        
        //make a hashmap and store tasks which are already done
        HashSet<Integer>set = new HashSet<>();
        
        for(int val : arr)
        set.add(val);

        //make an object of class Tasks and add answers in its arraylist
        Tasks t = new Tasks();
        
        int count =0;	//for determining if the index is odd or even
        for(int i = 1 ;i <= n ;i++){
            boolean flag = false;
            
            if(set.contains(i))
            flag = true;
            
            if(flag == false){
                if(count%2==0)
                t.tanya.add(i);
            
                else
                t.manya.add(i);
                
                count++;
            }
            
            
        }
        
        
        return t;
    }
}















content_copyCOPY

https://practice.geeksforgeeks.org/problems/completing-tasks0454/1