We use cookies to ensure you have the best browsing experience on our website. Please read our cookie policy for more information about how we use cookies.
  • HackerRank Home

    HackerRank

  • |
  • Prepare
  • Certify
  • Compete
  • Hiring developers?
  1. Prepare
  2. Algorithms
  3. Implementation
  4. Modified Kaprekar Numbers
  5. Discussions

Modified Kaprekar Numbers

Problem
Submissions
Leaderboard
Discussions
Editorial
Topics

    You are viewing a single comment's thread. Return to all comments →

  • therealsbkavin
    2 months ago+ 0 comments

    Java Solution

    public static void kaprekarNumbers(int p, int q) {
    // Write your code here
    int flag=0;
    for(long i=p;i<=q;i++){
        if(i==1){
            System.out.print(1+" ");
            flag=1;
        }
        long sq= i*i;
        String iVal=String.valueOf(i);
        long ilen=iVal.length();
        String sqVal=String.valueOf(sq);
        long sqlen=sqVal.length();
        if(sqlen==1){
            continue;
        }
        String rt=sqVal.substring(sqVal.length()-(int)ilen, sqVal.length());
        String lt=sqVal.substring(0, sqVal.length()-(int) ilen);
        long sm=Long.valueOf(rt)+Long.valueOf(lt);
        if(sm==i){
            System.out.print(i+" ");
            flag=1;
        }
    
    }
    if(flag==0){
        System.out.println("INVALID RANGE");
    }
    
    }
    
    0|
    Permalink
  • Blog
  • Scoring
  • Environment
  • FAQ
  • About Us
  • Support
  • Careers
  • Terms Of Service
  • Privacy Policy