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
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] + " ");
}
}
7 Steps to Solve Algorithm Problems
3 Algorithm Strategies
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.
//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