├── .github └── workflows │ ├── cd.yml │ └── ci.yml ├── .gitignore ├── Cargo.toml ├── LICENSE ├── README.md └── src └── main.rs /.github/workflows/cd.yml: -------------------------------------------------------------------------------- 1 | name: CD 2 | 3 | on: 4 | push: 5 | branches: 6 | - main 7 | 8 | jobs: 9 | build: 10 | runs-on: ubuntu-latest 11 | steps: 12 | - uses: actions/checkout@v3 13 | - name: Set up Rust 14 | uses: actions-rs/toolchain@v1 15 | with: 16 | toolchain: stable 17 | override: true 18 | - name: Build project 19 | run: cargo build --release 20 | -------------------------------------------------------------------------------- /.github/workflows/ci.yml: -------------------------------------------------------------------------------- 1 | name: CI 2 | 3 | on: 4 | push: 5 | branches: 6 | - main 7 | pull_request: 8 | branches: 9 | - main 10 | 11 | jobs: 12 | test: 13 | runs-on: ubuntu-latest 14 | steps: 15 | - uses: actions/checkout@v3 16 | - name: Set up Rust 17 | uses: actions-rs/toolchain@v1 18 | with: 19 | toolchain: stable 20 | override: true 21 | - name: Run tests 22 | run: cargo test 23 | -------------------------------------------------------------------------------- /.gitignore: -------------------------------------------------------------------------------- 1 | # Generated by Cargo 2 | # will have compiled files and executables 3 | debug/ 4 | target/ 5 | 6 | # Remove Cargo.lock from gitignore if creating an executable, leave it for libraries 7 | # More information here https://doc.rust-lang.org/cargo/guide/cargo-toml-vs-cargo-lock.html 8 | Cargo.lock 9 | 10 | # These are backup files generated by rustfmt 11 | **/*.rs.bk 12 | 13 | # MSVC Windows builds of rustc generate these, which store debugging information 14 | *.pdb 15 | 16 | # RustRover 17 | # JetBrains specific template is maintained in a separate JetBrains.gitignore that can 18 | # be found at https://github.com/github/gitignore/blob/main/Global/JetBrains.gitignore 19 | # and can be added to the global gitignore or merged into this file. For a more nuclear 20 | # option (not recommended) you can uncomment the following to ignore the entire idea folder. 21 | #.idea/ 22 | 23 | # Added by cargo 24 | 25 | /target 26 | -------------------------------------------------------------------------------- /Cargo.toml: -------------------------------------------------------------------------------- 1 | [package] 2 | name = "final-array-state" 3 | version = "0.1.0" 4 | edition = "2021" 5 | 6 | [dependencies] 7 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | MIT License 2 | 3 | Copyright (c) 2025 aliezza hn 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy 6 | of this software and associated documentation files (the "Software"), to deal 7 | in the Software without restriction, including without limitation the rights 8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | copies of the Software, and to permit persons to whom the Software is 10 | furnished to do so, subject to the following conditions: 11 | 12 | The above copyright notice and this permission notice shall be included in all 13 | copies or substantial portions of the Software. 14 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 21 | SOFTWARE. 22 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # Final Array State 2 | 3 | This Rust project computes the final state of an array after performing a series of multiplication operations on its elements. It is designed to be efficient and easy to use. 4 | 5 | ![CI](https://github.com/aliezzahn/final-array-state/actions/workflows/ci.yml/badge.svg) 6 | ![CD](https://github.com/aliezzahn/final-array-state/actions/workflows/cd.yml/badge.svg) 7 | [![License: MIT](https://img.shields.io/badge/License-MIT-yellow.svg)](https://opensource.org/licenses/MIT) 8 | 9 | --- 10 | 11 | ## Table of Contents 12 | 13 | - [Final Array State](#final-array-state) 14 | - [Table of Contents](#table-of-contents) 15 | - [Introduction](#introduction) 16 | - [Features](#features) 17 | - [Installation](#installation) 18 | - [Usage](#usage) 19 | - [Example Usage](#example-usage) 20 | - [Output](#output) 21 | - [Example](#example) 22 | - [Input](#input) 23 | - [Execution](#execution) 24 | - [Output](#output-1) 25 | - [Contributing](#contributing) 26 | - [License](#license) 27 | - [Acknowledgments](#acknowledgments) 28 | - [Contact](#contact) 29 | 30 | --- 31 | 32 | ## Introduction 33 | 34 | The project provides a function `get_final_state` that takes an array of integers, a number of operations `k`, and a `multiplier`. It performs `k` multiplication operations on the smallest element of the array and returns the final state of the array. 35 | 36 | This is particularly useful for scenarios where you need to repeatedly modify the smallest elements of an array and track the changes efficiently. 37 | 38 | --- 39 | 40 | ## Features 41 | 42 | - **Efficient Operations**: Uses a min-heap to efficiently find and update the smallest elements in the array. 43 | - **Modular Arithmetic**: Ensures that results stay within bounds using modular arithmetic. 44 | - **Easy to Use**: Simple API with clear documentation. 45 | 46 | --- 47 | 48 | ## Installation 49 | 50 | To use this project, you need to have Rust installed on your system. If you don't have Rust installed, follow the instructions at [rustup.rs](https://rustup.rs/). 51 | 52 | Once Rust is installed, clone the repository: 53 | 54 | ```bash 55 | git clone https://github.com/aliezzahn/final-array-state.git 56 | cd final-array-state 57 | ``` 58 | 59 | Build the project: 60 | 61 | ```bash 62 | cargo build 63 | ``` 64 | 65 | --- 66 | 67 | ## Usage 68 | 69 | The main function is `get_final_state`, which takes the following parameters: 70 | 71 | - `nums`: A vector of integers representing the initial state of the array. 72 | - `k`: The number of multiplication operations to perform. 73 | - `multiplier`: The value by which the smallest element is multiplied in each operation. 74 | 75 | ### Example Usage 76 | 77 | ```rust 78 | fn main() { 79 | let nums = vec![2, 1]; 80 | let k = 3; 81 | let multiplier = 10; 82 | 83 | let result = get_final_state(nums, k, multiplier); 84 | println!("Final state: {:?}", result); 85 | } 86 | ``` 87 | 88 | ### Output 89 | 90 | ``` 91 | Final state: [20, 100] 92 | ``` 93 | 94 | --- 95 | 96 | ## Example 97 | 98 | ### Input 99 | 100 | ```rust 101 | let nums = vec![2, 1]; 102 | let k = 3; 103 | let multiplier = 10; 104 | ``` 105 | 106 | ### Execution 107 | 108 | 1. Initial array: `[2, 1]`. 109 | 2. After 1st operation: `[2, 10]` (multiply `1` by `10`). 110 | 3. After 2nd operation: `[20, 10]` (multiply `2` by `10`). 111 | 4. After 3rd operation: `[20, 100]` (multiply `10` by `10`). 112 | 113 | ### Output 114 | 115 | ``` 116 | Final state: [20, 100] 117 | ``` 118 | 119 | --- 120 | 121 | ## Contributing 122 | 123 | Contributions are welcome! If you'd like to contribute, please follow these steps: 124 | 125 | 1. Fork the repository. 126 | 2. Create a new branch for your feature or bugfix. 127 | 3. Make your changes and commit them. 128 | 4. Push your changes to your fork. 129 | 5. Submit a pull request. 130 | 131 | Please ensure your code follows the project's coding standards and includes appropriate tests. 132 | 133 | --- 134 | 135 | ## License 136 | 137 | This project is licensed under the MIT License. See the [LICENSE](LICENSE) file for details. 138 | 139 | --- 140 | 141 | ## Acknowledgments 142 | 143 | - Thanks to the Rust community for creating such an amazing programming language. 144 | - Special thanks to everyone who contributed to this project. 145 | 146 | --- 147 | 148 | ## Contact 149 | 150 | If you have any questions or suggestions, feel free to reach out: 151 | 152 | - **GitHub**: [aliezzahn](https://github.com/aliezzahn) 153 | - **Email**: [aliezzahn@gmail.com](mailto:aliezzahn@gmail.com) 154 | 155 | --- 156 | -------------------------------------------------------------------------------- /src/main.rs: -------------------------------------------------------------------------------- 1 | use std::collections::{BinaryHeap, HashSet}; 2 | use std::cmp::Reverse; 3 | 4 | const MOD: i64 = 1_000_000_007; 5 | 6 | fn get_final_state(nums: Vec, k: i32, multiplier: i32) -> Vec { 7 | if multiplier == 1 { 8 | return nums; // No changes needed if multiplier is 1 9 | } 10 | 11 | let n = nums.len(); 12 | let mut k = k as usize; // Declare k as mutable 13 | let multiplier = multiplier as i64; 14 | 15 | // Min-heap to track the smallest elements and their indices 16 | let mut heap: BinaryHeap> = nums 17 | .iter() 18 | .enumerate() 19 | .map(|(i, &val)| Reverse((val as i64, i))) 20 | .collect(); 21 | 22 | // Set to track unseen indices 23 | let mut unseen: HashSet = (0..n).collect(); 24 | 25 | // Perform operations until all indices have been processed or k is exhausted 26 | while k > 0 && !unseen.is_empty() { 27 | if let Some(Reverse((num, idx))) = heap.pop() { 28 | // Multiply the smallest element by the multiplier 29 | let new_num = num * multiplier; 30 | heap.push(Reverse((new_num, idx))); 31 | unseen.remove(&idx); 32 | k -= 1; 33 | } 34 | } 35 | 36 | // Vector to store the final results 37 | let mut ans = vec![0; n]; 38 | 39 | // Distribute remaining operations and compute final values 40 | for i in 0..n { 41 | if let Some(Reverse((num, idx))) = heap.pop() { 42 | // Calculate the number of operations for this index 43 | let operations = k / n + if i < k % n { 1 } else { 0 }; 44 | // Compute the multiplier raised to the power of operations, modulo MOD 45 | let mlt = power_mod(multiplier, operations as i64, MOD); 46 | // Compute the final value for this index 47 | ans[idx] = ((num % MOD) * mlt) % MOD; 48 | } 49 | } 50 | 51 | // Convert the results back to i32 52 | ans.into_iter().map(|x| x as i32).collect() 53 | } 54 | 55 | // Modular exponentiation function 56 | fn power_mod(base: i64, exp: i64, mod_val: i64) -> i64 { 57 | let mut result = 1; 58 | let mut base = base % mod_val; 59 | let mut exp = exp; 60 | while exp > 0 { 61 | if exp % 2 == 1 { 62 | result = (result * base) % mod_val; 63 | } 64 | base = (base * base) % mod_val; 65 | exp /= 2; 66 | } 67 | result 68 | } 69 | 70 | fn main() { 71 | let nums = vec![2, 1]; 72 | let k = 3; 73 | let multiplier = 10; 74 | 75 | let result = get_final_state(nums, k, multiplier); 76 | println!("Final state: {:?}", result); 77 | } --------------------------------------------------------------------------------