ASSESSMENT - 2
A number is said to be a twisted
prime if it is a prime number and the reverse of the number is also a prime
number.
Input:
The first line of input contains a number N.
Output:
Print the answer "Yes" or "No".
sample input :
97
sample output :
Yes
const readline = require('readline');
const inp = readline.createInterface({
input: process.stdin
});
const userInput = [];
inp.on("line", (data) => {
userInput.push(data);
});
inp.on("close", () => {
var num = Number(userInput[0]);
var str = String(num);
var arr = str.split('').map((val) => Number(val))
var rarr = arr.reverse();
var rstr = rarr.join("");
var rnum = Number(rstr)
function isprime(n) {
for (i = 2; i < n; i++) {
if (n % i == 0)
return 0;
}
return 1;
}
var x = isprime(num);
var y = isprime(rnum);
if (x == 1 && y == 1)
console.log("Yes")
else
console.log("No")
})
Given an array A of
size N, print the second largest element from an array.
Input:
The first line contains an number N denoting the size of the array. The
second line contains the N space-separated numbers of the array
Output:
For each test case, in a new line, print the second largest element.
sample input :
5
89 24 75 11 23
sample output :
75
sample input :
3
2 2 1
sample output :
1
const readline = require('readline');
const inp = readline.createInterface({
input: process.stdin
});
const userInput = [];
inp.on("line", (data) => {
userInput.push(data);
});
inp.on("close", () => {
var N = Number(userInput[0]);
var A = String(userInput[1]);
var arr = A.split(" ").map((val) => Number(val));
var uniquearr = new Set(arr);
var uarr = Array.from(uniquearr)
var farr = uarr.sort(function (a, b) {
return b - a
});
console.log(farr[1])
})
Write a program to find the sum
of bit differences in all pairs that can be formed from array elements
n. Bit difference of a pair (x, y) is a count of different bits at the
same positions in binary representations of x and y. For example, bit difference
for 2 and 7 is 2. The binary representation of 2 is 010 and 7 is 111 ( first
and last bits differ in two numbers).
Explanation For Sample Test
Case:
1: 01
2: 10
Bit difference in pair (1, 2): 2
Bit difference in pair (2, 1): 2
Hence, total Bit difference = 2 + 2 = 4
Note: (a, b) and (b, a) are
considered two separate pairs.
sample input :
2
1 2
sample output :
4
const readline = require("readline");
const inp = readline.createInterface({
input: process.stdin
});
const userInput = [];
inp.on("line", (data) => {
userInput.push(data);
});
inp.on("close", () => {
var N = Number(userInput[0])
var arr = userInput[1].split(" ").map(val => +val)
var allpairs = [];
for (let i = 0; i < n; i++) {
for (let j = i + 1; j < n; j++) {
allpairs.push([arr[i], arr[j]])
allpairs.push([arr[j], arr[i]])
}
}
var bitDiffCount = (a, b) => {
const bitStr = ((a ^ b) >>> 0).toString(2);
return bitStr.split('1').length - 1;
};
var sum = 0;
for (i of allpairs) {
sum += bitDiffCount(i[0], i[1])
}
console.sum(bitdiff)
})
ALL TEST CASES PASSED FOR ALL QUESTIONS
Comments
Post a Comment