Rotate array to the left The 2019 Stack Overflow Developer Survey Results Are InCycling between element above and below current array elementSearching an element in a sorted arrayPartitioning array elements into two subsetsFind the max. difference between two array elements a[j] and a[i] such that j > iLeft Shifting an array of intsIdentifying array elements, rearranging them, and replacing themRotate an array to the right by a given number of stepsTo the right, to the left, now rotateFill 2D array recursivelyFor an array, check if an index exists where the sum of the elements to the left of it is equal to the sum of the elements right of it

How do you keep chess fun when your opponent constantly beats you?

For what reasons would an animal species NOT cross a *horizontal* land bridge?

What could be the right powersource for 15 seconds lifespan disposable giant chainsaw?

Is it safe to harvest rainwater that fell on solar panels?

Does HR tell a hiring manager about salary negotiations?

Getting crown tickets for Statue of Liberty

Why can't devices on different VLANs, but on the same subnet, communicate?

Does adding complexity mean a more secure cipher?

How to obtain a position of last non-zero element

Is it ok to offer lower paid work as a trial period before negotiating for a full-time job?

Keeping a retro style to sci-fi spaceships?

Why was M87 targeted for the Event Horizon Telescope instead of Sagittarius A*?

Old scifi movie from the 50s or 60s with men in solid red uniforms who interrogate a spy from the past

Ubuntu Server install with full GUI

What to do when moving next to a bird sanctuary with a loosely-domesticated cat?

Mathematics of imaging the black hole

writing variables above the numbers in tikz picture

How to translate "being like"?

Falsification in Math vs Science

Why “相同意思的词” is called “同义词” instead of "同意词"?

Did Scotland spend $250,000 for the slogan "Welcome to Scotland"?

Are spiders unable to hurt humans, especially very small spiders?

Dropping list elements from nested list after evaluation

Relationship between Gromov-Witten and Taubes' Gromov invariant



Rotate array to the left



The 2019 Stack Overflow Developer Survey Results Are InCycling between element above and below current array elementSearching an element in a sorted arrayPartitioning array elements into two subsetsFind the max. difference between two array elements a[j] and a[i] such that j > iLeft Shifting an array of intsIdentifying array elements, rearranging them, and replacing themRotate an array to the right by a given number of stepsTo the right, to the left, now rotateFill 2D array recursivelyFor an array, check if an index exists where the sum of the elements to the left of it is equal to the sum of the elements right of it



.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty,.everyoneloves__bot-mid-leaderboard:empty margin-bottom:0;








4












$begingroup$


I am a beginner in programming. To rotate the array to left, I have written the below code. Please point out how to optimize the code and anything I am doing which is not good programming practice.



package arrays;

import java.util.Scanner;

public class ArrayLeftRotation

public static int[] LeftRotation(int numberOfLeftRotations, int[] a, int[] b)

int n1 = numberOfLeftRotations, n2 = numberOfLeftRotations;
/*
* First for loop Given array a0=1,a1=2,a3=4,a4=5,a5=6,a6=7; First we
* are shifting the first 3 elements to last 3 slots and the array looks
* like below a0=1,a1=2,a2=3,a3=,a4=1,a5=2,a6=3; The above step we are
* performing through below logic
* a[array.length-numberOfLeftRotations]=b[i]; GIVEN ARRAY LENGTH -
* NUMBER OF LEFT ROTATIONS IS THE STARTING POINT TO MOVE THE FIRST
* ELEMENTS OF THE ARRAY TO LAST
*/
for (int i = 0; i < numberOfLeftRotations; i++)
a[a.length - n1] = b[i];
n1--;

/*
* Second for loop After executing the above loop the array look like
* below a0=1,a1=2,a2=3,a3=,a4=1,a5=2,a6=3; now except the last three
* slots every thing needs to be changed
* a0=4,a1=5,a2=6,a3=7,a4=1,a5=2,a6=3; we achieved the above array
* result through below logic a[i]=b[numberOfLeftRotations]; REPLACED
* THE REMAINING SLOTS WITH THE VALUES STARTING FROM
* NUMBEROFLEFTROTATIONS
*/
for (int i = 0; i < a.length - numberOfLeftRotations; i++)
a[i] = b[n2];
n2++;

return a;


public static int[] LeftRotation(int numberOfLeftRotations, int[] a)

return a;



public static int[] printArray(int[] a)

for (int i = 0; i < a.length; i++)
System.out.println(a[i]);

return a;


public static void main(String[] args)
// TODO Auto-generated method stub
Scanner sc = new Scanner(System.in);
System.out.println("Enter number of Left Rotations need to be done");
int l = sc.nextInt();
System.out.println("Enter number of elements in an array");
int e = sc.nextInt();
int[] a = new int[e];

for (int i = 0; i < a.length; i++)
a[i] = sc.nextInt();


int[] b = a.clone();

LeftRotation(l, a, b);
printArray(a);












share|improve this question









New contributor




varaprasad is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.







$endgroup$







  • 1




    $begingroup$
    Why do you have second rotation function?
    $endgroup$
    – E.Coms
    3 hours ago










  • $begingroup$
    In second rotation function I am actually replacing the initial elements in an array which are not replaced in first function.
    $endgroup$
    – varaprasad
    2 hours ago

















4












$begingroup$


I am a beginner in programming. To rotate the array to left, I have written the below code. Please point out how to optimize the code and anything I am doing which is not good programming practice.



package arrays;

import java.util.Scanner;

public class ArrayLeftRotation

public static int[] LeftRotation(int numberOfLeftRotations, int[] a, int[] b)

int n1 = numberOfLeftRotations, n2 = numberOfLeftRotations;
/*
* First for loop Given array a0=1,a1=2,a3=4,a4=5,a5=6,a6=7; First we
* are shifting the first 3 elements to last 3 slots and the array looks
* like below a0=1,a1=2,a2=3,a3=,a4=1,a5=2,a6=3; The above step we are
* performing through below logic
* a[array.length-numberOfLeftRotations]=b[i]; GIVEN ARRAY LENGTH -
* NUMBER OF LEFT ROTATIONS IS THE STARTING POINT TO MOVE THE FIRST
* ELEMENTS OF THE ARRAY TO LAST
*/
for (int i = 0; i < numberOfLeftRotations; i++)
a[a.length - n1] = b[i];
n1--;

/*
* Second for loop After executing the above loop the array look like
* below a0=1,a1=2,a2=3,a3=,a4=1,a5=2,a6=3; now except the last three
* slots every thing needs to be changed
* a0=4,a1=5,a2=6,a3=7,a4=1,a5=2,a6=3; we achieved the above array
* result through below logic a[i]=b[numberOfLeftRotations]; REPLACED
* THE REMAINING SLOTS WITH THE VALUES STARTING FROM
* NUMBEROFLEFTROTATIONS
*/
for (int i = 0; i < a.length - numberOfLeftRotations; i++)
a[i] = b[n2];
n2++;

return a;


public static int[] LeftRotation(int numberOfLeftRotations, int[] a)

return a;



public static int[] printArray(int[] a)

for (int i = 0; i < a.length; i++)
System.out.println(a[i]);

return a;


public static void main(String[] args)
// TODO Auto-generated method stub
Scanner sc = new Scanner(System.in);
System.out.println("Enter number of Left Rotations need to be done");
int l = sc.nextInt();
System.out.println("Enter number of elements in an array");
int e = sc.nextInt();
int[] a = new int[e];

for (int i = 0; i < a.length; i++)
a[i] = sc.nextInt();


int[] b = a.clone();

LeftRotation(l, a, b);
printArray(a);












share|improve this question









New contributor




varaprasad is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.







$endgroup$







  • 1




    $begingroup$
    Why do you have second rotation function?
    $endgroup$
    – E.Coms
    3 hours ago










  • $begingroup$
    In second rotation function I am actually replacing the initial elements in an array which are not replaced in first function.
    $endgroup$
    – varaprasad
    2 hours ago













4












4








4





$begingroup$


I am a beginner in programming. To rotate the array to left, I have written the below code. Please point out how to optimize the code and anything I am doing which is not good programming practice.



package arrays;

import java.util.Scanner;

public class ArrayLeftRotation

public static int[] LeftRotation(int numberOfLeftRotations, int[] a, int[] b)

int n1 = numberOfLeftRotations, n2 = numberOfLeftRotations;
/*
* First for loop Given array a0=1,a1=2,a3=4,a4=5,a5=6,a6=7; First we
* are shifting the first 3 elements to last 3 slots and the array looks
* like below a0=1,a1=2,a2=3,a3=,a4=1,a5=2,a6=3; The above step we are
* performing through below logic
* a[array.length-numberOfLeftRotations]=b[i]; GIVEN ARRAY LENGTH -
* NUMBER OF LEFT ROTATIONS IS THE STARTING POINT TO MOVE THE FIRST
* ELEMENTS OF THE ARRAY TO LAST
*/
for (int i = 0; i < numberOfLeftRotations; i++)
a[a.length - n1] = b[i];
n1--;

/*
* Second for loop After executing the above loop the array look like
* below a0=1,a1=2,a2=3,a3=,a4=1,a5=2,a6=3; now except the last three
* slots every thing needs to be changed
* a0=4,a1=5,a2=6,a3=7,a4=1,a5=2,a6=3; we achieved the above array
* result through below logic a[i]=b[numberOfLeftRotations]; REPLACED
* THE REMAINING SLOTS WITH THE VALUES STARTING FROM
* NUMBEROFLEFTROTATIONS
*/
for (int i = 0; i < a.length - numberOfLeftRotations; i++)
a[i] = b[n2];
n2++;

return a;


public static int[] LeftRotation(int numberOfLeftRotations, int[] a)

return a;



public static int[] printArray(int[] a)

for (int i = 0; i < a.length; i++)
System.out.println(a[i]);

return a;


public static void main(String[] args)
// TODO Auto-generated method stub
Scanner sc = new Scanner(System.in);
System.out.println("Enter number of Left Rotations need to be done");
int l = sc.nextInt();
System.out.println("Enter number of elements in an array");
int e = sc.nextInt();
int[] a = new int[e];

for (int i = 0; i < a.length; i++)
a[i] = sc.nextInt();


int[] b = a.clone();

LeftRotation(l, a, b);
printArray(a);












share|improve this question









New contributor




varaprasad is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.







$endgroup$




I am a beginner in programming. To rotate the array to left, I have written the below code. Please point out how to optimize the code and anything I am doing which is not good programming practice.



package arrays;

import java.util.Scanner;

public class ArrayLeftRotation

public static int[] LeftRotation(int numberOfLeftRotations, int[] a, int[] b)

int n1 = numberOfLeftRotations, n2 = numberOfLeftRotations;
/*
* First for loop Given array a0=1,a1=2,a3=4,a4=5,a5=6,a6=7; First we
* are shifting the first 3 elements to last 3 slots and the array looks
* like below a0=1,a1=2,a2=3,a3=,a4=1,a5=2,a6=3; The above step we are
* performing through below logic
* a[array.length-numberOfLeftRotations]=b[i]; GIVEN ARRAY LENGTH -
* NUMBER OF LEFT ROTATIONS IS THE STARTING POINT TO MOVE THE FIRST
* ELEMENTS OF THE ARRAY TO LAST
*/
for (int i = 0; i < numberOfLeftRotations; i++)
a[a.length - n1] = b[i];
n1--;

/*
* Second for loop After executing the above loop the array look like
* below a0=1,a1=2,a2=3,a3=,a4=1,a5=2,a6=3; now except the last three
* slots every thing needs to be changed
* a0=4,a1=5,a2=6,a3=7,a4=1,a5=2,a6=3; we achieved the above array
* result through below logic a[i]=b[numberOfLeftRotations]; REPLACED
* THE REMAINING SLOTS WITH THE VALUES STARTING FROM
* NUMBEROFLEFTROTATIONS
*/
for (int i = 0; i < a.length - numberOfLeftRotations; i++)
a[i] = b[n2];
n2++;

return a;


public static int[] LeftRotation(int numberOfLeftRotations, int[] a)

return a;



public static int[] printArray(int[] a)

for (int i = 0; i < a.length; i++)
System.out.println(a[i]);

return a;


public static void main(String[] args)
// TODO Auto-generated method stub
Scanner sc = new Scanner(System.in);
System.out.println("Enter number of Left Rotations need to be done");
int l = sc.nextInt();
System.out.println("Enter number of elements in an array");
int e = sc.nextInt();
int[] a = new int[e];

for (int i = 0; i < a.length; i++)
a[i] = sc.nextInt();


int[] b = a.clone();

LeftRotation(l, a, b);
printArray(a);









java beginner array






share|improve this question









New contributor




varaprasad is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.











share|improve this question









New contributor




varaprasad is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.









share|improve this question




share|improve this question








edited 3 hours ago









200_success

131k17157422




131k17157422






New contributor




varaprasad is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.









asked 4 hours ago









varaprasadvaraprasad

211




211




New contributor




varaprasad is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.





New contributor





varaprasad is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.






varaprasad is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.







  • 1




    $begingroup$
    Why do you have second rotation function?
    $endgroup$
    – E.Coms
    3 hours ago










  • $begingroup$
    In second rotation function I am actually replacing the initial elements in an array which are not replaced in first function.
    $endgroup$
    – varaprasad
    2 hours ago












  • 1




    $begingroup$
    Why do you have second rotation function?
    $endgroup$
    – E.Coms
    3 hours ago










  • $begingroup$
    In second rotation function I am actually replacing the initial elements in an array which are not replaced in first function.
    $endgroup$
    – varaprasad
    2 hours ago







1




1




$begingroup$
Why do you have second rotation function?
$endgroup$
– E.Coms
3 hours ago




$begingroup$
Why do you have second rotation function?
$endgroup$
– E.Coms
3 hours ago












$begingroup$
In second rotation function I am actually replacing the initial elements in an array which are not replaced in first function.
$endgroup$
– varaprasad
2 hours ago




$begingroup$
In second rotation function I am actually replacing the initial elements in an array which are not replaced in first function.
$endgroup$
– varaprasad
2 hours ago










0






active

oldest

votes












Your Answer





StackExchange.ifUsing("editor", function ()
return StackExchange.using("mathjaxEditing", function ()
StackExchange.MarkdownEditor.creationCallbacks.add(function (editor, postfix)
StackExchange.mathjaxEditing.prepareWmdForMathJax(editor, postfix, [["\$", "\$"]]);
);
);
, "mathjax-editing");

StackExchange.ifUsing("editor", function ()
StackExchange.using("externalEditor", function ()
StackExchange.using("snippets", function ()
StackExchange.snippets.init();
);
);
, "code-snippets");

StackExchange.ready(function()
var channelOptions =
tags: "".split(" "),
id: "196"
;
initTagRenderer("".split(" "), "".split(" "), channelOptions);

StackExchange.using("externalEditor", function()
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled)
StackExchange.using("snippets", function()
createEditor();
);

else
createEditor();

);

function createEditor()
StackExchange.prepareEditor(
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: false,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: null,
bindNavPrevention: true,
postfix: "",
imageUploader:
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
,
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
);



);






varaprasad is a new contributor. Be nice, and check out our Code of Conduct.









draft saved

draft discarded


















StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fcodereview.stackexchange.com%2fquestions%2f217280%2frotate-array-to-the-left%23new-answer', 'question_page');

);

Post as a guest















Required, but never shown

























0






active

oldest

votes








0






active

oldest

votes









active

oldest

votes






active

oldest

votes








varaprasad is a new contributor. Be nice, and check out our Code of Conduct.









draft saved

draft discarded


















varaprasad is a new contributor. Be nice, and check out our Code of Conduct.












varaprasad is a new contributor. Be nice, and check out our Code of Conduct.











varaprasad is a new contributor. Be nice, and check out our Code of Conduct.














Thanks for contributing an answer to Code Review Stack Exchange!


  • Please be sure to answer the question. Provide details and share your research!

But avoid


  • Asking for help, clarification, or responding to other answers.

  • Making statements based on opinion; back them up with references or personal experience.

Use MathJax to format equations. MathJax reference.


To learn more, see our tips on writing great answers.




draft saved


draft discarded














StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fcodereview.stackexchange.com%2fquestions%2f217280%2frotate-array-to-the-left%23new-answer', 'question_page');

);

Post as a guest















Required, but never shown





















































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown

































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown







Popular posts from this blog

名間水力發電廠 目录 沿革 設施 鄰近設施 註釋 外部連結 导航菜单23°50′10″N 120°42′41″E / 23.83611°N 120.71139°E / 23.83611; 120.7113923°50′10″N 120°42′41″E / 23.83611°N 120.71139°E / 23.83611; 120.71139計畫概要原始内容臺灣第一座BOT 模式開發的水力發電廠-名間水力電廠名間水力發電廠 水利署首件BOT案原始内容《小檔案》名間電廠 首座BOT水力發電廠原始内容名間電廠BOT - 經濟部水利署中區水資源局

格濟夫卡 參考資料 导航菜单51°3′40″N 34°2′21″E / 51.06111°N 34.03917°E / 51.06111; 34.03917ГезівкаПогода в селі 编辑或修订