[Solved] A company is transmitting data to another server Security key

A company needs to transmit data to another server, and to secure this data during transmission, they opt to use a security key. The security key is identified as the count of the unique repeating digits in the data. For instance, the data “112233” has three repeating digits (1, 2, and 3), so the security key would be 3.

Solution Steps

We will walk through the algorithmic solution to this problem step by step. The solution is implemented using C, C++, Python, and Java programming languages.

  1. Input Data: Read an integer data representing the data to be transmitted.
  2. Initialize Array: Create an integer array a of size 10 (for digits 0 to 9) and initialize all its elements to 0. This array will be used to count the occurrence of each digit.
  3. Count Repeating Digits: Use a loop to iterate through each digit of the input data. Inside the loop, perform the following:
    • Calculate the remainder r when data is divided by 10 (this gives the current digit).
    • Increment the count of the digit in the array a at the index r.
    • Update data by dividing it by 10, effectively removing the last digit.
  4. Count Unique Repeating Digits: Iterate through the array a (for digits 0 to 9). For each element in the array, if its value is greater than 1, increment the count variable. This means the digit has repeated.
  5. Output Security Key: Print the value of the count variable. This value represents the security key for the given data.

Test Cases
Test Case 1
Input:
12345
Expected Output:
0

Test Case 2
Input:
112233
Expected Output:
3

Test Case 3
Input:
111222333444
Expected Output:
4

Test Case 4
Input:
9876543210
Expected Output:
0

Test Case 5
Input:
112233445566
Expected Output:
6

Test Case 6
Input:
77777777
Expected Output:
1

Solution

#include <stdio.h>
#include <stdlib.h>

int main() {
    int n;
    scanf("%d", &n);
    int a[10] = {0};
    int count = 0;

    while (n > 0) {
        int r = n % 10;
        a[r]++;
        n = n / 10;
    }

    for (int i = 0; i < 10; i++) {
        if (a[i] > 1) {
            count++;
        }
    }

    printf("%d", count);
    return 0;
}
#include <iostream>
using namespace std;

int main() {
    int n;
    cin >> n;
    int a[10] = {0};
    int count = 0;

    while (n > 0) {
        int r = n % 10;
        a[r]++;
        n = n / 10;
    }

    for (int i = 0; i < 10; i++) {
        if (a[i] > 1) {
            count++;
        }
    }

    cout << count;
    return 0;
}
n = int(input())
a = [0] * 10
count = 0

while n > 0:
    r = n % 10
    a[r] += 1
    n = n // 10

for i in a:
    if i > 1:
        count += 1

print(count)
import java.util.Scanner;

public class SecurityKey {
    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        int n = scanner.nextInt();
        int[] a = new int[10];
        int count = 0;

        while (n > 0) {
            int r = n % 10;
            a[r]++;
            n = n / 10;
        }

        for (int i = 0; i < 10; i++) {
            if (a[i] > 1) {
                count++;
            }
        }

        System.out.println(count);
    }
}

Happy Learning – If you require any further information, feel free to contact me.

Share your love
Saurav Hathi

Saurav Hathi

I'm currently studying Bachelor of Computer Science at Lovely Professional University in Punjab.

📌 Nodejs and Android 😎
📌 Java

Articles: 444

Leave a Reply

Your email address will not be published. Required fields are marked *