Chef and his Students Codechef Solution

Chef and his Students Codechef Solution: Chef is instructor of the famous course “Introduction to Algorithms” in a famous univerisity. There are n students in his class. There is not enough space in the class room, so students sit in a long hallway in a linear fashion.

One day Chef was late to class. As a result, some of the students have formed pairs and are talking to each other, while the others are busy studying. This information is given to you by a string s of length n, consisting of characters ‘*’, <‘ and ‘>’, where ‘*’ denotes that the student is studying, ‘>’ denotes that the corresponding student is talking to the student to the right, and ‘<‘ denotes that the corresponding student is talking to the student to the left.

For example, consider a sample configuration of students – *><*. Here students numbered 1 and 4 are busy studying, while the student 2 and 3 are talking to each other. In this example, ><><, student 1 and 2 are talking to each other, and 3 and 4 are also talking to each other. You are guaranteed that the given input is a valid configuration, i.e. <> can not be a valid string s, as here student 1 is shown to be talking to left, but there is no student to the left. Same is the case for right. Similarly, >><< is also not a valid configuration, as students 2 and 3 are talking to each other, so student 1 won’t be able to talk to student 2.

When the students see their teacher coming, those who were talking get afraid and immediately turn around, i.e. students talking to left have now turned to the right, and the one talking to right have turned to the left. When Chef sees two students facing each other, he will assume that they were talking. A student who is busy studying will continue doing so. Chef will call each pair of students who were talking and punish them. Can you find out how many pairs of students will get punished?

For example, in case *><*, when students see Chef, their new configuration will be *<>*. Chef sees that no students are talking to each other. So no one is punished. While in case ><><, the new configuration of students will be <><>, Chef sees that student 2 and 3 are talking to each other and they will be punished.


The first line of the input contains an integer T denoting the number of the test cases.

Each test case contains a string s denoting the activities of students before students see Chef entering the class.


For each test case, output a single integer denoting the number of pairs of students that will be punished.


  • 1 ≤ T ≤ 10
  • 1 ≤ |s| ≤ 105

Sample Input 1 


Sample Output 1 



Example case 1. The updated configuration will be <>. No students are talking to each other, so no one will be punished.

Example case 2 and 3. These examples are already explained in the problem statement.

Chef and his Students CodeChef Solution in JAVA

import java.util.*;
import java.lang.*;
/* Name of the class has to be "Main" only if the class is public. */
class Codechef
    static Scanner s = new Scanner(;
    static void chefstud( int r ) {
		String inp = "" ;
		int c = 0 ;
		while ( r >= 1 ) {
			inp =;
			char inpa[] = inp.toCharArray();
			for(int i=0;i<inp.length();i++)
					inpa[i] = '<';
				else if(inpa[i]=='<')
					inpa[i] = '>';
					inpa[i] = '*';
			for(int i=0;i<inp.length()-1;i++)
				if(inpa[i]=='>' && inpa[i+1]=='<' && inpa[i]!='*' && inpa[i+1]!='*')
					c = c + 1;
			r = r - 1;
			c = 0;
	public static void main (String[] args) throws java.lang.Exception
		// your code goes here
		int r = s.nextInt();

Chef and his Students CodeChef Solution in CPP

#include <bits/stdc++.h>
typedef long long ll;
#define Code ios_base::sync_with_stdio(false);
#define by cin.tie(NULL);
#define alpha_st cout.tie(NULL);
using namespace std;
void online_judge() {
	freopen("input.txt", "r", stdin);
	freopen("output.txt", "w", stdout);
ll gcd(ll a, ll b) {
	if (b == 0)return a;
	return gcd(b, a % b);
ll sod(ll n) {
	ll ans = INT_MIN, sum = 0;
	while (n > 0) {
		sum += n % 10;
		n /= 10;
	if (sum >= ans) {
		ans = sum;
	return ans;
signed main() {
	Code by alpha_st
	int t;
	cin >> t;
	while (t--) {
		string students;
		cin >> students;
		for (int i = 0; i < students.length(); i++) {
			if (students[i] == '*')continue;
			else if (students[i] == '>')students[i] = '<';
			else students[i] = '>';
		int naughty = 0;
		for (int i = 1; i < students.length(); i++) {
			if (students[i] == '<' && students[i - 1] == '>')naughty++;
		cout << naughty << '\n';
	return 0;

Chef and his Students CodeChef Solution in Python

for i in range(int(input())):
    s = list(input())
    c = 0
    for i in range(len(s)):
        if i+1 < len(s):
            if s[i]=="<" and s[i+1]==">":

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

Sharing Is Caring

Leave a Comment