Sort by

recency

|

3103 Discussions

|

  • + 0 comments

    in scala without actually rotating

    def circularArrayRotation(a: Array[Int], k: Int, queries: Array[Int]): Array[Int] = {
      val kMod = k % a.length
      queries.map(x => a(if ((x - kMod + a.length) > 0) (x - kMod + a.length) % a.length else 0))
    }
    
  • + 0 comments
    def circularArrayRotation(a:list, k:int, queries:list):
        ### Shift Circulate 
        result = []
        for x in range(k):
            last = a[-1]
            a.insert(0,last)
            a.pop(-1)
        
        ### add numbers to result based on queary
        for num in queries:
            result.append( a[num])
        return result
    
  • + 0 comments
    def circularArrayRotation(a, k, queries):
        for _ in range(k):
            k=a.pop()
            a.insert(0,k)
        return [a[i] for i in queries]
    
  • + 0 comments

    Here is problem solution in Python, Java, C++, C and Javascript - https://programmingoneonone.com/hackerrank-circular-array-rotation-problem-solution.html

  • + 1 comment

    TS solution

        let n = k % a.length;
         
        let temp1 = a.slice(a.length-(n), a.length);
        let temp2 = a.slice(0, a.length-(n));
            
        a = [...temp1, ...temp2]
        
        let newArr: number[] = [];
        queries.map((num)=>{
            newArr.push(a[num]);
        })
        return newArr;