Posts

Given a number N followed by N numbers.Find the smallest number and largest number and print both the indices(1 based indexing).

Given a number N followed by N numbers.Find the smallest number and largest number and print both the indices(1 based indexing). Input Size : N <= 100000 Sample Testcase : INPUT 5 1 2 3 4 5 OUTPUT 1 5 const readline = require('readline'); const inp = readline.createInterface({   input: process.stdin }); const userInput = []; inp.on("line", (data) => { userInput.push(data); }); inp.on("close", () => {var a = parseInt(userInput[0]); var outarr =[]  var b = (userInput[1]);  var arr = b.split(" ").map((val)=>Number(val))  var sarr = []  for(i=0;i<arr.length;i++)  {sarr[i]=arr[i]}  sarr.sort(function(a,b){return a-b})  for(i=0;i<arr.length;i++)  {if(sarr[0]==arr[i])  {outarr[0]=1+i}  if(sarr[(arr.length)-1]==arr[i])  {outarr[1]=1+i}  }  var out = outarr.join(" ")  console.log(out)    });

Given a string S consisting of 2 words reverse the order of two words .

Given a string S consisting of 2 words reverse the order of two words . Input Size : |S| <= 10000000 Sample Testcase : INPUT hello world OUTPUT world hello 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 a = data.reverse();  var c= a.join(" "); console.log(c) });

Given 3 numbers N , L and R. Print 'yes' if N is between L and R else print 'no'.

Given 3 numbers N , L and R. Print 'yes' if N is between L and R else print 'no'. Sample Testcase : INPUT 3 2 6 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 a = parseInt(userInput[0]);  var b = (userInput[1]);  var c= b.split(" ")  if(+c[0]<a &&  a+c[1])  console.log("yes")  else  console.log("no")  });

Given 3 numbers A,B,C print 'yes' if they can form the sides of a right angled triangle,otherwise 'no'.

Given 3 numbers A,B,C print 'yes' if they can form the sides of a right angled triangle,otherwise 'no'. Input Size : A,B,C <= 100000 Sample Testcase : INPUT 3 4 5 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 data = userInput[0].split(" ");  var a = parseInt(data[0]);  var b= parseInt(data[1]);  var c = parseInt(data[2]);  var d = (a*a)+(b*b)  var e = c*c;  if(d==e)  console.log('yes')    else  console.log('no') });

Given base(B) and height(H) of a triangle find its area.

Given base(B) and height(H) of a triangle find its area. Input Size : N <= 1000000 Sample Testcase : INPUT 2 4 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 data = userInput[0].split(" ");  var a = parseInt(data[0]);  var b= parseInt(data[1]); console.log(0.5*a*b) });

Write a program to print the sum of the first K natural numbers.

Write a program to print the sum of the first K natural numbers. Input Size : n <= 100000 Sample Testcase : INPUT 3 OUTPUT 6 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 a = parseInt(data[0]);  var total=0;  for (i=0;i<=a;i++)  {total=total+i} console.log(total) });