Print all 3 digit repeating numbers in a very large number
Last Updated :
22 Sep, 2022
Given a very large number, print all the 3 digit repeating numbers with their frequency. If a 3 digit number appears more than once, print the number and its frequency.
Example:
Input: 123412345123456
Output: 123 - 3 times
234 - 3 times
345 - 2 times
Input: 43243243
Output: 432 - 2 times
324 - 2 times
243 - 2 times
Approach: Since the number is very large, it is stored in a string. Initially, the first three-digit number will be the first three characters from the left. Iterate in the string from the 3rd index from the left in the string and do %100 to remove the first character and append the ith index number at the end to get the new number. Increase the frequency of the number in the hash map. In the end, when all the 3-digit numbers are generated, print all the numbers which have a frequency of more than 1.
Below is the implementation of the above idea:
C++
#include <bits/stdc++.h>
using namespace std;
void printNum(string s)
{
int i = 0, j = 0, val = 0;
map < int , int > mp;
val = (s[0] - '0' ) * 100
+ (s[1] - '0' ) * 10
+ (s[2] - '0' );
mp[val] = 1;
for (i = 3; i < s.length(); i++) {
val = (val % 100) * 10 + s[i] - '0' ;
if (mp.find(val) != mp.end()) {
mp[val] = mp[val] + 1;
}
else {
mp[val] = 1;
}
}
for ( auto m : mp) {
int key = m.first;
int value = m.second;
if (value > 1)
cout << key << " - " << value << " times" << endl;
}
}
int main()
{
string input = "123412345123456" ;
printNum(input);
}
|
Java
import java.util.*;
import java.lang.*;
public class GFG {
static void printNum(String s)
{
int i = 0 , j = 0 , val = 0 ;
LinkedHashMap<Integer, Integer> hm
= new LinkedHashMap<>();
val = (s.charAt( 0 ) - '0' ) * 100
+ (s.charAt( 1 ) - '0' ) * 10
+ (s.charAt( 2 ) - '0' );
hm.put(val, 1 );
for (i = 3 ; i < s.length(); i++) {
val = (val % 100 ) * 10 + s.charAt(i) - '0' ;
if (hm.containsKey(val)) {
hm.put(val, hm.get(val) + 1 );
}
else {
hm.put(val, 1 );
}
}
for (Map.Entry<Integer, Integer> en : hm.entrySet()) {
int key = en.getKey();
int value = en.getValue();
if (value > 1 )
System.out.println(key + " - " + value + " times" );
}
}
public static void main(String args[])
{
String input = "123412345123456" ;
printNum(input);
}
}
|
Python3
def printNum(s):
i, j, val = 0 , 0 , 0
mp = {}
val = (( ord (s[ 0 ]) - ord ( '0' )) * 100 +
( ord (s[ 1 ]) - ord ( '0' )) * 10 +
( ord (s[ 2 ]) - ord ( '0' )))
mp[val] = 1
for i in range ( 3 , len (s)):
val = (val % 100 ) * 10 + ord (s[i]) - ord ( '0' )
if (val in mp):
mp[val] = mp[val] + 1
else :
mp[val] = 1
for m in mp:
key = m
value = mp[m]
if (value > 1 ):
print (key, " - " , value, " times" )
if __name__ = = "__main__" :
input = "123412345123456"
printNum( input )
|
C#
using System;
using System.Collections.Generic;
class GFG
{
static void printNum(String s)
{
int i = 0, val = 0;
Dictionary< int ,
int > hm = new Dictionary< int ,
int >();
val = (s[0] - '0' ) * 100 +
(s[1] - '0' ) * 10 +
(s[2] - '0' );
hm.Add(val, 1);
for (i = 3; i < s.Length; i++)
{
val = (val % 100) * 10 + s[i] - '0' ;
if (hm.ContainsKey(val))
{
hm[val] = hm[val] + 1;
}
else
{
hm.Add(val, 1);
}
}
foreach (KeyValuePair< int , int > en in hm)
{
int key = en.Key;
int value = en.Value;
if (value > 1)
Console.WriteLine(key + " - " +
value + " times" );
}
}
public static void Main(String []args)
{
String input = "123412345123456" ;
printNum(input);
}
}
|
Javascript
<script>
function printNum(s)
{
let i = 0, j = 0, val = 0;
let hm = new Map();
val = (s[0].charCodeAt(0) - '0' .charCodeAt(0)) * 100
+ (s[1].charCodeAt(0) - '0' .charCodeAt(0)) * 10
+ (s[2].charCodeAt(0) - '0' .charCodeAt(0));
hm.set(val, 1);
for (i = 3; i < s.length; i++) {
val = (val % 100) * 10 + s[i].charCodeAt(0) - '0' .charCodeAt(0);
if (hm.has(val)) {
hm.set(val, hm.get(val) + 1);
}
else {
hm.set(val, 1);
}
}
for (let [Key, Value] of hm.entries()) {
let key = Key;
let value = Value;
if (value > 1)
document.write(key + " - " + value + " times<br>" );
}
}
let input = "123412345123456" ;
printNum(input);
</script>
|
Output
123 - 3 times
234 - 3 times
345 - 2 times
Share your thoughts in the comments
Please Login to comment...