Maximum Streaks HackerRank Java Solution

Input Format In the first line, there is a single integer n denoting the number of tosses. Then, n lines follow. The ith of them contains a string t
Admin

Maximum Streaks HackerRank java solution
A coin was tossed numerous times. You need to find the longest streak of tosses resulting Heads and Tails the longest streak of tosses resulting in Tails.

Formally, given the results of n tosses of a coin, find the maximum number of consecutive Heads and the maximum number of consecutive Tails.

Consider the following example: a coin was tossed n=7 times and the results were Heads

Tails, Tails, Tails, Heads, Heads, Tails. Therefore, the longest Heads streak was 3 and the longest Tails streak was 2.

Complete the function getMaxStreaks which takes an array of strings toss and returns an array of two integers denoting the maximum streaks of Heads and Tails respectively.

Input Format

In the first line, there is a single integer n denoting the number of tosses.

Then, n lines follow. The ith of them contains a string tossi denoting the result of the ith toss of the coin.

Constraints

i ≤ n ≤ 50 

toss€ { Heads, Tails}

Output Format

In a single line, print two space-separated integers denoting the maximum streak of Heads and the maximum streak of Tails respectively.

Sample Input 0
7
Heads
Tails
Tails
Tails
Heads
Heads
Tails
Sample Output 0
2 3

Explanation 0

The longest streak of Heads is 2 and the longest streak of Tails is 3.

Sample Input 1
3
Tails
Tails
Tails
Sample Output 1
0 3

Explanation 1

The longest streak of Heads is 0 since there were no such tosses, and the longest streak of Tails is 3.

Sample Input 2
4
Heads
Heads
Heads
Heads
Sample Output 2
4 0

Explanation 2

The longest streak of Heads is 4, and the longest streak of Tails is 0 since there were no such tosses.

Solution
import java.io.*;
import java.util.*;
public class Solution {
    public static void main(String[] args) {
        Scanner sc=new Scanner(System.in);
        List<String> l = new ArrayList<>();
        int n=sc.nextInt();
        for(int i=0;i<n;i++)
        {
            String str=sc.next();
            l.add(str);
        }
        getMaxStreaks(l); 
    }
    public static List<Integer> getMaxStreaks(List<String> toss) {
        List<Integer> listR = new ArrayList<>();
        int headCountM = 0;
        int tailCountM = 0;
        int headCount = 0;
        int tailCount = 0;
        for (String t : toss) {
            if (t.equals("Heads")) {
                tailCount = 0;
                headCount++;
                if (headCountM < headCount) {
                    headCountM = headCount;
                }
            } else {
                headCount = 0;
                tailCount++;
                if (tailCountM < tailCount) {
                    tailCountM = tailCount;
                }
            }
        }
        listR.add(headCountM);
        listR.add(tailCountM);
        
        System.out.print(headCountM+" ");
        System.out.print(tailCountM+" ");
        return listR;
    }
}

Post a Comment

Cookie Consent
We serve cookies on this site to analyze traffic, remember your preferences, and optimize your experience.
Oops!
It seems there is something wrong with your internet connection. Please connect to the internet and start browsing again.
AdBlock Detected!
We have detected that you are using adblocking plugin in your browser.
The revenue we earn by the advertisements is used to manage this website, we request you to whitelist our website in your adblocking plugin.
Site is Blocked
Sorry! This site is not available in your country.