Skip to content

Commit

Permalink
Swaps in arrays
Browse files Browse the repository at this point in the history
Given a string 'S' swap the even and odd characters starting from index 1(Assume the index starts from 0).
Input Size : |s| <= 10000000(complexity O(n))
Sample Testcase :
INPUT
codekata
OUTPUT
ocedakat
  • Loading branch information
Arunkumar1712 authored Dec 18, 2023
0 parents commit fb26757
Showing 1 changed file with 29 additions and 0 deletions.
29 changes: 29 additions & 0 deletions test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@

// Getting input via STDIN
const readline = require("readline");

const inp = readline.createInterface({
input: process.stdin
});

const userInput = [];

inp.on("line", (data) => {
userInput.push(data);
});

inp.on("close", () => {
let a=userInput[0].split("");
let temp=0;
for( var i=0;i<a.length;i++){
if(i%2 === 0){
if(i+1<a.length){
temp=a[i];
a[i]=a[i+1];
a[i+1]=temp;}
}
}

a=a.join(" ")
console.log(a);
});

0 comments on commit fb26757

Please sign in to comment.