Java Program to Rotate the elements of an array to the left By N times | Interview Question Answers

preview_player
Показать описание
In this video, we demonstrated how to Rotate the elements of an array to the left By N times.

Input Array - {1 2 3 4 5}
N - 3
Output Array - {4 5 1 2 3}

#java
#javacoding
#javainterviewquestions
#javaprogramming
#coding
Рекомендации по теме
Комментарии
Автор

Can we use queue where we pop the first element and push It to the end

yashogg
Автор

package arrays;
public class RotateArray {

public static void main(String[] args) {
// TODO Auto-generated method stub
int[] arr = {1, 2, 3, 4, 5, 6, 7, 8};
int val = 12;
if (val == 0)
return;

int n = arr.length;
// in case the rotating factor is
// greater than array length
val = val % n;
System.out.println(val);
removeElement(arr, 0, val - 1);
removeElement(arr, val, n - 1);
removeElement(arr, 0, n - 1);
printArray(arr);

}

private static int[] removeElement(int[] arr, int start, int end) {
// TODO Auto-generated method stub
int temp;
while (start < end) {
temp = arr[start];
arr[start] = arr[end];
arr[end] = temp;
start++;
end--;
}
return arr;
}
private static void printArray(int[] arr) {
// TODO Auto-generated method stub
for(int i=0;i<arr.length;i++)
{
at "+ i +"th index is "+ arr[i]);
}

}


}

kalvakolusubhash
Автор

class position{
public static void main(String[] args){
int[] a={1, 2, 3, 4, 5};
int temp=a[2];
a[2]=a[4];
a[4]=temp;

}
}

vamshivarma
Автор

int arr[]={1, 2, 3, 4, 5};
int n=3;
for(int i=0;i<n;i++) {
int first=arr[0], j;
for( j=0;j<arr.length-1;i++) {
arr[j]=arr[j+1];
}
arr[j]=first;
}
for(int i=0;i<arr.length;i++) {
System.out.print(arr[i]+" ");
}
its not working

saoae
Автор

public class Main{

public static void main(String[] args) {
int[] raju= new int[] {1, 2, 3, 4, 5};
int n=3;
for(int i=0;i<n;i++){
int rani=raju[0], j;
for(j=0;j<raju.length-1;j++){
raju[j]=raju[j+1];
}
raju[j]=rani;

}

for(int i=0;i<raju.length;i++){


System.out.println(raju[i]+" ");
}}}

output is
1
2
3
i didnot find error plz find error

rajumanepalli
visit shbcf.ru