Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 907749
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 15, 20262026-05-15T16:36:08+00:00 2026-05-15T16:36:08+00:00

Given two numbers a, b such that 1 <= a , b <= 10000000000

  • 0

Given two numbers a, b such that 1 <= a , b <= 10000000000 (10^10). My problem is to check whether the digits in them are permutation of each other or not. What is the fastest way of doing it? I was thinks of using hashing but unable to find any suitable hash function. Any suggestions?

For e.g –
123 is a valid permutation of 312

Also I don’t want to sort the digits in the numbers.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-15T16:36:08+00:00Added an answer on May 15, 2026 at 4:36 pm

    If you mean the characters of the numbers (such as 1927 and 9721), there are (at least) a couple of approaches.

    If you were allowed to sort, one approach is to simply sprintf them to two buffers, sort the characters in the buffers, then see if the strings are equal.

    However, given your desire to not sort the digits, another alternative is to set up a ten-element array, with all elements initially set to zero, then process each digit in the first number, incrementing the relevant element.

    Then do the same with the second number but decrementing.

    If, at the end, it’s still all zeros, the numbers were a permutation of each other.

    This is efficient in that it’s an O(n) algorithm where n is the number of digits in the two numbers. The pseudo-code for such a beast would be something like:

    def arePermutations (num1, num2):
        create array count, ten elements, all zero.
        for each digit in num1:
            increment count[digit]
        for each digit in num2:
            decrement count[digit]
        for each item in count:
            if item is non-zero:
                return false
        return true
    

    In C, the following complete program illustrates how this can be done:

    #include <stdio.h>
    #include <stdlib.h>
    
    #define FALSE (1==0)
    #define TRUE  (1==1)
    
    int hasSameDigits (long num1, long num2) {
        int digits[10];
        int i;
    
        for (i = 0; i < 10; i++)      // Init all counts to zero.
            digits[i] = 0;
    
        while (num1 != 0) {           // Process all digits.
            digits[num1%10]++;        // Increment for least significant digit.
            num1 /= 10;               // Get next digit in sequence.
        }
    
        while (num2 != 0) {           // Same for num2 except decrement.
            digits[num2%10]--;
            num2 /= 10;
        }
    
        for (i = 0; i < 10; i++)
            if (digits[i] != 0)       // Any count different, not a permutation.
                return FALSE;
    
        return TRUE;                  // All count identical, was a permutation.
    }
    

     

    int main (int c, char *v[]) {
        long v1, v2;
    
        if (c != 3) {
            printf ("Usage: %s <number1> <number2>\n", v[0]);
            return 1;
        }
    
        v1 = atol (v[1]);
        v2 = atol (v[2]);
        if (hasSameDigits (v1, v2)) {
            printf ("%d and %d are permutations\n", v1, v2);
        } else {
            printf ("%d and %d are not permutations\n", v1, v2);
        }
    
        return 0;
    }
    

    Simply pass it two (positive) numbers and, assuming they fit in a long, it’ll tell you whether they have the same digit counts.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Given a set of numbers, divide the numbers into two subsets such that difference
I encountered this problem while preparing for my exams. Given two arrays of numbers
Given two strings of equal length such that s1 = ACCT s2 = ATCT
(Python) Given two numbers A and B. I need to find all nested groups
Given any two sequences/vectors of M real numbers, I can easily compute their closeness
Given 3 numbers, I need to find which number lies between the two others.
Two sets of 'n' numbers are given A and B. Chose one element from
Given two tuples of the same arity, how can I lexicographically compare them? It
Extension of Euclid's Algorithm We already know that, for any two whole numbers a
Is there an algorithm that given two strings yields the degree of equality between

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.