[Solved] Friendship Test with Java, C++, Python

Friendship Test: Michael is celebrating his 10th birthday and he wished to arrange a party to all his class mates. But there are n tough guys amongst his class who are weird. They thought that this is the best occasion for testing their friendship with him. They put up conditions before Michael that they will break the friendship unless he gives them a grand party on their chosen day. Formally, ith friend will break his friendship if he does not receive a grand party on dith day.

Michael is not a lavish spender and can give at most one grand party daily. Also, he wants to invite only one person in a party. So he just wonders what the maximum number of friendships he can save.
Please help Michael in this difficult task.

Input Format:
First line will contain a single integer denoting n. Assume that the maximum value for n as 50.
Second line will contain n space separated integers where ith integer corresponds to the day dith as given in the problem.

Output Format:
Print a single line corresponding to the maximum number of friendships Michael can save.
Refer sample input and output for formatting specifications.

Sample Input 1:
2
3 2

Sample Output 1:
2

Sample Input 2:
5
4 1 3 7 5

Sample Output 2:
5

Sample Input 3:
6
1 2 3 3 2 1
Sample Output 3:
3

Solution

import java.util.*;
public class Main {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        int n = sc.nextInt();
        int[] arr = new int[n];
        for (int i = 0; i < n; i++) {
            arr[i] = sc.nextInt();
        }
        //sauravhathi
        Set<Integer> set = new HashSet<>();
        for (int i = 0; i < n; i++) {
            set.add(arr[i]);
        }
        //sauravhathi
        System.out.println(set.size());
    }
}
#include <bits/stdc++.h>
using namespace std;

int main()
{
    int n, i;
    set<int> s;
    cin >> n;
    for (i = 0; i < n; i++)
    {
        int x;
        cin >> x;
        // sauravhathi
        s.insert(x);
    }
    cout << s.size() << endl;
    return 0;
}
n = int(input())
l = list(map(int,input().split()))
s = set()
for i in l:
    s.add(i)
    #sauravhathi
print(len(s))

Happy Learning – If you require any further information, feel free to contact me.

Share your love
Saurav Hathi

Saurav Hathi

I'm currently studying Bachelor of Computer Science at Lovely Professional University in Punjab.

📌 Nodejs and Android 😎
📌 Java

Articles: 444

Leave a Reply

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