Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6531537
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 25, 20262026-05-25T09:53:23+00:00 2026-05-25T09:53:23+00:00

I have implemented a function TraverseStringClockwise which takes a comma separated string of integers,

  • 0

I have implemented a function TraverseStringClockwise which takes a comma separated string of integers, and a width and a height, and returns a new string by walking in a clockwise order using 2D Char Array. I am trying to do the same using 1D array, but I am running into issues.

Example:

Str=”1,2,3,4,5,6,7,8,9,10,11,12”;Width=3;Height=4;Returnstr=”1,2,3,6,9,12,11,10,7,4,5,8”

Any pointers / help ?

here’s the code

public class TraverseStringClockwise {

// Build 2-dimensional matrix representing h * w
public static String[][] buildMatrix(String[] s, int width, int height)
{
    String[][] matrix = new String[height][width];
    int charPos = 0;
    for(int i = 0; i < height; i++)
    {
        for(int j = 0; j < width; j++)
        {
            matrix[i][j] = s[charPos++];
        }
    }
    return matrix;
}

public static String traverseStringClockwise(String s, int width, int height)
{
    // invalid if width or height are zero or there aren't enough elems in String to fill the matrix
    if(s == null || width == 0 || height == 0 || (s.split(",").length != width * height) )
    {
        return null;
    }
    String[][] matrix = buildMatrix(s.split(","), width, height); // O(n) where n = w*h
    Cursor cursor = new Cursor(width, height);
    StringBuilder sb = new StringBuilder();
    while(!cursor.isWalkComplete()) // // O(n) where n = w*h
    {
        cursor.walk();
        sb.append(matrix[cursor.colPos][cursor.rowPos]);
        if(!cursor.isWalkComplete())
        {
            sb.append(",");
        }
    }
    return (sb.length() > 1) ? sb.toString() : null;
}


/**
 * @param args
 */
public static void main(String[] args) {
    // TODO Auto-generated method stub
    String input = "1,2,3,4,5,6,7,8,9,10,11,12";
    int width = 3, height = 4;
    String[][] block = buildMatrix(input.split(","), 3, 4);
    System.out.println("INPUT = " + input);
    System.out.println("OUTPUT = " + walkStringClockwise(input, width, height));
}

}

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-25T09:53:23+00:00Added an answer on May 25, 2026 at 9:53 am

    If you already have the code to walk the array in clockwise order in a 2D array, you can use a standard trick to convert this code to using just a 1D array by linearizing the 2D array into a 1D array. One way to do this is to store the 2D array as a 1D array in row-major order. For example, given this 2D array:

     1  2  3
     4  5  6
     7  8  9
    10 11 12
    

    You would encode it as the 1D array

     1 2 3 4 5 6 7 8 9 10 11 12
    

    That is, you lay out all of the elements of the first row, then all the elements of the second row, then all the elements of the third row, etc.

    The advantage of this approach is that if you are given an index (row, col) into the original 2D array, you can find the matching position in the 1D array efficiently. To see how to do this, note that each horizontal step you take in the original array corresponds to a horizontal step in the 1D array, while each vertical step you take in the originl array corresponds to skipping over one row of elements in the 1D array. Overall, th formula is that the element at (row, col) in the 2D array can be found at position row * width + col in the 1D array.

    Given this, you can try rewriting your code to just use a 1D array, replacing all instances where you were using the 2D array with corresponding code to access the proper element in the 1D array as described above.

    Hope this helps!

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have implemented below function in a separate .js script file which I load
For an Android application, I have implemented an external function in C, which I
If I have a function foo() that windows has implemented in kernel32.dll and it
I have implemented a python webserver. Each http request spawns a new thread. I
i have implemented a function: def postback(i,user,tval): functie ce posteaza raspunsul bazei de date;stringul
I have implemented the javascript function that allows me to do a .NET (4.0)
I have implemented the following function to be used with showDialog() and onDialogCreate(), but
I have implemented a search function using Castel Active Record. I thought the code
I have implemented the sort function in my codebehind, it works fine with words
I have implemented the same function distancebetween as in Nerddinner. I created an airport

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.