Team of Two Codechef Solution

Hello coders, today we are going to solve the Team of Two Codechef Solution whose Problem Code is TEAMOF2.

Team of Two Codechef Solution
Team of Two Codechef Solution

Team of Two Codechef Solution

Problem

Your class recently got a maths assignment with 5 questions. There are N( ≤ 20) students in the class and at most 2 people can collaborate. For each student, you know which problems they solved.

Find out if there exists a team of two students who can together solve all problems.

Input Format

The first line of input will contain a single integer T, denoting the number of test cases. T-test cases follow.

  • Each of the following test cases contains N + 1 lines, where N is the number of students in the class.
    • The first line contains a single integer N.
    • Each of the following NN lines contains Ki​ + 1 positive integers separated by whitespaces.
      • In the i^{th}ith line, the first positive integer Ki​ is the number of problems the ith student can solve. The next Ki​ integers are x1​,x2​,…,xKi​​, the indices of the problems this student can solve.

Output Format

The output must consist of T lines.

  • Each line must contain a single string: The solution to the ith test case as a YES or NO (where YES should be returned if some pairing of students is capable of solving all the problems, and NO otherwise).

You may print each character of the string in uppercase or lowercase (for example, the strings YESyEsyes, and yeS will all be treated as identical).

Constraints

Team of Two Codechef Solution

Sample 1

Input

4
3
1 3
1 1
4 1 3 4 5
2
4 2 3 4 5
3 1 2 4
3
1 1
1 2
1 3
2
2 1 5
3 2 3 4

Output

NO
YES
NO
YES

Explanation:

Test case 1: There is no student who solved the second question.

Test case 2: The second student can solve the first question and the first student can solve all the remaining problems, so they can form a team to solve all the problems together.

Test case 3: There is no student who solved fourth and fifth questions.

Test case 4: Given 2 people can collaborate to solve all the problems.

Team of Two Codechef Solution in CPP

#include <bits/stdc++.h>
#define ll long long int
using namespace std;
int solu(vector<int>a,vector<int>b) {
    set<int>st;
    for(int i=0;i<a.size();i++) {
        st.insert(a[i]);
    }
    for(int i=0;i<b.size();i++) {
        st.insert(b[i]);
    }
    int s=0;
    s = st.size();
    return s ;
}
int main() {
	// your code goes here
	int t;
	cin>>t;
	while(t--) {
	    int n;
	    cin>>n;
	    vector<vector<int>>vec_studen;
	    while(n--) {
	        vector<int>v;
	        int size;
	        cin>>size;
	        for(int i=0;i<size;i++) {
	            int q;
	            cin>>q;
	            v.push_back(q);
	        }
	        vec_studen.push_back(v);
	    }
	    //vector<int>ans;
	    bool flag = false;
	    for(auto i=vec_studen.begin();i!=vec_studen.end();i++) {
	        for(auto j=vec_studen.begin();j!=vec_studen.end();j++) {
	           if (solu(*i,*j)==5) {
	               flag=true;
	               break;
	           }
	        }
	    }
	    //cout<<*max_element(ans.begin(),ans.end());
	    //sort(ans.begin(),ans.end());
	    if(flag == true){
	        cout<<"YES"<<endl;
	    }
	    else {
	        cout<<"NO"<<endl;
	    }
	}
	return 0;
}
       

Team of Two Codechef Solution in JAVA

//Use this Code for Reference Only
import java.io.DataInputStream;
import java.io.PrintWriter;
import java.io.IOException;
public class Main {
    //Start of Logic
    public static void main(String[] args) throws IllegalArgumentException {
        initializeIO();
        int times = nextInt();
        while(times -- > 0) {
            int[][] data = new int[nextInt() + 1][],//To store the given data
                    ques = new int[6][];//To store data according to number of questions solved
            int[] quesC = new int[6];//To store how many questions a student answers
            //Storing data
            int temp = 0;
            for(int i = 1; i < data.length; i ++) {
                data[i] = new int[nextInt()];
                quesC[data[i].length] ++;
                for(int j = 0; j < data[i].length; j ++) {
                    data[i][j] = nextInt();
                }
                java.util.Arrays.sort(data[i]);
            }
            //Converting data to usable form
            for(int i = 0; i < quesC.length; i ++) {
                ques[i] = new int[quesC[i]];
            }
            for(int i = 1; i < data.length; i ++) {
                ques[data[i].length][-- quesC[data[i].length]] = i;
            }
            //Processing data
            boolean found = false;
      Loop: for(int i = 5; i > 2; i --) {
                if(ques[i].length == 0) {
                    continue;
                }
                for(int j = 0; j < ques[i].length - 1; j ++) {
                    for(int k = j + 1; k < ques[i].length; k ++) {
                        if(isAllElementsPresent(data[ques[i][j]], data[ques[i][k]])) {
                            found = true;
                            break Loop;
                        }
                    }
                }
                for(int j = i - 1; j + i >= 5; j --) {
                    if(ques[j].length == 0) {
                        continue;
                    }
                    for(int k = 0; k < ques[i].length; k ++) {
                        for(int l = 0; l < ques[j].length; l ++) {
                            if(isAllElementsPresent(data[ques[i][k]], data[ques[j][l]])) {
                                found = true;
                                break Loop;
                            }
                        }
                    }
                }
            }
            //Printing Answer
            if(found) {
                out.println("Yes");
            } else {
                out.println("No");
            }
        }
        close();
    }
    private static boolean isAllElementsPresent(int[] arr1, int[] arr2) {
        int count1 = 0, count2 = 0;
  Loop: for(int i = 1; i <= 5 ; i ++) {
            while(count1 < arr1.length && arr1[count1] <= i) {
                if(arr1[count1 ++] == i) {
                    continue Loop;
                }
            }
            while(count2 < arr2.length && arr2[count2] <= i) {
                if(arr2[count2 ++] == i) {
                    continue Loop;
                }
            }
            return false;
        }
        return true;
    }
    //End of Logic
    //Initialize IO Objects
    private static DataInputStream dis;
    private static byte[] buffer;
    private static int bytesRead, bufferPos;
    private static PrintWriter out;
    private static void initializeIO() {
        dis = new DataInputStream(System.in);
        buffer = new byte[1 << 16];
        out = new PrintWriter(System.out);
    }
    private static void close() {
        try {
            out.flush();
            dis.close();
            out.close();
        }
        catch(IOException e) {
        }
    }
    //For Input
    private static int nextInt() throws IllegalArgumentException {
        int temp = 0;
        byte c = skipSpace();
        boolean neg = (c == '-');
        if(neg) {
            c = read();
        }
		if(!checkIfDigit(c)) {
		    throw new IllegalArgumentException();
		} else {
   	     	do {
     			temp = temp * 10 + c - '0';
    			c = read();
    		} while(checkIfDigit(c));
		}
        return (neg) ? -temp : temp;
    }
    private static long nextLong() throws IllegalArgumentException {
		long temp = 0;
		byte c = skipSpace();
		boolean neg = (c == '-');
		if (neg) {
			c = read();
		}
		if(!checkIfDigit(c)) {
		    throw new IllegalArgumentException();
		} else {
   	     	do {
     			temp = temp * 10 + c - '0';
    			c = read();
    		} while(checkIfDigit(c));
		}
		return (neg) ? -temp : temp;
	}
	private static boolean checkIfDigit(byte c) {
	    return (c >= '0' && c <= '9');
	}
    private static byte skipSpace() {
        byte c = read();
        while(c == ' ' || c == '\n') {
            c = read();
        }
        return c;
    }
    private static String nextLine() {
        byte[] buff = new byte[64];
        int count = 0;
        byte c = skipSpace();
        while((c = read()) != -1) {
            if(c == '\n') {
                break;
            }
            buff[count ++] = c;
        }
        return new String(buff, 0, count);
    }
    private static void fillBuffer() throws IOException {
        bytesRead = dis.read(buffer, bufferPos = 0, buffer.length);
        if(bytesRead == -1) {
            buffer[0] = -1;
        }
    }
    private static byte read() {
        if(bufferPos >= bytesRead) {
            try {
                fillBuffer();
            }
            catch(IOException e) {
            }
        }
        return buffer[bufferPos ++];
    }
}

Team of Two Codechef Solution in Python

# Chase2learn
for _ in range(int(input())):
    n = int(input())
    a = []
    for i in range(n):
        solved = list(map(int, input().split()))[1:]
        a.append(solved)
    ans = 'no'
    for i in range(n):
        for j  in range(n):
            if len(set(a[i] + a[j])) == 5:
                ans = 'yes'
    print(ans)
    

Disclaimer: The above Problem (Team of Two) is generated by CodeChef but the solution is provided by Chase2learn.This tutorial is only for Educational and Learning purpose.

Sharing Is Caring