Question:
The company digital secure data transfer solutions provides data encryption and data sharing services. Their process uses a key K for encryption when transmitting a number. To encrypt a number, each digit in the number is replaced by the Kth digit after it in the number. The series of digits is considered in a cyclic fashion for the last K digits.
write an algorithm to find the encrypted number.
Input:
The first line consists of the input as an integer data representing the number. The second line consists of an integer and key, representing the key (K).
Output:
print an integer representing the encrypted number.
Examples:
input:
25143
3
output:
43251
Explanation:
Replace 2 with 4, 5 with 3, 1 with 2, 4 with 5 and 3 with 1. so the output will be 43251
Algoritm to solve
- Take all inputs from user , integer N and Key value.
- First convert integer to arraylist
- Then convert arraylist to array.
- Then “Rotate array ” towards right side by “key-1” time to get the new array.
- Now convert array to string type and return the answer as 43251
JAVA Implementation
package arrayproblems; import java.util.ArrayList; class Problem21 { public static void main(String[] args) { int N=25143; int key=3; String res=""; ArrayList array1=new ArrayList(); while(N!=0) { array1.add(N%10); N=N/10; } int[] array2=new int[array1.size()]; int k=0; for(int i=array1.size()-1;i>=0;i--) { array2[k]=(int) array1.get(i); k=k+1; } for(int j=0;j<key-1;j++) { int temp=array2[array2.length-1]; for(int i=array2.length-1;i>=1;i--) { array2[i]=array2[i-1]; } array2[0]=temp; } for(int i:array2) { System.out.print(i+res); } } }
Watch step by step explanation for above code below
Similar off campus coding Questions
Xoriant off campus coding question and solution
Crack TCS Coding Round By solving These Questions