Find Largest Difference Between Two Adjacent Digits

In digital mathematics and number analytics, a frequently encountered problem is the need to understand variations within a given number. This is especially crucial in applications like data prediction, anomaly detection in digit streams, and numerical cryptography. Highlighting the variance between digits can provide deeper insights into the number’s characteristics and behavior, particularly when looking at a sequence or a stream of numbers.

Your challenge today, rooted in real-world numerical analysis, is to develop an algorithm that pinpoints the largest difference between any two adjacent digits of a number. Such a task is not merely theoretical but finds applications in sectors like banking (fraud detection), telecom (signal fluctuation analysis), and even in stock market predictions.

For this computational endeavour, you are to accept a number N and then discern and output the most significant difference between its adjacent digits. This number crunching is invaluable for financial analysts, data scientists, and encryption specialists who need a quick reference on the number’s volatility.

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

Input Format:
A single line containing the integer N, the subject of your analysis.

Output Format:
A single line showing the largest difference between two adjacent digits in N.

Example Input/Output 1:
Input:
23876

Output:
5

Explanation:
Here, the most significant difference between adjacent digits in the number 23876 is between 8 and 3, which is 5.

n = input().strip()
max_diff = 0
for i in range(1, len(n)):
    diff = abs(int(n[i]) - int(n[i-1]))
    max_diff = max(max_diff, diff)
print(max_diff)
#include<stdio.h>
#include<stdlib.h>

int main() {
    char n[10];
    scanf("%s", n);
    int max_diff = 0;
    for(int i = 1; n[i] != ''; i++) {
        int diff = abs(n[i] - n[i-1]);
        if(diff > max_diff) max_diff = diff;
    }
    printf("%d", max_diff);
    return 0;
}
#include <iostream>
#include <cmath>
using namespace std;

int main() {
    string n;
    cin >> n;
    int max_diff = 0;
    for(int i = 1; i < n.size(); i++) {
        int diff = abs(n[i] - n[i-1]);
        if(diff > max_diff) max_diff = diff;
    }
    cout << max_diff;
    return 0;
}
import java.util.Scanner;

public class LargestDifference {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        String n = sc.next();
        int max_diff = 0;
        for(int i = 1; i < n.length(); i++) {
            int diff = Math.abs(n.charAt(i) - n.charAt(i-1));
            if(diff > max_diff) max_diff = diff;
        }
        System.out.println(max_diff);
    }
}
Previous Article

Function productMatrix – CTS PATTERN

Next Article

Function findMinElement - CTS PATTERN

Write a Comment

Leave a Comment

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