CSES Solutions - Weird Algorithm Last Updated : 16 Mar, 2024 Summarize Comments Improve Suggest changes Share 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 CSES Problem Set Solutions A aayushbvc8m Follow Improve Article Tags : Competitive Programming CSES Problems Similar Reads CSES Solutions - Number Spiral A number spiral is an infinite grid whose upper-left square has the number 1. The task is to find the number in row Y and column X. Here are the first five layers of the spiral: Examples: Input:Â Y = 2, X = 3Output:Â 8Explanation: The 2nd row, 3rd column contains 8. Input:Â Y = 4, X = 2Output:Â 15Explan 9 min read CSES Solutions - Apple Division There are N apples with known weights given as arr[]. Your task is to divide the apples into two groups so that the difference between the weights of the groups is minimal. Examples: Input: N = 5, arr[] = {3, 2, 7, 4, 1}Output: 1Explanation: Group 1 has weights 2, 3 and 4 (total weight 9), and group 5 min read CSES Solutions - Sum of Two values You are given an array arr[] of N integers, and your task is to find two different indices whose sum is X. Examples: Input: N = 4, X = 8, arr[] = {2, 7, 5, 1}Output: 2 4Explanation: The sum of arr[2] and arr[4] (1-based indexing) is 7 + 1 = 8 Input: N = 5, X = 6, arr[] = {1, 2, 3, 4, 5}Output: 1 5Ex 9 min read CSES Problem Set Solutions In this article, we have compiled comprehensive, high-quality tutorials on the CSES Problem Set Solutions to assist you in understanding the problem set for learning algorithmic programming. What is CSES Problem Set?CSES Problem Set is a collection of competitive programming tasks hosted on the CSES 8 min read CSES Solutions - Missing Number Given an array arr[] of size N - 1 containing numbers between 1, 2... N, except one, the task is to find the missing number.Examples:Input: N = 5, arr[] = {2, 3, 1, 5}Output: 4Explanation: arr[] contains all numbers from 1 to 5 except 4, therefore the answer is 4.Input: N = 6, arr[] = {4, 5, 1, 2, 3 7 min read Find 'N' number of solutions with the given inequality equations Find the value of a1, a2, a3, ....an such that the following two conditions are satisfied. a_1^2 + a_2^2 + a_3^2 + ....+ a_n^2 \geq X a_1 + a_2 + a_3 + ....+ a_n \leq Y Print the value of a1, a2, ..., an and "No solution" otherwise. Note: There maybe a several solutions, print any of them. Examples: 5 min read Like