HackerRank Left rotation code using Java 8Encoding a screenshot to JPEG and saving it to a...

How would a Dictatorship make a country more successful?

What is the purpose of easy combat scenarios that don't need resource expenditure?

Dilemma of explaining to interviewer that he is the reason for declining second interview

How can animals be objects of ethics without being subjects as well?

If I delete my router's history can my ISP still provide it to my parents?

Strange Sign on Lab Door

What is better: yes / no radio, or simple checkbox?

gimp: use modifiable layer as mask of another layer

name character vectors with same name of list

What kind of hardware implements Fourier transform?

Closed form for these polynomials?

How would one buy a used TIE Fighter or X-Wing?

Why did Jodrell Bank assist the Soviet Union to collect data from their spacecraft in the mid 1960's?

Why exactly do action photographers need high fps burst cameras?

On infinite combinatorics of ultrafilters

What to do if authors don't respond to my serious concerns about their paper?

Would a National Army of mercenaries be a feasible idea?

A starship is travelling at 0.9c and collides with a small rock. Will it leave a clean hole through, or will more happen?

It took me a lot of time to make this, pls like. (YouTube Comments #1)

Pre-1980's science fiction short story: alien disguised as a woman shot by a gangster, has tentacles coming out of her breasts when remaking her body

Every character has a name - does this lead to too many named characters?

Is residual finiteness a quasi isometry invariant for f.g. groups?

dirname appears not to work with xargs

En passant for beginner



HackerRank Left rotation code using Java 8


Encoding a screenshot to JPEG and saving it to a memorystreamListening to modifications on variablesSimple java code measuringImplementation of stackUsing Scanner.nextInt() whilst ignoring rest of lineCircular array rotation JavaHackerRank week of code 32 competition: GeometrickTrickJava Left Rotate Array upto n timesAvoid code duplication javaHackerRank - Left Rotation in JavaScript













0












$begingroup$


I tried to solve this Left Rotation problem on HackerRank. My code works out for most test cases, but for some it doesn't, probably because they have large umber of data. So there seems to be some optimization or performance issue in my code, and I can't really figure out how to fix it. Any help will be greatly appreciated :)



public class Solution {



private static final Scanner scanner = new Scanner(System.in);

public static void main(String[] args) {
String[] nd = scanner.nextLine().split(" ");
int c;
int n = Integer.parseInt(nd[0]);

int d = Integer.parseInt(nd[1]);

int[] a = new int[n];

int arr[] = new int[n];

String[] aItems = scanner.nextLine().split(" ");
scanner.skip("(rn|[nru2028u2029u0085])?");

for (int i = 0; i < n; i++) {
int aItem = Integer.parseInt(aItems[i]);
a[i] = aItem;
}

scanner.close();

for (int k=1;k<=d;k++) {
for (int j=0;j<n;j++) {
if (j==n-1)
{ c=0;}
else
{ c=j+1;}
arr[j]=a[c];
}
System.arraycopy(arr, 0, a, 0, n);
}

for (int j=0;j<n;j++) {
System.out.print(arr[j]+ " ");
}
}


}









share







New contributor




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







$endgroup$

















    0












    $begingroup$


    I tried to solve this Left Rotation problem on HackerRank. My code works out for most test cases, but for some it doesn't, probably because they have large umber of data. So there seems to be some optimization or performance issue in my code, and I can't really figure out how to fix it. Any help will be greatly appreciated :)



    public class Solution {



    private static final Scanner scanner = new Scanner(System.in);

    public static void main(String[] args) {
    String[] nd = scanner.nextLine().split(" ");
    int c;
    int n = Integer.parseInt(nd[0]);

    int d = Integer.parseInt(nd[1]);

    int[] a = new int[n];

    int arr[] = new int[n];

    String[] aItems = scanner.nextLine().split(" ");
    scanner.skip("(rn|[nru2028u2029u0085])?");

    for (int i = 0; i < n; i++) {
    int aItem = Integer.parseInt(aItems[i]);
    a[i] = aItem;
    }

    scanner.close();

    for (int k=1;k<=d;k++) {
    for (int j=0;j<n;j++) {
    if (j==n-1)
    { c=0;}
    else
    { c=j+1;}
    arr[j]=a[c];
    }
    System.arraycopy(arr, 0, a, 0, n);
    }

    for (int j=0;j<n;j++) {
    System.out.print(arr[j]+ " ");
    }
    }


    }









    share







    New contributor




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







    $endgroup$















      0












      0








      0





      $begingroup$


      I tried to solve this Left Rotation problem on HackerRank. My code works out for most test cases, but for some it doesn't, probably because they have large umber of data. So there seems to be some optimization or performance issue in my code, and I can't really figure out how to fix it. Any help will be greatly appreciated :)



      public class Solution {



      private static final Scanner scanner = new Scanner(System.in);

      public static void main(String[] args) {
      String[] nd = scanner.nextLine().split(" ");
      int c;
      int n = Integer.parseInt(nd[0]);

      int d = Integer.parseInt(nd[1]);

      int[] a = new int[n];

      int arr[] = new int[n];

      String[] aItems = scanner.nextLine().split(" ");
      scanner.skip("(rn|[nru2028u2029u0085])?");

      for (int i = 0; i < n; i++) {
      int aItem = Integer.parseInt(aItems[i]);
      a[i] = aItem;
      }

      scanner.close();

      for (int k=1;k<=d;k++) {
      for (int j=0;j<n;j++) {
      if (j==n-1)
      { c=0;}
      else
      { c=j+1;}
      arr[j]=a[c];
      }
      System.arraycopy(arr, 0, a, 0, n);
      }

      for (int j=0;j<n;j++) {
      System.out.print(arr[j]+ " ");
      }
      }


      }









      share







      New contributor




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







      $endgroup$




      I tried to solve this Left Rotation problem on HackerRank. My code works out for most test cases, but for some it doesn't, probably because they have large umber of data. So there seems to be some optimization or performance issue in my code, and I can't really figure out how to fix it. Any help will be greatly appreciated :)



      public class Solution {



      private static final Scanner scanner = new Scanner(System.in);

      public static void main(String[] args) {
      String[] nd = scanner.nextLine().split(" ");
      int c;
      int n = Integer.parseInt(nd[0]);

      int d = Integer.parseInt(nd[1]);

      int[] a = new int[n];

      int arr[] = new int[n];

      String[] aItems = scanner.nextLine().split(" ");
      scanner.skip("(rn|[nru2028u2029u0085])?");

      for (int i = 0; i < n; i++) {
      int aItem = Integer.parseInt(aItems[i]);
      a[i] = aItem;
      }

      scanner.close();

      for (int k=1;k<=d;k++) {
      for (int j=0;j<n;j++) {
      if (j==n-1)
      { c=0;}
      else
      { c=j+1;}
      arr[j]=a[c];
      }
      System.arraycopy(arr, 0, a, 0, n);
      }

      for (int j=0;j<n;j++) {
      System.out.print(arr[j]+ " ");
      }
      }


      }







      java performance





      share







      New contributor




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










      share







      New contributor




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








      share



      share






      New contributor




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









      asked 9 mins ago









      ben trashben trash

      1




      1




      New contributor




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





      New contributor





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






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






















          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
          });


          }
          });






          ben trash 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%2f214606%2fhackerrank-left-rotation-code-using-java-8%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








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










          draft saved

          draft discarded


















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













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












          ben trash 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%2f214606%2fhackerrank-left-rotation-code-using-java-8%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

          Fairchild Swearingen Metro Inhaltsverzeichnis Geschichte | Innenausstattung | Nutzung | Zwischenfälle...

          Pilgersdorf Inhaltsverzeichnis Geografie | Geschichte | Bevölkerungsentwicklung | Politik | Kultur...

          Marineschifffahrtleitung Inhaltsverzeichnis Geschichte | Heutige Organisation der NATO | Nationale und...