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

10th Sep 2020, 6:48 PM
Ashutosh Raturi
Ashutosh Raturi - avatar
11 odpowiedzi
+ 2
Ashutosh Raturi yaa! Yw Corrected that 😅😆 Thanks for the appreciation!
10th Sep 2020, 7:34 PM
Namit Jain
Namit Jain - avatar
+ 9
Can you show your attempt ?
10th Sep 2020, 6:58 PM
Nilesh
Nilesh - avatar
+ 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).
12th Sep 2020, 5:46 PM
Gaurav Agrawal
Gaurav Agrawal - avatar
+ 1
Ashutosh Raturi will java work? I can show you the java version of this! Or c++? or Ruby? Or C?
10th Sep 2020, 7:20 PM
Namit Jain
Namit Jain - avatar
+ 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); } }
10th Sep 2020, 7:25 PM
Namit Jain
Namit Jain - avatar
+ 1
Opt2 is just opt1 with N-1 plus N.
12th Sep 2020, 10:18 AM
Aekries
Aekries - avatar
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)
10th Sep 2020, 7:02 PM
Namit Jain
Namit Jain - avatar
0
Namit Jain can you explain this ? Not familier with python syntax...
10th Sep 2020, 7:06 PM
Ashutosh Raturi
Ashutosh Raturi - avatar
0
Namit Jain go on with java
10th Sep 2020, 7:21 PM
Ashutosh Raturi
Ashutosh Raturi - avatar
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👍
10th Sep 2020, 7:32 PM
Ashutosh Raturi
Ashutosh Raturi - avatar
0
😃😃
10th Sep 2020, 7:35 PM
Ashutosh Raturi
Ashutosh Raturi - avatar