DEV Community

Cover image for Hi, New to writing
Vinayak
Vinayak

Posted on

1 1 1 1 1

Hi, New to writing

def test_response(args)
    print args[:happy_reacts] 
end
Enter fullscreen mode Exit fullscreen mode

I hope to write at least once a week on dev.to . It's a request to the dev.to community to support me and others like me to write more.

Now going ahead I just want to add a small code snippet to rotate the array in rust.

it was pratice problem in hackerearth

I'll just explain here what i wrote in Rust(new to rust so your feedback is valuable in comments).

The video has better explanation.

The first step was to get the number of test cases.

let mut input = String::new();
    io::stdin().read_line(&mut input).expect("Failed to read input");
    let t: usize = input.trim().parse().expect("Invalid input");
Enter fullscreen mode Exit fullscreen mode

then loop form 0 to t

for _ in 0..t {
}
Enter fullscreen mode Exit fullscreen mode

inside the loop add another input to get the length of array and the times that rotation has to performed.

 let mut input_n_k = String::new();
        io::stdin().read_line(&mut input_n_k).expect("Failed to read input");
        let mut nk = input_n_k.trim().split_whitespace();
        let n: usize = nk.next().unwrap().parse().expect("Invalid input");
        let k: usize = nk.next().unwrap().parse().expect("Invalid input");
Enter fullscreen mode Exit fullscreen mode

following that another input stream to get the array and store in a Vector of i32 type. (i32 was within limits of the test cases)

let mut input_arr = String::new();
        io::stdin().read_line(&mut input_arr).expect("Failed to read input");
        let mut elements: Vec<i32> = input_arr.trim().split_whitespace()
            .map(|x| x.parse().expect("Invalid input"))
            .collect();
Enter fullscreen mode Exit fullscreen mode

now before performing the array rotation it is good practice sanitise the rotation times and the length of the array

 let k = k % n; // Handle cases where k is larger than n
Enter fullscreen mode Exit fullscreen mode

now create a new array to store the results

let mut result: Vec<i32> = Vec::with_capacity(n);
Enter fullscreen mode Exit fullscreen mode

the actual rotation operation

for i in 0..n {
            let new_index = (n + i - k) % n;
            result.push(elements[new_index]);
        }
Enter fullscreen mode Exit fullscreen mode

the magic of rotation happens when the new_index is created.
e.g., if n =5 and k =2
in this rotation loop
new_index = (5+0-2)%5 = 3
new_index = (5+1-2)%5 = 4
new_index = (5+2-2)%5 = 0
new_index = (5+3-2)%5 = 1
new_index = (5+5-2)%5 = 3

the new_index variable denotes the current index and when to push in the result array.

finally print the result and a new line to continue

      for i in 0..n {
            print!("{} ", result[i]);
        }
        println!();
Enter fullscreen mode Exit fullscreen mode

This was simple array manipulation question solved using Rust.

The concepts of Rust used here are mutables,loops and Vectors.

AWS Q Developer image

Your AI Code Assistant

Automate your code reviews. Catch bugs before your coworkers. Fix security issues in your code. Built to handle large projects, Amazon Q Developer works alongside you from idea to production code.

Get started free in your IDE

Top comments (0)

Billboard image

Create up to 10 Postgres Databases on Neon's free plan.

If you're starting a new project, Neon has got your databases covered. No credit cards. No trials. No getting in your way.

Try Neon for Free →

👋 Kindness is contagious

Please leave a ❤️ or a friendly comment on this post if you found it helpful!

Okay