Suppose we have a binary string S with n bits. There are no redundant leading zeroes. We can perform two different operations on S −Swap any pair of adjacent bitsReplace all "11" with "1"Let val(S) is decimal representation of S. We have to find minimum correct string, where correct string A is less than another correct string 'B' when val(A) < val(B)So, if the input is like S = "1001", then the output will be 100, because we can perform the operation like "1001" -> "1010" -> "1100" -> "100".StepsTo solve this, we will follow these steps −n := size ... Read More
Suppose we have a matrix of size n x m. Cells are either 'W' as white or 'B' as black. Some square inside the table with odd length was painted black. We have to find the center of this square.So, if the input is likeWWBBBWWWBBBWWWBBBWWWWWWWWWWWWWthen the output will be (3, 1).StepsTo solve this, we will follow these steps −n := row count of matrix m := column count of matrix cnt := 0 X := 0 Y := 0 for initialize i := 0, when i < n, update (increase i by 1), do: for initialize j := 0, ... Read More
In this article, we will understand how to determine the unicode code point at a given index. Each character is represented by a unicode code point. A code point is an integer value that uniquely identifies the given character. Unicode characters can be encoded using different encodings, like UTF-8 or UTF-16.Below is a demonstration of the same −Suppose our input is −Input String: Java Program Index value: 5The desired output would be −Unicode Point: 80AlgorithmStep 1 - START Step 2 - Declare a string value namely input_string and two integer values namely index and result Step 3 - Define the ... Read More
Suppose we have two numbers n and k. We have to find smallest integer x which is greater than n and divisible by k.So, if the input is like n = 5; k = 3, then the output will be 6.StepsTo solve this, we will follow these steps −return n + k - (n mod k)ExampleLet us see the following implementation to get better understanding −#include using namespace std; int solve(int n, int k){ return n + k - n % k; } int main(){ int n = 5; int k = 3; cout
Suppose we have two arrays X and Y of same size. There are stone piles with X[i] number of stones on ith index on the first day and on the second day ith index has Y[i] number of stones. On the first day many members have come. Either they do nothing or they add few stones to some pile or they swap few stones from one pile to another. We have to check whether Y is valid from X or not.So, if the input is like X = [1, 2, 3, 4, 5]; Y = [2, 1, 4, 3, 5], ... Read More
Suppose we have an array A with n elements. There are n students in a school and each of them has exactly k votes and all votes should be used. There are two parties. The A[i] represents ith student has given A[i] amount of votes to first party and this implies the second party will get k- A[i] number of votes. The second party wants to set k in such a way that they win. What will be the minimum possible value of k.So, if the input is like A = [2, 2, 3, 2, 2], then the output will ... Read More
Suppose we have a number n. We have to check whether the number is triangular number or not. As we know, if n dots (or balls) can be arranged in layers to form a equilateral triangle then n is a triangular number.So, if the input is like n = 10, then the output will be True.StepsTo solve this, we will follow these steps −for initialize i := 1, when i
Suppose we have a string S with n character. On a text editor, there is a strange rule. The word corrector of this text editor works in such a way that as long as there are two consecutive vowels in the word, it deletes the first vowel in a word. If there are no two consecutive vowels in the word, it is considered to be correct. We have to find corrected word from S. Here vowels are 'a', 'e', 'i' 'o', 'u' and 'y'.So, if the input is like S = "poor", then the output will be "por".StepsTo solve this, ... Read More
Suppose we have two coordinates (x1, y1) and (x2, y2). A robot is at the point (x1, y1) and wants to go to the point (x2, y2). In a single step, the robot can move towards one cell to its 8 adjacent coordinates. We have to find minimal number of steps needed to reach the final position.So, if the input is like x1 = 3; y1 = 4; x2 = 6; y2 = 1;, then the output will be 3, becauseStepsTo solve this, we will follow these steps −return maximum of |x2 - x1| and |y2 - y1|ExampleLet us see ... Read More
In this article, we will understand how to sort a string. String is a datatype that contains one or more characters and is enclosed in double quotes(“ ”). Strings are a sequence of charactersBelow is a demonstration of the same −Suppose our input is −Input string: javaprogramThe desired output would be −String after sorting is: [a, a, a, g, j, m, o, p, r, r, v]AlgorithmStep 1 - START Step 2 - Declare a string value namely input_string, a character array charArray, char value name temp and an int value namely string_size. Step 3 - Define the values. Step 4 ... Read More