CSES Solutions - Weird Algorithm Last Updated : 16 Mar, 2024 Comments Improve Suggest changes Like Article Like 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 = 13Output: 13 40 20 10 5 16 8 4 2 1Explanation: Initially N = 13 which is odd, so N becomes = 40Now, N = 40, which is even, so N becomes = 20Now, N = 20, which is even, so N becomes = 10Now, N = 10, which is even, so N becomes = 5Now, N = 5, which is odd, so N becomes = 16Now, N = 16, which is even, so N becomes = 8Now, N = 8, which is even, so N becomes = 4Now, N = 4, which is even, so N becomes = 2Now, N = 2, which is even, so N becomes = 1Input: N = 5Output: 5 16 8 4 1Explanation: Initially, N = 5, which is odd, so N becomes = 16Now, N = 16, which is even, so N becomes = 8Now, N = 8, which is even, so N becomes = 4Now, N = 4, which is even, so N becomes = 2Now, N = 2, which is even, so N becomes = 1Approach: 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 + 1Else if N is even, N = N / 2Below 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() Output13 40 20 10 5 16 8 4 2 1Time Complexity: O(N)Auxiliary Space: O(1) Comment More infoAdvertise with us Next Article Non-linear Components A aayushbvc8m Follow Improve Article Tags : Competitive Programming CSES Problems Similar Reads Non-linear Components In electrical circuits, Non-linear Components are electronic devices that need an external power source to operate actively. Non-Linear Components are those that are changed with respect to the voltage and current. Elements that do not follow ohm's law are called Non-linear Components. Non-linear Co 11 min read Spring Boot Tutorial Spring Boot is a Java framework that makes it easier to create and run Java applications. It simplifies the configuration and setup process, allowing developers to focus more on writing code for their applications. This Spring Boot Tutorial is a comprehensive guide that covers both basic and advance 10 min read Dynamic Programming or DP Dynamic Programming is an algorithmic technique with the following properties.It is mainly an optimization over plain recursion. Wherever we see a recursive solution that has repeated calls for the same inputs, we can optimize it using Dynamic Programming. The idea is to simply store the results of 3 min read Class Diagram | Unified Modeling Language (UML) A UML class diagram is a visual tool that represents the structure of a system by showing its classes, attributes, methods, and the relationships between them. It helps everyone involved in a projectâlike developers and designersâunderstand how the system is organized and how its components interact 12 min read Python Variables In Python, variables are used to store data that can be referenced and manipulated during program execution. A variable is essentially a name that is assigned to a value. Unlike many other programming languages, Python variables do not require explicit declaration of type. The type of the variable i 6 min read Spring Boot Interview Questions and Answers Spring Boot is a Java-based framework used to develop stand-alone, production-ready applications with minimal configuration. Introduced by Pivotal in 2014, it simplifies the development of Spring applications by offering embedded servers, auto-configuration, and fast startup. Many top companies, inc 15+ min read Backpropagation in Neural Network Back Propagation is also known as "Backward Propagation of Errors" is a method used to train neural network . Its goal is to reduce the difference between the modelâs predicted output and the actual output by adjusting the weights and biases in the network.It works iteratively to adjust weights and 9 min read Polymorphism in Java Polymorphism in Java is one of the core concepts in object-oriented programming (OOP) that allows objects to behave differently based on their specific class type. The word polymorphism means having many forms, and it comes from the Greek words poly (many) and morph (forms), this means one entity ca 7 min read What is an Operating System? An Operating System is a System software that manages all the resources of the computing device. Acts as an interface between the software and different parts of the computer or the computer hardware. Manages the overall resources and operations of the computer. Controls and monitors the execution o 5 min read CTE in SQL In SQL, a Common Table Expression (CTE) is an essential tool for simplifying complex queries and making them more readable. By defining temporary result sets that can be referenced multiple times, a CTE in SQL allows developers to break down complicated logic into manageable parts. CTEs help with hi 6 min read Like