0, 0, 2, 1, 4, 2, 6, 3, 8, 4, 10, 5, 12, 6, 14, 7, 16, 8 This series is a mixture of 2 series all the odd terms in this series form even numbers in ascending order and every even terms is derived from the previous  term using the formula (x/2)

Consider the below series :

0,0,2,1,4,2,6,3,8,4,10,5,12,6,14,7,16,8

  • This series is a mixture of 2 series all the odd terms in this series form even numbers in ascending order
  • Every even terms is derived from the previous  term using the formula (x/2)

Write a program to find the nth term in this series.

  1. The value n in a positive integer that should be read from STDIN the nth term that is calculated by the program should be written to STDOUT.
  2. Other than the value of the nth term no other characters /strings or message should be written to STDOUT.

For example

if n=10,the 10 th term in the series is to be derived from the 9th term in the series. The 9th term is 8 so the 10th term is (8/2)=4. Only the value 4 should be printed to STDOUT.

You can assume that the n will not exceed 20,000.

53 comments on “0, 0, 2, 1, 4, 2, 6, 3, 8, 4, 10, 5, 12, 6, 14, 7, 16, 8 This series is a mixture of 2 series all the odd terms in this series form even numbers in ascending order and every even terms is derived from the previous  term using the formula (x/2)”


  • GRAHEETH

    #include
    using namespace std;
    int main()
    {
    int n;
    cin>>n;
    int ans;
    if(n%2==0)
    {
    ans=(n/2-1);

    }
    else
    {
    ans=(n/2*2);

    }
    cout<<ans;
    }


  • 1DS18ME050

    //will this work

    #include
    using namespace std;

    int main(){
    int n;
    cin>>n;
    if(n%2==0 && n>0){
    cout<<(n-2)/2<<endl;
    }
    else{
    cout<<n-1;
    }
    }


  • sagar kanti

    python code
    position= int(input())
    if position % 2 !=0:
    print(position-1)
    else:
    print((position//2)-1)


  • Aditya

    n = int(input(“Enter a number : “))
    print(‘0 0’)
    i = 2
    for i in range(2,n+1):
    if(i%2==0):
    print(i,” “)
    else:
    print((i-1)/2,” “)


  • Ansh

    #include

    void even(int n)
    {
    int y = n ;
    printf(“%d”,y);
    }

    void odd(int n)
    {
    int x = n ;
    printf(“%d”, x);
    }

    void main()
    {
    int n;
    scanf(“%d”,&n);

    if(n%2 != 0)
    {
    odd(n/2);
    }
    else
    {
    even (n);
    }
    }

    even this code runs perfectly for the given problem


  • SARAN

    #include
    using namespace std;
    int main()
    {
    int n,r;
    cin>>n;
    if(n%2==0)
    {
    r=(n-2)/2;
    }
    else
    {
    r=n-1;
    }
    cout<<r;
    }


  • Nithish

    *** Simply by understanding the series, we can solve these type of problems without any logic ***
    ********************* JAVA code **********************
    import java.util.*;
    public class Main
    {
    public static void main(String[] args) {
    Scanner sc = new Scanner(System.in);
    int N = sc.nextInt();
    if(N==1 || N==2) {
    System.out.println(“0”);
    }
    else if(N%2 == 0) {
    System.out.println((N-2)/2);
    }
    else {
    System.out.println(N-1);
    }
    }
    }


  • Sanjai

    num=int(input(“enter the number:”))
    if num%2==0:
    z=num//2
    x=z-1
    print(x)
    else:
    y=num-1
    print(y)


  • ASHWINI

    n=int(input())
    a=0
    if (n==1) or (n==2):
    print(a)
    elif(n % 2 ==0):
    print((n-1)//2)
    else:
    print(n-1)