


Use addition or subtraction to get the minimum number of steps for N at each step
From the above problem statement, our task is to get the minimum number of steps in which a given number N can be obtained using addition or subtraction in each step. We can understand that we need to print the minimum number of steps that can be performed and the order of steps for any given integer N, by adding and subtracting the step numbers to arrive at a number starting from 0.
In this problem set, we can add or subtract a number equal to the number of steps to the current position at each step. For example, we can add 1 or -1 in step 1. Further we can add 2 or -2 in step 2 and so on. We can add or subtract numbers at each step depending on the situation.
The main challenge of this problem is that we need to perform the minimum number of steps starting from 0 to reach N. Let us understand this issue better with an example.
The example given below will show you every number we can get in 2 steps starting from 0 by doing the above operations.

For example, assume we have N=1.
Output
Minimum no of steps: 1 Sequence of steps: 1
illustrate
We can achieve this in two ways 1 -
Just add 1 to step 1 to move from 0 to 1, which takes 1 step.
Subtract 1 in step 1 to move from 0 to -1, then add 2 in step 2 to move from -1 to 1, which takes 2 steps.
Since the question states that we need a minimum number of steps to reach any number N, the desired output for this input will be 1.
For, N=3
Output
Minimum no of steps: 2 Sequence of steps: 1 2
illustrate
We add 1 in step 1 to move from 0 to 1, then add 2 in step 2 to move from 1 to 3.
method
The best way to solve the problem is to first figure out whether N is a positive or negative number. We must add or subtract the appropriate number of steps respectively to solve the problem.
If N is a positive number, continue adding steps until the sum is greater than or equal to N.
Similarly, if N is a negative number, continue subtracting the number of steps until the sum is greater than or equal to N.
If the sum equals N in the above case, return the number of steps and the order of the steps. The main problem is handling the situation when N is exceeded.
Once the sum exceeds N, check if (sum-N) is even or odd.
If (sum-N) is even, then we must perform subtraction in steps of (sum-N)/2 to reach N.
Let us understand this case better with a suitable example.
For, N=8
1 2 3 4=10, which is more than 8.
Because 10-8=2 is an even number. So we're going to subtract in steps of 2/2, which is
step 1. So the order of steps will be -1 2 3 4 and minimum
The number of steps to reach N will be 4.
If (sum-N) is an odd number, first determine whether the number whose sum exceeds N in the previous step is even or odd.
If the previous step was odd, then perform a step by adding the next step number which will make our (sum-N) even and then perform the above steps to get the desired result.
For example, N=9
1 2 3 4=10, which is more than 9.
Because 10-9=1, this is an odd number. The next step is 5, which is an odd number, so we just do one step and add 5 to the sum to get 15, making (sum-N)=6. Performing the subtraction in step 3 will give you the sequence 1 2 -3 4 5, which is the desired output.
Assume that the previous step is an even number, in this case, we need to perform two steps, add the i-th step and subtract the (i 1) step, and get (sum- N) as an even number to obtain the desired sequence of steps.
For N=5
1 2 3=6, more than 5.
Since (sum-N) =1, we will consider the last step when su exceeds the number N. Since it is an even number, we will perform two steps, step 4 and step 5. Our task is to make (sum-N) even so, by adding in step 4 and subtracting in step 5 we can make (sum-N) even if 1 is subtracted from the sum. Since (sum-N) is equal to 0, we get N. Therefore, the sequence is 1 2 3 4 -5.
Example
The following is the C code of this method -
#include <iostream> #include <bits/stdc++.h> using namespace std; void minimumStep(int n){ vector <int> steps; // for storing the sequence int totalSum=0; int temp=0; if(n>=0){ // if n is positive then temp will store positive temp=1; } else { temp=-1; // n is negative then temp will store negative } n=abs(n); int step=0; for(step=1;totalSum<n;step++){ // for storing the steps till sum is not greater than n steps.push_back(temp*step); totalSum=totalSum+step; } if(totalSum>temp*n) { //when sum greater than n if(step%2==0) { //when step is even totalSum=totalSum-n; if((totalSum)%2!=0) { // when totalSum-n is odd steps.push_back(temp*step); //store the addition of next step steps.push_back((temp*-1)*(step+1)); // store the subtraction of next step totalSum--; //make totalSum even } int check=(totalSum)/2; check--; steps[check]=steps[check]*-1; } else { //when step is odd totalSum=totalSum-n; if((totalSum)%2!=0) { // when totalSum-n is odd steps.push_back(temp*step); //store the next addition value totalSum+=step; step++; } int check=(totalSum)/2; check--; steps[check]=steps[check]*-1; } } //print the minimum number of steps taken cout<<"The minimum number of steps : "<<steps.size()<<endl; //print the steps is stored in vector cout<<"Sequence of steps : "; for(int i=0;i<steps.size();i++){ cout<<steps[i]<<" "; } } int main(){ int m=17; minimumStep(m); return 0; }
Output
The minimum number of steps : 6 Sequence of steps : 1 -2 3 4 5 6
Time complexity: O(sqrt(N))
Space complexity: O(sqrt(N))
in conclusion
In this article we try to explain the method to find the minimum number of steps to reach N by adding or subtracting at each step and printing the sequence. I hope this article helps you learn this concept better.
The above is the detailed content of Use addition or subtraction to get the minimum number of steps for N at each step. For more information, please follow other related articles on the PHP Chinese website!

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

AI Hentai Generator
Generate AI Hentai for free.

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics



1. First open the online python editing page. 2. Then enter the program code in the program area. 3. Then click Run in the upper left corner, enter the first addend, press Enter, and enter the second addend. 4. Finally press Enter to get the sum of the two addends. This is simple python addition code.

In this problem, we only need to divide two integers without using multiplication, division and modulo operators. Although we can use addition, multiplication or bit operations. The problem statement states that we will get two integers x and y. Without using multiplication, division, or the modulo operator, we need to determine the quotient of x divided by y. Example Input: x=15, y=5 Output: 3 Input: x=10, y=4 Output: 2 Input: x=-20, y=3 Output: -6 Method Method 1 (use simple math) here In this method, we will use a simple mathematical algorithm. Here is a step-by-step explanation of the steps we will follow - we will keep subtracting the divisor (i.e. y) from the dividend (i.e. x) until x is greater than or equal to y. when y is greater than x

As a powerful relational database management system, Oracle database provides a wealth of computing operations to meet user needs. In daily database operations, subtraction operation is a common and important operation. It can help us realize the subtraction operation of data to obtain the results we need. This article will discuss in detail the techniques related to subtraction operations in Oracle database, and give specific code examples to help readers better understand and use this function. 1. Basic concepts of subtraction operations in Oracle data

Excel is an indispensable office software in our daily office, so for some people who are learning Excel for the first time, they will always encounter some small problems, such as how to do subtraction in Excel. Today I will talk to my friends Share this operation step. The specific operation steps are below. Friends, come and take a closer look! 1. First, open the Excel data sheet. If Excel wants to do subtraction, it uses formulas, and formulas are generally guided by the equal sign. Therefore, in the cells that need to be subtracted, first enter =, (as shown in red below) Circled portion shown). 2. Then, click on the cell where the minuend is located, and the name of the cell will be automatically added to the formula (as shown in the red circle in the figure below). 3

In-depth understanding of Python operators: addition, subtraction, multiplication, division and their meaning requires specific code examples. In the Python programming language, operators are one of the important tools for performing various mathematical operations. Among them, addition, subtraction, multiplication and division are the most common operators. This article will delve into the meaning of these operators and how to use them in Python. Addition Operator (+) The addition operator is used to add two numbers and can also be used to concatenate two strings. x=5y=3result

1. The basic beautification operation space of the chart is small, and the interfering display elements are removed. Elements that interfere with the data include background, grid lines, and legends, which can be deleted, beautified, and shadows softened. 2. Enter [PPT], [Open] chart, click [Chart], select [+], and uncheck [check] it, as shown in the figure. 3. [Right-click] to set the format of the data series, click [Fill], and check [No Fill]. Click [Data Column], click [Shadow] to remove the shadow, select [Outline], and color [Text] white. 4. Click [Scale], select [Scale Mark], adjust [Theme Type] None, [Color] white, as shown in the figure. 5. Delete the places that need to be deleted to make the table clearer. Don’t blindly add things when designing, do it appropriately.

Here we will see how to perform matrix addition and subtraction using a multi-threaded environment. pthread is used to execute multiple threads simultaneously in C or C++. There are two matrices A and B. The order of each matrix is (mxn). Each thread will get each row and perform addition or subtraction. So, for m rows, there are m different threads. Example#include<iostream>#include<pthread.h>#include<cstdlib>#include<cstdint>#defineCORE3#defineMAX3usingnamespacestd;i

Bitwise operators operate on bits (that is, operate on the binary value of the operand) Operator Description & Bitwise AND | Bitwise OR ^ Bitwise XOR << Left shift >> Right shift - Two's complement bitwise AND aba&b000010100111 Bitwise OR aba | b000011101111 Bitwise XOR aba^b000011101110 Example The following is a C program for addition and multiplication 2 with the help of bitwise operators - live demonstration #include<stdio.h>main(){ inta; printf
