Cognizant Genc Next coding assessment(Help me in solving this question)

A school is organizing a picnic for all its students. There is a total of N students labeled from 1 to N in the school. Each student i has a compatibility factor of Xi

It is time for the picnic and all students to stand in a line. The student line is to be split into groups. A set of consecutive students standing in a line can form a group. For the picnic to be safe, each group must have at least 2 students with the same compatibility factor:

Finding the maximum number of groups that can be created.

Input Specification:

input1: N, denoting the number of students.

input2: An array of N elements where the ith element denotes the

compatibility factor of th student.

Output Specification:

Your function should return the maximum number of groups that can be


Example 1:

Input 1: 2
Input 2:{1,1}
Output: 1


Only 1 group exists which consists of first 2 students.

Example 2:

input1: 8
input2: (1,2,1,1,1,1,1,1)

Output: 3


The following 3 groups can be formed


You can check the solution here

Extremely simple code, such a solution is very light, so I don’t think anyone will need it

Can I get the implementation in Java?

#include <bits/stdc++.h>
using namespace std;

int main()
long long n, val ,ans = 0;

cin >> n;
unordered_map<int, int> m;

for (int i = 0; i < n; i++){
    cin >> val;
    if( m[val] == 2 ) {
cout << ans << endl;