Related Articles

# Counting numbers whose difference from reverse is a product of k

• Difficulty Level : Easy
• Last Updated : 10 Mar, 2021

Given two number l and r. Count the total numbers between l and r which when subtracted from their respective reverse, the difference is a product of k.
Examples:

```Input : 20 23 6
Output : 2
20 and 22 are the two numbers.
|20-2| = 18 which is a product of 6
|22-22| = 0 which is a product of 6

Input : 35 45 5
Output : 2
38 and 44 are the two numbers.
|38-83| = 45 which is a product of 5
|44-44| = 0 which is a product of 5```

Approach: For each number between the given range check if the difference between the number and its reverse number is divisible by k, increment count if yes.

## C++

 `// C++ program to Count the numbers``// within a given range in which when``// you subtract a number from its``// reverse, the difference is a product``// of k``#include ``using` `namespace` `std;` `bool` `isRevDiffDivisible(``int` `x, ``int` `k)``{``    ``// function to check if the number``    ``// and its reverse have their``    ``// absolute difference divisible by k``    ``int` `n = x;``    ``int` `m = 0;``    ``int` `flag;``    ``while` `(x > 0)``    ``{  ``        ``// reverse the number``        ``m = m * 10 + x % 10;``        ``x /= 10;``    ``}``    ` `    ``return` `(``abs``(n - m) % k == 0);``}` `int` `countNumbers(``int` `l, ``int` `r, ``int` `k)``{``    ``int` `count = 0;``    ``for` `(``int` `i = l; i <= r; i++)   ``        ``if` `(isRevDiffDivisible(i, k))       ``            ``++count;``    ``return` `count;``}` `// Driver code``int` `main()``{``    ``int` `l = 20, r = 23, k = 6;``    ``cout << countNumbers(l, r, k) << endl;``    ``return` `0;``}`

## Java

 `// Java program to Count the``// numbers within a given range``// in which when you subtract``// a number from its reverse,``// the difference is a product of k``import` `java.io.*;``import` `java.math.*;` `class` `GFG {``    ` `    ``static` `boolean` `isRevDiffDivisible(``int` `x, ``int` `k)``    ``{``        ``// function to check if the number``        ``// and its reverse have their``        ``// absolute difference divisible by k``        ``int` `n = x;``        ``int` `m = ``0``;``        ``int` `flag;``        ``while` `(x > ``0``)``        ``{``            ``// reverse the number``            ``m = m * ``10` `+ x % ``10``;``            ``x /= ``10``;``        ``}``        ` `        ``return` `(Math.abs(n - m) % k == ``0``);``    ``}``    ` `    ``static` `int` `countNumbers(``int` `l, ``int` `r, ``int` `k)``    ``{``        ``int` `count = ``0``;``        ``for` `(``int` `i = l; i <= r; i++)``            ``if` `(isRevDiffDivisible(i, k))    ``                ``++count;``        ``return` `count;``    ``}``    ` `    ``// Driver code``    ``public` `static` `void` `main(String args[])``    ``{``        ``int` `l = ``35``, r = ``45``, k = ``5``;``        ``System.out.println(countNumbers(l, r, k));``    ``}``}` `// This code is contributed``// by Nikita Tiwari.`

## Python3

 `# Python 3 program to Count the numbers``# within a given range in which when you``# subtract a number from its reverse,``# the difference is a product of k` `def` `isRevDiffDivisible(x, k) :``    ``# function to check if the number``    ``# and its reverse have their``    ``# absolute difference divisible by k``    ``n ``=` `x; m ``=` `0``    ``while` `(x > ``0``) :` `        ``# Reverse the number``        ``m ``=` `m ``*` `10` `+` `x ``%` `10``        ``x ``=` `x ``/``/` `10``        ` `    ``return` `(``abs``(n ``-` `m) ``%` `k ``=``=` `0``)` `def` `countNumbers(l, r, k) :``    ``count ``=` `0``    ``for` `i ``in` `range``(l, r ``+` `1``) :` `        ``if` `(isRevDiffDivisible(i, k)) :``            ``count ``=` `count ``+` `1``    ` `    ``return` `count``    ` `    ` `# Driver code``l ``=` `20``; r ``=` `23``; k ``=` `6``print``(countNumbers(l, r, k))`  `# This code is contributed by Nikita Tiwari.`

## C#

 `// C# program to Count the``// numbers within a given range``// in which when you subtract``// a number from its reverse,``// the difference is a product of k``using` `System;` `class` `GFG {``    ` `    ``static` `bool` `isRevDiffDivisible(``int` `x, ``int` `k)``    ``{``        ``// function to check if the number``        ``// and its reverse have their``        ``// absolute difference divisible by k``        ``int` `n = x;``        ``int` `m = 0;``        ` `        ``// int flag;``        ``while` `(x > 0)``        ``{``            ``// reverse the number``            ``m = m * 10 + x % 10;``            ``x /= 10;``        ``}``        ` `        ``return` `(Math.Abs(n - m) % k == 0);``    ``}``    ` `    ``static` `int` `countNumbers(``int` `l, ``int` `r, ``int` `k)``    ``{``        ``int` `count = 0;``        ` `        ``for` `(``int` `i = l; i <= r; i++)``            ``if` `(isRevDiffDivisible(i, k))``                ``++count;``        ``return` `count;``    ``}``    ` `    ``// Driver code``    ``public` `static` `void` `Main()``    ``{``        ``int` `l = 35, r = 45, k = 5;``        ``Console.WriteLine(countNumbers(l, r, k));``    ``}``}` `// This code is contributed``// by vt_m.`

## PHP

 ` 0)``    ``{``        ``// reverse the number``        ``\$m` `= ``\$m` `* 10 + ``\$x` `% 10;``        ``\$x` `= (int)``\$x` `/ 10;``    ``}``    ` `    ``return` `(``abs``(``\$n` `- ``\$m``) %``                ``\$k` `== 0);``}` `function` `countNumbers(``\$l``, ``\$r``, ``\$k``)``{``    ``\$count` `= 0;``    ``for` `(``\$i` `= ``\$l``; ``\$i` `<= ``\$r``; ``\$i``++)``        ``if` `(isRevDiffDivisible(``\$i``, ``\$k``))``            ``++``\$count``;``    ``return` `\$count``;``}` `// Driver code``\$l` `= 20; ``\$r` `= 23; ``\$k` `= 6;``echo` `countNumbers(``\$l``, ``\$r``, ``\$k``);` `// This code is contributed by mits.``?>`

## Javascript

 ``

Output:

`2`

Attention reader! Don’t stop learning now. Get hold of all the important mathematical concepts for competitive programming with the Essential Maths for CP Course at a student-friendly price. To complete your preparation from learning a language to DS Algo and many more,  please refer Complete Interview Preparation Course.

My Personal Notes arrow_drop_up
{"ft_lang_label":"__label__en","ft_lang_prob":0.7113423,"math_prob":0.984566,"size":5455,"snap":"2021-31-2021-39","text_gpt3_token_len":1790,"char_repetition_ratio":0.15061456,"word_repetition_ratio":0.40136635,"special_character_ratio":0.36590284,"punctuation_ratio":0.15154265,"nsfw_num_words":0,"has_unicode_error":false,"math_prob_llama3":0.9998505,"pos_list":[0],"im_url_duplicate_count":[null],"WARC_HEADER":"{\"WARC-Type\":\"response\",\"WARC-Date\":\"2021-07-28T22:38:42Z\",\"WARC-Record-ID\":\"<urn:uuid:5cd2bdb6-ae81-4e7b-9640-e4c5cea079ae>\",\"Content-Length\":\"152434\",\"Content-Type\":\"application/http; msgtype=response\",\"WARC-Warcinfo-ID\":\"<urn:uuid:ddebc323-9d27-4b2d-a71b-25107fe8bf58>\",\"WARC-Concurrent-To\":\"<urn:uuid:b9da6aa2-9a22-4bab-9b1f-ea35082782d1>\",\"WARC-IP-Address\":\"23.205.105.172\",\"WARC-Target-URI\":\"https://www.geeksforgeeks.org/counting-numbers-whose-difference-from-reverse-is-a-product-of-k/\",\"WARC-Payload-Digest\":\"sha1:H6FBDFA2TXLOWFQGOKN3VR5T2CYIB3MA\",\"WARC-Block-Digest\":\"sha1:GW5233TIG4ZMRHTJVLXUY5QZWKYWDZ3T\",\"WARC-Identified-Payload-Type\":\"text/html\",\"warc_filename\":\"/cc_download/warc_2021/CC-MAIN-2021-31/CC-MAIN-2021-31_segments_1627046153803.69_warc_CC-MAIN-20210728220634-20210729010634-00618.warc.gz\"}"}