0
Can someone solve this ?? Please give me the psuodocode....
Given a number N and an option opt=1 or 2 , find the result as per below rules, If opt=1 Result = N-(N-1)+(N-2)-(N-3)+(N-4).. till 1 If opt=2 Result = N+(N-1)-(N-2)+(N-3)-(N-4).. till 1 e.g. if N=6 and opt=1 Result = 6 - 5 + 4 -3 +2- 1=3
11 Antworten
+ 2
Ashutosh Raturi yaa! Yw
Corrected that 😅😆
Thanks for the appreciation!
+ 9
Can you show your attempt ?
+ 9
if n is odd then
opt1= (1+1+1... n/2 times) + 1(last term)
= (n/2)+1
opt2= n + (1+1+1... n/2 times)
= n + (n/2)
else
opt1=(1+1+1... n/2 times)
= n/2
opt2= n + (1+1+1... (n-1)/2 times + 1(last term))
= n + ((n-1)/2) +1
Every division is integer division.
Maybe you need to make formula for either even or odd only, like if you made formula for even n then for odd n separate first term and use formula for (n-1) which is even(add or substract, opt1 or opt2 for a opt you have to decide).
+ 1
Ashutosh Raturi will java work?
I can show you the java version of this!
Or c++? or Ruby? Or C?
+ 1
Ashutosh Raturi
import java.util.*;
class HelloWorld {
public static void main(String[] args) {
int N = 6;
int opt = 1;
int s = N;
double sign;
for (int i = 0; i < N-1; i++) {
sign = opt==1 ? Math.pow(-1, i + 1) : Math.pow(-1, i);
s += sign*(N - i - 1);
}
System.out.println(s);
}
}
+ 1
Opt2 is just opt1 with N-1 plus N.
0
N = 6
opt = 1
s = N
for i in range(N-1):
sign = (-1)**(i+1) if opt==1 else (-1)**i
s+=sign*(N-i-1)
print(s)
0
Namit Jain can you explain this ? Not familier with python syntax...
0
Namit Jain go on with java
0
Namit Jain Thanks a lot mate ❤...btw double astrisk dont work in java for exponential operations.... Math.pow( ) can be used for it..Thumbs up for your logic👍
0
😃😃