Digit Appearing Most After First Occurrence

The program must accept an integer N as the input. The task is to find the digit that appears the most number of times after its first occurrence. If there are multiple such digits, the program should print the largest among them.

Boundary Condition(s):
1 <= N <= 10^18

Input Format:
The first line contains the integer N.

Output Format:
A single line contains the digit that appears most after its first occurrence.

Example Input/Output 1:
Input:
1123451456

Output:
1

Explanation:
After the first occurrence of 1, it appears 2 more times. No other digit appears more than 1 after its first occurrence. Hence the output is 1.

Example Input/Output 2:
Input:
9292992385

Output:
9

Explanation:
After the first occurrence of 9, it appears 2 more times. Similarly, 2 also appears 2 more times after its first occurrence. Since 9 is larger than 2, the output is 9.

N = input().strip()
count_after_first = [0] * 10
first_occurred = [False] * 10

for digit in N:
    d = int(digit)
    if first_occurred[d]:
        count_after_first[d] += 1
    else:
        first_occurred[d] = True

max_count = max(count_after_first)
if max_count == 0:
    print(max([int(d) for d in N]))
else:
    print(count_after_first.index(max_count))
#include<stdio.h>
#include<string.h>

int main() {
    char N[20];
    scanf("%s", N);
    int len = strlen(N);
    int first_occurred[10] = {0};
    int count_after_first[10] = {0};

    for(int i = 0; i < len; i++) {
        int d = N[i] - '0';
        if(first_occurred[d]) {
            count_after_first[d]++;
        } else {
            first_occurred[d] = 1;
        }
    }

    int max_digit = 0;
    for(int i = 1; i < 10; i++) {
        if(count_after_first[i] >= count_after_first[max_digit]) {
            max_digit = i;
        }
    }
    printf("%d", max_digit);
    return 0;
}
#include <iostream>
#include <string>
using namespace std;

int main() {
    string N;
    cin >> N;
    int first_occurred[10] = {0};
    int count_after_first[10] = {0};

    for(char digit : N) {
        int d = digit - '0';
        if(first_occurred[d]) {
            count_after_first[d]++;
        } else {
            first_occurred[d] = 1;
        }
    }

    int max_digit = 0;
    for(int i = 1; i < 10; i++) {
        if(count_after_first[i] >= count_after_first[max_digit]) {
            max_digit = i;
        }
    }
    cout << max_digit;
    return 0;
}
import java.util.Scanner;

public class MostAfterFirst {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        String N = sc.next();
        boolean[] firstOccurred = new boolean[10];
        int[] countAfterFirst = new int[10];

        for(char ch : N.toCharArray()) {
            int d = ch - '0';
            if(firstOccurred[d]) {
                countAfterFirst[d]++;
            } else {
                firstOccurred[d] = true;
            }
        }

        int maxDigit = 0;
        for(int i = 1; i < 10; i++) {
            if(countAfterFirst[i] >= countAfterFirst[maxDigit]) {
                maxDigit = i;
            }
        }
        System.out.println(maxDigit);
    }
}
Previous Article

Determine if the Number is a Factorial Value

Next Article

Double Every Prime Digit and Append to the Number

Write a Comment

Leave a Comment

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