In the realm of numbers, patterns often hide in plain sight. One such pattern that often intrigues the curious minds is the recurrence of digits in consecutive positions. For example, in the number 1223334444, we observe the digit 4 recurring four times consecutively, which is the maximum consecutive occurrence in this number.
Your task is to examine a number and identify which digit is repeated the most consecutively and how many times it’s repeated. If multiple digits have the same maximum consecutive occurrence, report the highest digit.
Boundary Condition(s):
10 <= N <= 10^9
Input Format:
A single integer N, representing the number you need to analyze.
Output Format:
Two integers separated by a space. The first integer is the digit that’s repeated the most consecutively, and the second integer is the count of its consecutive occurrences.
Example Input/Output 1:
Input:
1223334555
Output:
5 3
Explanation:
Both digit 3 and digit 5 are repeated three times consecutively. Since 5 is the higher digit, the output is “5 3”.
Example Input/Output 2:
Input:
112233
Output:
3 2
Explanation:
All the digits 1, 2, and 3 are repeated twice consecutively. However, 3 being the highest, the output is “3 2”.
Example Input/Output 3:
Input:
555556666677777
Output:
7 5
Explanation:
The digit 7 is repeated consecutively five times, which is the highest consecutive occurrence among all digits.
n = input().strip()
current_digit = n[0]
current_count = 1
max_digit = current_digit
max_count = 1
for digit in n[1:]:
if digit == current_digit:
current_count += 1
else:
if current_count > max_count or (current_count == max_count and current_digit > max_digit):
max_digit = current_digit
max_count = current_count
current_digit = digit
current_count = 1
if current_count > max_count or (current_count == max_count and current_digit > max_digit):
max_digit = current_digit
max_count = current_count
print(max_digit, max_count)
#include<stdio.h>
#include<string.h>
int main() {
char n[10];
scanf("%s", n);
int len = strlen(n);
char current_digit = n[0];
int current_count = 1;
char max_digit = current_digit;
int max_count = 1;
for(int i = 1; i < len; i++) {
if(n[i] == current_digit) {
current_count++;
} else {
if(current_count > max_count || (current_count == max_count && current_digit > max_digit)) {
max_digit = current_digit;
max_count = current_count;
}
current_digit = n[i];
current_count = 1;
}
}
if(current_count > max_count || (current_count == max_count && current_digit > max_digit)) {
max_digit = current_digit;
max_count = current_count;
}
printf("%c %d", max_digit, max_count);
return 0;
}
#include <iostream>
#include <string>
using namespace std;
int main() {
string n;
cin >> n;
char current_digit = n[0];
int current_count = 1;
char max_digit = current_digit;
int max_count = 1;
for(size_t i = 1; i < n.size(); i++) {
if(n[i] == current_digit) {
current_count++;
} else {
if(current_count > max_count || (current_count == max_count && current_digit > max_digit)) {
max_digit = current_digit;
max_count = current_count;
}
current_digit = n[i];
current_count = 1;
}
}
if(current_count > max_count || (current_count == max_count && current_digit > max_digit)) {
max_digit = current_digit;
max_count = current_count;
}
cout << max_digit << " " << max_count;
return 0;
}
import java.util.Scanner;
public class ConsecutiveDigitCount {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
String n = sc.next();
char current_digit = n.charAt(0);
int current_count = 1;
char max_digit = current_digit;
int max_count = 1;
for(int i = 1; i < n.length(); i++) {
if(n.charAt(i) == current_digit) {
current_count++;
} else {
if(current_count > max_count || (current_count == max_count && current_digit > max_digit)) {
max_digit = current_digit;
max_count = current_count;
}
current_digit = n.charAt(i);
current_count = 1;
}
}
if(current_count > max_count || (current_count == max_count && current_digit > max_digit)) {
max_digit = current_digit;
max_count = current_count;
}
System.out.println(max_digit + " " + max_count);
}
}
Leave a Reply