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 CSES Solutions - Weird Algorithm A aayushbvc8m Follow Improve Article Tags : Mathematical Competitive Programming DSA CSES Problems Practice Tags : Mathematical Similar Reads Linear Search Algorithm Given an array, arr of n integers, and an integer element x, find whether element x is present in the array. Return the index of the first occurrence of x in the array, or -1 if it doesn't exist.Input: arr[] = [1, 2, 3, 4], x = 3Output: 2Explanation: There is one test case with array as [1, 2, 3 4] 9 min read 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 Boothâs Multiplication Algorithm Booth's algorithm is a multiplication algorithm that multiplies two signed binary numbers in 2's complement notation. Booth used desk calculators that were faster at shifting than adding and created the algorithm to increase their speed. Boothâs algorithm is of interest in the study of computer arch 15 min read Like