In this article, we have a line and its coordinates are given. Our task is to check if this line passes through the origin or not. The basic two-point form of a straight line is given by: $$ \frac{y - y_1}{y_2 - y_1} = \frac{x - x_1}{x_2 - x_1} + c $$ For the above line to pass through the origin, put x = 0, y = 0, and c = 0. The formula for a straight line to pass through the origin can be given by: $$ y_1(x_2 - x_1) = x_1(y_2 - y_1) $$ Here are some ... Read More
In this article, we will learn how to find the square root of a number up to a given precision by using binary search algorithm and implement it in C++. Before dive into the concept, make sure that you have a basic understanding of binary search algorithm. Square Root of a Number Upto a Given Precision In this problem, you are given a positive floating point number N and a positive integer P. The task is to find the square root of N up to P decimal places using binary search. Scenario 1 Input: N = ... Read More
In this article, we will explain a beginner level problem that involves finding answers for questions by checking for vowels in the question text. We will implement a C++ program that solves this problem. Let's break down the problem statement below. Find Answers by Vowel Checking Amal and Bimal are playing a game. Amal will ask any questions whose answers will be either "Yes" or "No". If the question’s last letter is a vowel, then Bimal will answer "Yes" otherwise "No". You are given a string containing the question that Amal asks. Your task is to determine the answer ... Read More
In this article, our task is to check if we can represent a given number as the sum of two non-zero powers of 2 in C++. For this, we will check whether the given number N can be represented as (2^x + 2^y) where x, y > 0. Here are some example scenarios: Scenario 1 Input:10 Output: Can be represented Explanation: 10 = 8 + 2 10 = 2^3 + 2^1 => 10 can be represented as 2x + 2y Scenario 2 Input:3 Output: Can be represented Explanation: 10 = 2 + ... Read More
A string is a class in Java that stores a series of characters enclosed within double quotes, and a continuous sequence of characters within that string is known as a substring. Those characters are string-type objects. In this article, we will learn how to write Java programs to check if a string contains a substring or not. Java Program to Check if a String Contains a Substring We can check whether the given string contains a substring or not in the following ways: Using indexOf() Method Using contains() Method ... Read More
In this article, we will learn how to count all distinct binary strings of length n such that no two 1's appear consecutively. We'll explore this problem using both recursive and dynamic programming approaches in C and C++. What is a Binary String? A binary string is a sequence of characters that contains only '0' and '1'. It represents information in base-2 format. For example, "0101" is a binary string of length 4. We are given a positive integer n, and our task is to count all possible distinct binary strings of length n that do not contain ... Read More
A string is a collection of characters. In Java, it is represented by the String class, and it accepts NULL values. Checking for Null or Empty in Java In this article, we will learn if a string is null or empty in Java. The following are the ways to check if a string is null or empty in Java: Using isEmpty() Method Using length() Method Using isBlank() Method Using isEmpty() Method The isEmpty() method of the String class checks if a string is empty. It returns TRUE if ... Read More
We are given a string with both vowels and consonants. Our task is to rearrange it so that vowels and consonants appear alternately, while keeping their original order within their groups. This rearrangement is only possible if the number of vowels and consonants is equal, or their difference is exactly one. If multiple valid arrangements are possible, we return the one that is lexicographically smaller. Let's understand this with a few example scenarios. Scenario 1 Input: "objective" Output: "bojecitev" Explanation: Vowels = [o, e, i, e], Consonants = [b, j, c, t, v] Consonants are more by 1 -> valid ... Read More
The Fibonacci sequence starts from 0 and 1, and each number is the sum of the previous two. In this problem, we are given a number n, and we need to print the first n numbers from the Fibonacci series, but only the numbers at alternate positions (like 0th, 2nd, 4th, and so on). Let's look at some example scenarios to understand it clearly: Scenario 1 Input: n = 7 Output: 0 1 3 8 Explanation: The first 7 Fibonacci numbers are 0 1 1 2 3 5 8. If we pick alternate numbers (index 0, 2, ... Read More
In C++, when we pass multiple arguments to a function, a common question arises, in what order are these arguments evaluated? Is it from left to right, right to left, or does it depend on the compiler? In this article, we will learn how function parameter evaluation works in C++, why the order of evaluation is important, and how it can vary across different compilers. Is the Order of Evaluation Defined in C++? The C++ standard does not guarantee a fixed order of evaluation for function arguments. This means compilers are free to evaluate arguments from left to ... Read More