Fizz-Buzz Problem Solution In Hindi Using 4 Approaches

Fizz-Buzz एक बहुत ही सरल प्रोग्रामिंग कार्य है, जिसे सॉफ़्टवेयर डेवलपर जॉब इंटरव्यू में पूछा जाता है।

Fizz-Buzz एक लोकप्रिय समस्या है जो बुनियादी प्रोग्रामिंग अवधारणाओं जैसे कि if-else, loops, String Operation, Mathematical Operation, ऑप्टिमाइज़ेशन आदि को और अधिक अच्छे से सिखने मैं मदद करती है।

Let’s understand the Fizz-Buzz problem!

Fizz buzz Leetcode समस्या बहुत सरल है, आपको एक प्रोग्राम लिखने की आवश्यकता है जो “fizz” लौटाता है यदि संख्या 3 से विभाज्य है, “buzz” वापस करें यदि 5 से विभाज्य है, और “fizzbuzz” वापस करें यदि संख्या दोनों 3 और 5 से विभाज्य है.

यदि संख्या 3 या 5 से विभाज्य नहीं है, तो केवल संख्या ही लौटानी चाहिए।

  1. output[i] = “FizzBuzz” if i 3 और 5 दोनों से विभाज्य है.
  2. output[i] = “Fizz” if i 3 से विभाज्य है.
  3. output[i] = “Buzz” if i 5 से विभाज्य है.
  4. output[i] = “i” if उपरोक्त में से कोई भी Condition सत्य नहीं है.

Important Note: समाधानों पर आगे बढ़ने से पहले, हम अनुशंसा करते हैं कि इस समस्या को कम से कम 15 या 30 मिनट के लिए Notebook पर Solve करें।

Example:

Input: n = 15

Output: [“1”, “2”, “Fizz”, “4”, “Buzz”, “Fizz”, “7”, “8”, “Fizz”, “Buzz”, “11”, “Fizz”, “13”, “14”, “FizzBuzz”]

Learn Two Sum Leetcode Problem Solution Click Here

Fizz Buzz Leetcode problem Solution Approach

  1. Solution approach 1: Using modulus operation
  2. Solution approach 2: Without using modulus operations
  3. Solution approach 3: Using string concatenation
  4. Solution approach 4: Using hash table

Fizz-Buzz Solution approach 1: Using modulus operation

सबसे बुनियादी समाधान एक लूप को 1 से n तक चलाना और मॉड्यूलस ऑपरेशन (modulus operation) का उपयोग करना है। इस लूप में, हम यह जांचने के लिए Conditional Statements का उपयोग करते हैं कि प्रत्येक पूर्णांक 3 या 5 या दोनों से विभाज्य है या नहीं। जब Conditions में से एक सही हो जाता है, तो हम Output[] स्ट्रिंग में ith इंडेक्स पर उचित स्ट्रिंग Value को Store करते हैं। एक बार लूप पूरा हो जाने के बाद, हम Output[] स्ट्रिंग को Return करते हैं।

Solution Steps:

  1. हम एक N Size कि खाली Output[] स्ट्रिंग को Define करते हैं।
  2. हम 1 से n तक लूप चलाते हैं और 3 या 5 या दोनों से विभाज्यता (Divisibility) की जांच करते हैं।
  3. यदि i, 3 और 5 दोनों से विभाज्य है, हम आउटपुट स्ट्रिंग के ith इंडेक्स में स्ट्रिंग “FizzBuzz” जोड़ते हैं।
  4. अन्यथा, यदि पूर्णांक i, 3 से विभाज्य है, तो हम “Fizz” जोड़ते हैं।
  5. अन्यथा, यदि पूर्णांक i, 5 से विभाज्य है, तो हम “Buzz” जोड़ते हैं।
  6. अन्यथा, हम पूर्णांक i का स्ट्रिंग मान जोड़ते हैं।

FizzBuzz Implementation code C++

// Function to implement the FizzBuzz Problem
string* fizzBuzz(int n){
	// Initialize an array of strings to store the output
	string *output = new string[n];
	
	// Iterate over the range 1 to n
	for(int i = 1; i <= n; i++){
		// Check if the current number is divisible by 3 and 5
		if((i%3 == 0)&&(i%5 == 0))
			output[i-1] = "FizzBuzz";
			
		// Check if the current number is divisible by 3
		else if(i%3 == 0)
			output[i-1] = "Fizz";
			
		// Check if the current number is divisible by 5
		else if(i%5 == 0)
			output[i-1] = "Buzz";
		
		// If the number is not divisible by 3 or 5, store the number itself
		else
			output[i-1] = to_string(i);
	}
	return output;
}
C++

FizzBuzz Implementation code Python

def fizzBuzz(n);
	#Initialize an empty list to store the output
	
	output = []
	
	#Iterate over the range 1 to n
	
	for i in range (1, n+1):
		# check if the current number is divisible by 3 and 5
		if(i%3 == 0) and (i%5 == 0):
			output.append("FizzBuzz");
		
		# check if the current number is divisible by 3	
		if(i%3) == 0:
			output.append("Fizz");
			
		# check if the current number is divisible by 5	
		if(i%5) == 0:
			output.append("Buzz");
			
		# if the current number is not divisible by 3 and 5, store the number itself
		
		else:
			output.append(str(i));
	retur output;
Python

FizzBuzz Implementation code Java

public class fizzBuzz{
	// Function to implement the FizzBuzz Problem Solution
	public List<String> fizzBuzz(int n){
		// Initialize an empty list to store the output
		List<String> output = new ArrayList<>();
		
		// Iterate over the range 1 to n
		for(int i = 1; i <= n; i++){
			// Check if the current number is divisible by 3 and 5
			if(i%3 == 0 && i%5 == 0)
				output.add("FizzBuzz");
			
			// Check if the current number is divisible by 3
			if(i%3 == 0)
				output.add("Fizz");
				
			// Check if the current number is divisible by 5
			if(i%5 == 0)
				output.add("Buzz");
			
			# if the number is not divisible by 3 and 5, store the number itself
			else
				output.add(String.valueOf(i));
		}
		return output;
	}
}
Java

Solution Analysis

error: Content is protected!! You are not allowed to copy.