Skip to main content

ASSESSMENT - 1

ASSESSMENT - 1


Given a string S of length N,reverse every word in place
Input Size:1<=N<=100000

sample input :

GOOD GOOD

sample output :

DOOG DOOG




const readline = require('readline');

const inp = readline.createInterface({

    input: process.stdin

});

const userInput = [];

inp.on("line", (data=> {

    userInput.push(data);

});

inp.on("close", () => {

    var str = String(userInput);

    function wordsReverser(str) {

        return str.split("").reverse().join("").split(" ").reverse().join(" ")

    }

    var ans = wordsReverser(str)

    console.log(ans)

})

//ANOTHER METHOD

Var str = “TRY IT”;

Var arr = str.split(““)

var s = [];
for (i = 0i < arr.lengthi++) {
    var c = arr[i].split('');
    var d = c.reverse();
    var e = d.join('');
    s.push(e)
}
console.log(s)



Given 4 numbers N,P,Q,R followed by N integers,find the maximum value of Pa[i]+Qa[j]+Ra[k](i<=j<=k).
Input Size:1<=N

-100000<=P,Q,R<=100000

example

INPUT

5 1 2 3

1 2 3 4 5

OUTPUT

30





const readline = require('readline');
const inp = readline.createInterface({
    input: process.stdin
});
const userInput = [];
inp.on("line", (data=> {
    userInput.push(data);
});
inp.on("close", () => {
    var data = userInput[0].split(' ');
    var rdata = data.map((val=> Number(val))

    var N = rdata[0];
    var P = rdata[1];
    var Q = rdata[2];
    var R = rdata[3];
    var a = userInput[1].split('');
    var b = a.map((val=> Number(val))

    var sorta = b.sort(function (ab) {
        return b - a
    })
    var len = sorta.length
    if (P > 0 && Q > 0 && R > 0)
        var high = sorta[0];
    else
        var high = sorta[(len - 1)]
    console.log(high)
    console.log(PQR)
    var ans = (P * high) + (Q * high) + (R * high)
    console.log(ans)
})





Given 2 numbers N and Q followed by N numbers and Q inputs of 2 Numbers U and V are given. U indicates the starting index and V indicates the ending index. So for each U,V find the sum of all values of the array from the index U to V(1 based indexing).


Input size: N<=100000

sample input :

5 3

1 1 1 1 1

1 3

2 4

3 4

sample output :

3

3

2





const readline = require('readline');
const inp = readline.createInterface({
    input: process.stdin
});
const userInput = [];
inp.on("line", (data=> {
    userInput.push(data);
});
inp.on("close", () => {
    var data = userInput[0].split(' ');
    var ndata = data.map((val=> Number(val))
    var N = ndata[0];
    var Q = ndata[1];
    var arr = userInput[1].split(' ');
    var sarr = arr.map((val=> Number(val));

    var sum;
    for (i = 2i < (Q + 2); i++)

    {
        var sum = 0;
        var uv = userInput[i];
        var uvarr = uv.split(" ").map((val=> Number(val));

        var U = uvarr[0];
        var V = uvarr[1];

        for (j = uvarr[0]; j <= uvarr[1]; j++)

            sum += sarr[(j - 1)]
        console.log(sum)
    }
})



2 QUESTIONS ARE COREECT

1 QUESSTION  - 1 TEST CASE FAILED

Forget to write else condition for negative number
















Comments

Popular posts from this blog

ASSESSMENT - 2

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 . ...