Found 34494 Articles for Programming

K-th Smallest Element in an Unsorted Array using Priority Queue

Prabhdeep Singh
Updated on 24-Aug-2023 15:53:20

252 Views

A priority queue is a heap-based data structure, stores elements in such a way that the greatest or smallest element always be present on the top. We are given an array that is unsorted and we have to find the Kth smallest element from it using the Priority Queue. Here, the element k will be given and it must be in the range from 1 to the size of the given array. Example Let's understand the problem with the help of an input-output example − Input int arr[] = {1, 5, 6, 2, 3, 6, 7, 9, 12, 15, 0, ... Read More

Find the winner of game of repeatedly removing the first character to empty given string

Prabhdeep Singh
Updated on 24-Aug-2023 15:51:29

56 Views

In this game, we are given an array of strings of length N. Each string consists of the digits 1 to N only. The game starts with the first person and removes the first character of the 0th index, then the removed character from the string digit number player will go for the next move. Each player with index y will remove the digit from the string from index y-1 and then the remove digit number player will move next. When any player is not able to remove the character will win the game. Example Let's understand the problem with ... Read More

Java Program for Queries for rotation and Kth character of the given string in constant time

Shubham Vora
Updated on 24-Aug-2023 16:53:07

50 Views

In this problem, programmers require to execute the queries on the string. Also, need to rotate the string and print the characters of the updated string. The best approach to solve the problem is that keep updating the index value and access string characters when we need to print the character. Problem statement – We have given the string alpha and the array containing the pair of numbers named ‘que’. The task is that perform the queries given in the array onto the string alpha. Follow the below query operation rules. (1, a) – Make total left rotations of ... Read More

Java Program for Check if a string can be formed from another string by at most X circular clockwise shifts

Shubham Vora
Updated on 24-Aug-2023 16:45:26

53 Views

In this problem, we need to convert one string to another by performing at most x circular shift operations on each character of the first string. The naïve approach to solving the problem is to rotate each character of the alpha1 string for x times, and we check if it matches the character of the alpha2 string, which is at the same index. The second approach is to solve the problem by finding the circular difference between characters at the same index. Problem statement – We have given a positive integer X. Also, we have given a string alpha1 ... Read More

Encode given String by inserting in Matrix column-wise and printing it row-wise

Shubham Vora
Updated on 24-Aug-2023 16:42:49

84 Views

In this problem, we will encode the string by inserting it in the matrix in a column-wise manner and printing the string in a row-wise manner. The naïve approach to solve the problem is to create a matrix, fill the matrix top-bottom manner, and print the string row-wise. The second solution is to use the vector to store the string and print each vector value individually. Here, we will learn both approaches to solve the problem. Problem statement – We have given string str of length N. Also, we have given the positive integer rows. The task is to encode ... Read More

Difference between concatenation of strings using (str += s) and (str = str + s)

Shubham Vora
Updated on 24-Aug-2023 16:40:30

77 Views

In this tutorial, we will learn the difference between the concatenation of string using the ‘+=’ or ‘+’ operator. Both operators are used to merge strings, but we learn some differences below with examples. What is Addition Assignment (+=) operator? The addition assignment (+=) operator concatenates the two strings. It takes two operands. The left operand is the original string, and the right operand is a string we need to concatenate with the original string. Generally, we use the ‘+=’ operator when we require to concatenate only two strings. Syntax Users can follow the syntax below to use the addition ... Read More

Decrypt the encoded string with help of Matrix as per given encryption decryption technique

Shubham Vora
Updated on 24-Aug-2023 16:35:44

99 Views

In this problem, we need to decrypt the given ciphertext by traversing the matrix diagonally. We can solve the problem by traversing the matrix diagonally. Also, we require to traverse only the upper part of the matrix diagonally to get the decrypted string. Problem statement – We have given an encrypted string of length N and row counts. We need to put the string in the matrix in a row-wise manner. After that, we need to traverse the matrix diagonally, starting from the [0, 0] index to decrypt the string. Sample examples Input str = "TRSI_ _ _UIPN _ ... Read More

Count levels in a Binary Tree consisting of nodes valued 1 grouped together

Prabhdeep Singh
Updated on 24-Aug-2023 15:47:42

102 Views

A binary tree is a tree where each node has a maximum of two children. We are given a binary tree that consists of only 0 and 1 as the node values. We have to find the number of levels of the binary tree that consists of at least one 1 and all the ones of that level must be present consecutively. Example Let's understand with the help of an example − Input 0 / \ 1 0 / ... Read More

Minimize cost to convert all characters of a binary string to 0s

Prabhdeep Singh
Updated on 24-Aug-2023 14:29:16

72 Views

A binary string is a string that only contains the binary numbers in it. In this problem, we are given a binary string, an array that represents the last index up to which we can flip the ones after starting from the ith index which will cost and cost for each index is given in another cost array. We have to perform some number of operations on the string to make the string completely zero. Example Let's understand the problem with the help of an example − Input string str = "101011" int arr[] = {1, 2, 2, 4, 5, ... Read More

Count of 3 sized Strings of all same or different characters using total X 0s, Y 1s and Z 2s

Shubham Vora
Updated on 24-Aug-2023 16:34:30

45 Views

In this problem, we will count the number of strings we can create using the given frequencies such that the string contains same or different characters. We have four options to create a string of length 3 using the 0, 1, and 2 characters. The first string is 012, 000, 111, and 222. So, we need to count the total number of such strings to get the answer. Problem statement – We have given three integer values: X, Y, and Z. The X represents the frequency of the ‘0’, Y represents the frequency of the ‘1’, and Z represents the ... Read More

Advertisements