Tuesday, 25 July 2017

CTCI Challange, DAY 1

Gayle laakmann mcdowell has provided few guidence videos on how to solve the algorithms and about the Algorithm Strategies.

7 Steps to Solve Algorithm Problems

3 Algorithm Strategies

Problem Statement:- Arrays: Left Rotation

I solved my first day problem on Arrays.

Easy but took 1.5 hours to complete with 10 iterations.2 test cases failed due to time error.Good need to catch up a lot tmrw.

public class Arrays_Left_Rotation {
//TODO remove the temp array
    public static void main(String[] args) {
        Scanner scan = new Scanner(System.in);
        int n = scan.nextInt();
        int d = scan.nextInt();
        int[] array = new int[n];
        for (int i = 0; i < n; i++) {
            array[i] = scan.nextInt();
        }
        int k = d % n;
        int temp[] = new int[n];
        for (int i = 0; i < n; i++) {
            int count = 0, j = i;
            while (count != k) {
                if (j == 0)
                    j = n - 1;
                else
                    j--;
                count++;
            }
            temp[j] = array[i];
        }
        for (int i = 0; i < n; i++) {
            System.out.print(temp[i] + " ");
        }
    }


No comments:

Post a Comment

Installing Docker and Minikube

  install docker-   sudo apt install docker.io   set user to docker group:- sudo gpasswd -a   {user} docker   imp commands:- ...