Physical Address

304 North Cardinal St.
Dorchester Center, MA 02124

Friend or Girlfriend CodeChef Solution

Problem -Friend or Girlfriend CodeChef Solution

This website is dedicated for CodeChef solution where we will publish right solution of all your favourite CodeChef problems along with detailed explanatory of different competitive programming concepts and languages.

Friend or Girlfriend CodeChef Solution in C++14

#include <iostream>
using namespace std;
#define ll long long int
int main() {
	// your code goes here
	int t;
	cin>>t;
	while (t--)
	{
	    ll n;
	    cin>>n; 
	    string str;
	    char c;
	    cin>>str>>c;
	    ll ans =0;
	    ll count =0;
	    for (ll q=0; q<n; q++)
	    {
	        if (str[q]==c)
	        {
	            ans = ans + ((count*(count+1))/2);
	            count =0;
	        }
	        else
	        {
	            count ++;
	        }
	        
	    }
	    ans = ans + ((count*(count+1))/2);
	    n= (n*(n+1))/2;
	    cout<<n-ans<<endl;
	}
	return 0;
}

Friend or Girlfriend CodeChef Solution in PYTH 3


for _ in range(int(input())):
    n = int(input())
    s, x = input().split()
    l = [-1]
    not_sub = 0
    for i in range(len(s)):
        if (s[i] == x):
            l.append(i)
    l.append(n)
    for i in range(1, len(l)):
        k = l[i]-l[i-1]-1
        not_sub += k*(k+1)/2

    ans = (n*(n+1)/2)-not_sub
    print(int(ans))

Friend or Girlfriend CodeChef Solution in C

#include <stdio.h>
#include <string.h>
void sol()
{
    long long int n,sum,tol=0;
    scanf("%lli", &n);
    char s[n+2];
    scanf("%s", s);
    char c;

    do
    {
        scanf("%c", &c);
    }while(c==' ');

    long long int l=strlen(s);
    sum=(l*(l+1))/2;

    long long int i=0,y=0;
    while(i<l)
    {
        if(s[i]!=c)
        y++;
        else
        {
            tol+=(y*(y+1))/2;
            y=0;
        }
        i++;
    }
    if(y>0)
    tol+=(y*(y+1))/2;
    printf("%lli", sum-tol);
}
int main()
{
    int t;
    scanf("%i", &t);

    while(t--)
    {
        sol();
        printf("\n");
    }
}

Friend or Girlfriend CodeChef Solution in JAVA

/* package codechef; // don't place package name! */

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

/* Name of the class has to be "Main" only if the class is public. */
class Codechef
{
	public static void main (String[] args) throws java.lang.Exception
	{
		BufferedReader reader=new BufferedReader(new InputStreamReader(System.in));
		BufferedWriter writer=new BufferedWriter(new OutputStreamWriter(System.out));
		int tt=Integer.parseInt(reader.readLine().trim());
		while(tt-->0){
		    int n=Integer.parseInt(reader.readLine().trim());
		    String[] parts=reader.readLine().split("\\s+");
		    String s=parts[0];
		    char c=parts[1].charAt(0);
		    long total=0L;
		    for(int i=0;i<n;){
		        int j=i;
		        while(j<n && s.charAt(j)!=c){
		            j++;
		        }
		        if(i==j){
		            i++;
		        }else{
		            int l=j-i;
		            total+=(l*(l+1L))/2L;
		            i=j;
		        }
		    }
		    writer.write(((n*(n+1L))/2L-total)+"\n");
		}
		writer.flush();
	}
	
    
    private static int[] parseInt(String str) {
        String[] parts=str.split("\\s+");
        int[] ans=new int[parts.length];
        for(int i=0;i<ans.length;i++){
            ans[i]=Integer.parseInt(parts[i]);
        }
        return ans;
    }

    private static long[] parseLong(String str) {
        String[] parts=str.split("\\s+");
        long[] ans=new long[parts.length];
        for(int i=0;i<ans.length;i++){
            ans[i]=Long.parseLong(parts[i]);
        }
        return ans;
    }
}

Friend or Girlfriend CodeChef Solution in PYPY 3

for _ in range(int(input())):
    n = int(input())
    a, b = input().split()
    i = n-1
    ans = 0
    last = 0
    while(i>= 0):
        if a[i] == b:
            ans += n-i
            last = n-i
        else:
            ans += last
        i -=1
    print(ans)

Friend or Girlfriend CodeChef Solution in PYTH

t = int(raw_input())
for i in range(t):
	N = int(raw_input())
	st = raw_input().split()
	s = st[0]
	X = st[1]
	r = N*(N+1)/2
	n = 0
	for x in s:
		if x == X:
			r -= n*(n+1)/2
			n = 0
		else:
			n += 1
		# endif
	# endfor x
	r -= n*(n+1)/2
	print r
# endfor i

Friend or Girlfriend CodeChef Solution in C#

using System;

public class Test
{
	public static void Main()
	{
            int t = int.Parse(Console.ReadLine());
            while (t > 0)
            {
                int n = int.Parse(Console.ReadLine());
                string line = Console.ReadLine();
                string[] tokens = line.Split(' ');
                string s = tokens[0];
                char x = tokens[1][0];

                int lastPos = -1;
                long answer = 0;
                for (int i = 0; i < n; i++)
                {
                    if (s[i] == x)
                    {
                        if (lastPos == -1)
                        {
                            lastPos = i;
                        }
                        else
                        {
                            answer += (lastPos + 1) * (i - lastPos);
                            lastPos = i;
                        }
                    }
                }

                answer += (lastPos + 1) * (n - lastPos);
                Console.WriteLine(answer);
                t--;
            }
	}
}

Friend or Girlfriend CodeChef Solution in NODEJS

var input = ``;

function cacheInput(data) {
    input += data;
}

var lineNumber = 0;
function main() {
	let lines = input.split('\n');
	let noOfTestCases = Number(lines[lineNumber++]);
    
    // Go through each test case
    while(noOfTestCases--){
		// Input string length
		let strLen = Number(lines[lineNumber++]);
        let [str,ch] = lines[lineNumber++].split(' ');
        let ans = ((strLen)*(strLen+1))/2;
		count = 0;
		for(let i=0;i<str.length;i++){
			if(str[i] == ch){
				ans -= (count*(count+1))/2;
				count = 0;
			}else{
				count++;
			}
        }
        ans -= (count*(count+1))/2;

        console.log(ans);
	}
}

process.stdin.resume();
process.stdin.setEncoding('utf8');
process.stdin.on('data', cacheInput).on('end', main);

Friend or Girlfriend CodeChef Solution in GO

package main

import "fmt"

func main() {
	var i, n, t int64
	var s, dummy string
	var x rune
	fmt.Scanln(&t)
	for i = 0; i < t; i++ {
		fmt.Scanln(&n)
		fmt.Scanf("%s %c", &s, &x)
		fmt.Scanln(&dummy)
		fmt.Println(calSub(n, s, x))
	}
}

func calSub(n int64, s string, x rune) int64 {
	var m []int64
	var i, ans, prev, val int64
	ans = 0
	prev = 0
	for i = 0; i < n; i++ {
		if byte(x) == s[i] {
		    m = append(m, i+1)
		}
	}

    // fmt.Println(m)
	for _, val = range m {
		temp := (val-prev)*(n-val+1)
		ans = ans + temp
		prev = val
	}
	
	return ans
}
Friend or Girlfriend CodeChef Solution Review:

In our experience, we suggest you solve this Friend or Girlfriend CodeChef Solution and gain some new skills from Professionals completely free and we assure you will be worth it.

If you are stuck anywhere between any coding problem, just visit Queslers to get the Friend or Girlfriend CodeChef Solution.

Find on CodeChef

Conclusion:

I hope this Friend or Girlfriend CodeChef Solution would be useful for you to learn something new from this problem. If it helped you then don’t forget to bookmark our site for more Coding Solutions.

This Problem is intended for audiences of all experiences who are interested in learning about Programming Language in a business context; there are no prerequisites.

Keep Learning!

More Coding Solutions >>

Cognitive Class Answer

CodeChef Solution

Microsoft Learn

Leave a Reply

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