Problem Statement:
A company is transmitting its data to a new server. There are N files. Each file contains numeric data. Before being transmitted the data encrypted for security. The company wishes to encrypt the data will be converted into its binary form from the decimal. Bits in binary form will be inverted and then the inverted new binary formed will be converted back into a decimal number.
Write an algorithm to encrypt the data as per the given requirements.
The first line of input consists of an integer - numData, representing the number of data entries (N).Output Format:
The last line consists of N space-separated integers representing the data that is to be encrypted.
Print N space-separated integers representing the encrypted data of each file after following the given steps.Sample Input:
4
65 29 56 34
Sample Output:
62 2 7 29
Solution in | JAVA | PYTHON |
---|
import java.util.Scanner;
public class EncryptData{
public static void main(String args[]){
Scanner sc = new Scanner(System.in);
int n = sc.nextInt();
int data[] = new int[n];
for(int i=0;i<n;i++){
data[i] = sc.nextInt();
}
int encryptedData[] = new int[n];
for(int i=0;i<n;i++){
String binary = Integer.toBinaryString(data[i]);
String binaryInvert = "";
for(int j=0;j<binary.length();j++){
if(binary.charAt(j)=='0'){
binaryInvert += '1';
}else{
binaryInvert += '0';
}
}
encryptedData[i] = Integer.parseInt(binaryInvert,2);
}
for(int i=0;i<n;i++){
System.out.print(encryptedData[i]);
if(i<n-1){
System.out.print(" ");
}
}
}
}
public class EncryptData{
public static void main(String args[]){
Scanner sc = new Scanner(System.in);
int n = sc.nextInt();
int data[] = new int[n];
for(int i=0;i<n;i++){
data[i] = sc.nextInt();
}
int encryptedData[] = new int[n];
for(int i=0;i<n;i++){
String binary = Integer.toBinaryString(data[i]);
String binaryInvert = "";
for(int j=0;j<binary.length();j++){
if(binary.charAt(j)=='0'){
binaryInvert += '1';
}else{
binaryInvert += '0';
}
}
encryptedData[i] = Integer.parseInt(binaryInvert,2);
}
for(int i=0;i<n;i++){
System.out.print(encryptedData[i]);
if(i<n-1){
System.out.print(" ");
}
}
}
}
Input
4
65 29 56 34
Output
62 2 7 29
4
65 29 56 34
Output
62 2 7 29