Found 33676 Articles for Programming

C++ code to count local extrema of given array

Riya Kumari
Updated on 05-Jan-2024 16:17:29

862 Views

Extrema are such numbers which are either minima or maxima. In other words, it is a number or element which is either greater than or less than both of its adjacent values. Suppose we have an array A with n elements. An element of this array A[i] is called a local minimum if and only if it is strictly less than both of its neighbours. Also if it is strictly greater than its neighbours it will be local maximum. For A[0] and A[n-1] as there are only one neighbour they are not maxima or minima. We have to find the ... Read More

C++ code to find palindrome string whose substring is S

Arnab Chakraborty
Updated on 29-Mar-2022 12:01:05

254 Views

Suppose we have a string S with n letters. We have to find another string T, such that T is palindrome and S is subsequence of T.So, if the input is like S = "ab", then the output will be "aabaa" (other answers are also available)StepsTo solve this, we will follow these steps −res := S reverse the array S res := res + S return resExampleLet us see the following implementation to get better understanding −#include using namespace std; string solve(string S){    string res = S;    reverse(S.begin(), S.end());    res += S;    return res; } ... Read More

C++ code to find reduced direction string of robot movement

Arnab Chakraborty
Updated on 29-Mar-2022 11:59:08

274 Views

Suppose we have a string S with n letters. The letters are either 'R' or 'U'. On a 2D plane, a robot can go right or up. When it is 'R' it moves right and when it is 'U' it moves up. However the string is too large, we want to make the string smaller. A pair like "RU" or "UR" will be replaced as diagonal move "D". We have to find the length of the final updated reduced string.So, if the input is like S = "RUURU", then the output will be 5, because the string will be "DUD"StepsTo ... Read More

C++ code to get shortest distance from circular stations

Arnab Chakraborty
Updated on 29-Mar-2022 11:56:52

467 Views

Suppose we have two numbers s and t, and another array D with n elements. The circle line of the Dreamland subway has n different stations. We know the distances between all pairs of neighboring stations: D[i] is the distance between station i and i+1, and D[n-1] is the distance between (n-1) and 0th station. We have to find shortest distance from s to t.So, if the input is like s = 1; t = 3; D = [2, 3, 4, 9], then the output will be 5.StepsTo solve this, we will follow these steps −n := size of D ... Read More

Java program to print even length words

AmitDiwan
Updated on 09-Aug-2024 23:58:12

2K+ Views

In this article, we will understand how to print even-length words. The string is a datatype that contains one or more characters and is enclosed in double quotes (“ ”). Char is a datatype that contains an alphabets an integer or a special character. Problem Statement Write a Java program to print even-length words. Below is a demonstration of the same − Input Input string: Java Programming are cool Output  The words with even lengths are: Java cool Approaches to print even length words Below are the different approaches to print even length words − Using ... Read More

C++ code to check given flag is stripped or not

Arnab Chakraborty
Updated on 29-Mar-2022 11:53:56

292 Views

Suppose we have a matrix of size n x m. Each cell will hold one value from 0 to 9. There is a flag should be striped: each horizontal row of the flag should contain squares of the same color, and the colors of the adjacent horizontal rows should be different. We have to check given matrix is valid flag or not.So, if the input is like000111333StepsTo solve this, we will follow these steps −n := row count of matrix m := column count of matrix l := 'm' res := 1 for initialize i := 0, when i < ... Read More

C++ code to find minimum correct string from given binary string

Arnab Chakraborty
Updated on 29-Mar-2022 11:49:30

160 Views

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

C++ code to find answers by vowel checking

Farhan Muhamed
Updated on 04-Aug-2025 19:02:44

248 Views

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

C++ code to find center of inner box

Arnab Chakraborty
Updated on 29-Mar-2022 11:42:12

397 Views

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

C++ code to find greater number whose factor is k

Arnab Chakraborty
Updated on 29-Mar-2022 11:37:21

194 Views

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

Advertisements