Thursday, 22 April 2021

Left Rotation Array

   Left Rotation:

left rotation operation on an array shifts each of the array's elements  unit to the left. For example, if  left rotations are performed on array , then the array would become . Note that the lowest index item moves to the highest index in a rotation. This is called a circular array.

Given an array  of  integers and a number, , perform  left rotations on the array. Return the updated array to be printed as a single line of space-separated integers.

Function Description

Complete the function rotLeft in the editor below.

rotLeft has the following parameter(s):

  • int a[n]: the array to rotate
  • int d: the number of rotations

Returns

  • int a'[n]: the rotated array

Input Format

The first line contains two space-separated integers  and , the size of  and the number of left rotations.
The second line contains  space-separated integers, each an .

Constraints

Sample Input

5 4
1 2 3 4 5

Sample Output

5 1 2 3 4

Explanation

When we perform  left rotations, the array undergoes the following sequence of changes:

function rotLeft(a, d) {
    for(let i=0;i<d;i++){
        let lastElement=a[a.length-1];
        for(let j=0;j<a.length-1;j++){
            let updatedIndex=(j+a.length-1)%(a.length);
            a[updatedIndex]=a[j];
        }
         a[a.length-2]=lastElement;
    }
    return a;
}

   //OR

function rotLeft(a, d) {
    if(d==a.length)
        return a;
    else if(d>a.length)
        d=d%a.length;

      let newArr= a.splice(d,a.length)
     newArr= newArr.concat(a);
    return newArr;
}

 

Wednesday, 21 April 2021

Interview Programs


        //input user_name_Km outout userNameKm
    function getVariableName(inp) {
        let output = '';
        if (inp.indexOf('_') != -1) {
            var splitArr = inp.split("_");
            let i = 1;
            if (splitArr[0] === '') {
                i = 2;
                output += splitArr[1].toLowerCase();
            }
            else
                output += splitArr[0].toLowerCase();
            for (let j = ij < splitArr.lengthj++) {
                console.log(splitArr[j], splitArr[j].slice(1splitArr[j].length));
                output += splitArr[j][0].toUpperCase() + splitArr[j].slice(1splitArr[j].length);
            }
            return console.log(inpoutput);
        }
        for (let i = 0i < inp.lengthi++) {
            console.log(inp[i].charCodeAt(), inp[i]);
            if (inp[i].charCodeAt() <= 90)
                output += '_' + inp[i].toUpperCase();
            else output += inp[i];
        }
        console.log(inpoutput);

    }
    getVariableName("user_name_Km");
    getVariableName("_Nname_Km");
    getVariableName("Nname_jm_U");
    getVariableName("useUameKm");
    getVariableName("userName");
    getVariableName("YnameJmU");

    //Binary to Decimal
    var binary = "1101000";
    binaryToDecimal(binary)
    var digit = parseInt(binary2);
    console.log(digit);
    function binaryToDecimal(string) {
        let decimal = 0;
        let bits = 1;
        for(let i = string.length-1i >=0i--) {
            let currNum = (string[i]);
            console.log(currNum,"currNum")
            if(currNum === "1") {
                decimal += bits;
            }
            
            bits *= 2;
            console.log(bits,"bits",decimal)
        }
        console.log(decimal);
    }