Find Two Largest Adjacent Digits

In the fascinating world of numbers, various patterns emerge when we closely observe them. An interesting pattern is to identify two adjacent digits that are the largest in a given number. Given an integer input, your mission is to find and display the two largest adjacent digits in that number.

Boundary Condition(s):
10 <= Number <= 10^9

Input Format:
The input is a single integer.

Output Format:
The output should display the two largest adjacent digits from the number.

Example Input/Output 1:

Input:
123459876

Output:
98

Explanation:
The largest two adjacent digits in the number 123459876 are 9 and 8. Hence, the output is 98.

Example Input/Output 2:

Input:
57813579

Output:
79

Explanation:
The largest two adjacent digits in the number 57813579 are 7 and 9. Hence, the output is 79.

number = input()
largest_pair = max([number[i:i+2] for i in range(len(number)-1)])
print(largest_pair)
#include<stdio.h>
#include<string.h>

int main() {
    char number[11];
    scanf("%s", number);
    int max_pair = 0;
    for(int i=0; i<strlen(number)-1; i++) {
        int pair = (number[i]-'0')*10 + (number[i+1]-'0');
        if(pair > max_pair)
            max_pair = pair;
    }
    printf("%d", max_pair);
    return 0;
}
#include <iostream>
#include <string>
using namespace std;

int main() {
    string number;
    cin >> number;
    int max_pair = 0;
    for(int i=0; i<number.length()-1; i++) {
        int pair = (number[i]-'0')*10 + (number[i+1]-'0');
        if(pair > max_pair)
            max_pair = pair;
    }
    cout << max_pair;
    return 0;
}
import java.util.Scanner;

public class LargestAdjacentDigits {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        String number = sc.next();
        int max_pair = 0;
        for(int i=0; i<number.length()-1; i++) {
            int pair = (number.charAt(i)-'0')*10 + (number.charAt(i+1)-'0');
            if(pair > max_pair)
                max_pair = pair;
        }
        System.out.println(max_pair);
    }
}
Previous Article

Find the Digit Repeated the Most Consecutively

Next Article

Function consecutiveChar - CTS PATTERN

Write a Comment

Leave a Comment

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