Common Digit Transactions

Moderate
0/80
0 upvote
Asked in company
Goldman Sachs

Problem statement

You are given a list of 'N' non-negative transaction IDs. Your task is to perform two operations: a verification check and an audit sum.

  • Verification: Check if for every pair of adjacent transaction IDs in the list, they share at least one common digit.
  • Audit: Calculate the sum of all transaction IDs in the list.


    The verification is successful only if the common digit condition is true for all adjacent pairs.


  • Detailed explanation ( Input/output format, Notes, Images )
    Input Format:
    The first line of input contains an integer 'N', the number of transaction IDs.
    
    The second line contains 'N' space-separated long integers, representing the transaction IDs.
    


    Output Format:
    The first line of output must be either true or false, indicating the result of the verification check.
    
    The second line of output must be a single integer, the total sum of all transaction IDs.
    


    Note:
    If N <= 1, there are no adjacent pairs to check, so the verification is considered true.
    
    Transaction IDs can be large, so use a data type that can handle 64-bit integers (e.g., long in Java, long long in C++).
    
    Sample Input 1:
    3
    12 23 34
    


    Sample Output 1:
    true
    69
    


    Explanation for Sample 1:
    - Pair 1: `12` and `23`. They share the digit '2'. 
    - Pair 2: `23` and `34`. They share the digit '3'. 
    Since all adjacent pairs have a common digit, the first output is `true`.
    The sum is `12 + 23 + 34 = 69`.
    


    Sample Input 2:
    1
    500
    


    Sample Output 2:
    true
    500
    


    Explanation for Sample 2:
    There is only one transaction ID, so there are no adjacent pairs to check. The condition is vacuously true.
    The sum is 500.
    


    Expected Time Complexity:
    The expected time complexity is O(N * log(max_id)), where log(max_id) is the number of digits in the largest transaction ID.
    


    Constraints:
    1 <= N <= 10^5
    1 <= transaction ID <= 10^10
    
    Time limit: 1 sec
    
    Approaches (1)
    Common Digit Transactions
    Time Complexity
    Space Complexity
    Code Solution
    (100% EXP penalty)
    Common Digit Transactions
    Full screen
    Console