Open In App

CSES Solutions – Weird Algorithm

Last Updated : 16 Mar, 2024
Improve
Improve
Like Article
Like
Save
Share
Report

Consider an algorithm that takes as input a positive integer N. If N is even, the algorithm divides it by two, and if N is odd, the algorithm multiplies it by three and adds one. The algorithm repeats this, until N is one. Your task is to simulate the execution of the algorithm for a given value of N.

Examples:

Input: N = 13
Output: 13 40 20 10 5 16 8 4 2 1
Explanation:

  • Initially N = 13 which is odd, so N becomes = 40
  • Now, N = 40, which is even, so N becomes = 20
  • Now, N = 20, which is even, so N becomes = 10
  • Now, N = 10, which is even, so N becomes = 5
  • Now, N = 5, which is odd, so N becomes = 16
  • Now, N = 16, which is even, so N becomes = 8
  • Now, N = 8, which is even, so N becomes = 4
  • Now, N = 4, which is even, so N becomes = 2
  • Now, N = 2, which is even, so N becomes = 1

Input: N = 5
Output: 5 16 8 4 1
Explanation:

  • Initially, N = 5, which is odd, so N becomes = 16
  • Now, N = 16, which is even, so N becomes = 8
  • Now, N = 8, which is even, so N becomes = 4
  • Now, N = 4, which is even, so N becomes = 2
  • Now, N = 2, which is even, so N becomes = 1

Approach: To solve the problem, follow the below idea:

The problem can be solved by running a while loop till N is not equal to 1. Inside the while loop, check if N is odd then multiply it by 3 and add 1 to it otherwise if N is even then divide it by 2.

Step-by-step algorithm:

  • Maintain a while loop till N is not equal to 1.
  • Inside the loop,
    • Print N.
    • If N is odd, N = N * 3 + 1
    • Else if N is even, N = N / 2

Below is the implementation of algorithm:

C++
#include <bits/stdc++.h>
#define ll long long int

using namespace std;
int main()
{
    ll N = 13;
    while (N != 1) {
        cout << N << " ";
        // If N is odd, then multiply it by 3 and add 1
        if (N & 1LL)
            N = N * 3 + 1;
        // If N is even, divide it by 2
        else
            N /= 2;
    }
    cout << 1;
}
Java
public class Main {
    public static void main(String[] args) {
        long N = 13;
        while (N != 1) {
            System.out.print(N + " ");
            // If N is odd, then multiply it by 3 and add 1
            if (N % 2 != 0)
                N = N * 3 + 1;
            // If N is even, divide it by 2
            else
                N /= 2;
        }
        System.out.print(1);
    }
}
C#
using System;

class GFG {
    public static void Main (string[] args) {
        // Declare and initialize N
        long N = 13;       
        // Continue the loop until N becomes 1
        while (N != 1) {
            // Print the current value of N
            Console.Write (N + " ");
            if ((N & 1) == 1)
                N = N * 3 + 1;
            // If N is even divide it by 2
            else
                N /= 2;
        }       
        // Print the final value of N (1)
        Console.Write (1);
    }
}
Javascript
let N = 13;
let output = '';

while (N !== 1) {
    output += N + ' ';
    
    // If N is odd, then multiply it by 3 and add 1
    if (N & 1) {
        N = N * 3 + 1;
    } 
    // If N is even, divide it by 2
    else {
        N /= 2;
    }
}

output += '1';
console.log(output);

// Note: output variable is used to display result in a single line.
Python3
def main():
    N = 13
    while N != 1:
        print(N, end=" ")  
        # If N is odd, then multiply it by 3 and add 1
        if N % 2 != 0:
            N = N * 3 + 1
        # If N is even, divide it by 2
        else:
            N //= 2
    print(1)  # Print the final value of 1


if __name__ == "__main__":
    main()

Output
13 40 20 10 5 16 8 4 2 1





Time Complexity: O(N)
Auxiliary Space: O(1)



Like Article
Suggest improvement
Previous
Next
Share your thoughts in the comments

Similar Reads