Programming Articles

Page 1486 of 2547

Find the Rotation Count in Rotated Sorted array in C++

Arnab Chakraborty
Arnab Chakraborty
Updated on 11-Mar-2026 331 Views

Consider we have an array, which is rotated sorted array. We have to find number of rotations are required to sort the array. (We will consider rotation right to left.)Suppose the array is like: {15, 17, 1, 2, 6, 11}, then we have to rotate the array two times to sort. The final order will be {1, 2, 6, 11, 15, 17}. Here output is 2.The logic is simple. If we notice, we can see that the number of rotation is same as the value of index of minimum element. So if we get the minimum element, then its index ...

Read More

Finding LCM of more than two (or array) numbers without using GCD in C++

Arnab Chakraborty
Arnab Chakraborty
Updated on 11-Mar-2026 1K+ Views

We have an array A, we have to find the LCM of all elements without using the GCD operation. If the array is like {4, 6, 12, 24, 30}, then the LCM will be 120.The LCM can be calculated easily for two numbers. We have to follow this algorithm to get the LCM.getLCM(a, b) −begin    if a > b, then m := a, otherwise m := b       while true do          if m is divisible by both a and b, then return m             m := m + ...

Read More

How to sort HashSet in Java

AmitDiwan
AmitDiwan
Updated on 11-Mar-2026 2K+ Views

To sort HashSet in Java, you can use another class, which is TreeSet.Following is the code to sort HashSet in Java −Exampleimport java.util.*; public class Main {    public static void main(String args[]) {       Set hashSet = new HashSet();       hashSet.add("green");       hashSet.add("blue");       hashSet.add("red");       hashSet.add("cyan");       hashSet.add("orange");       hashSet.add("green");       System.out.println("HashSet elements"+ hashSet);       Set treeSet = new TreeSet(hashSet);       System.out.println("Sorted elements"+ treeSet);    } }OutputHashSet elements [red, orange, green, blue, cyan] Sorted elements [blue, cyan, green, ...

Read More

Compare two Strings in Java

AmitDiwan
AmitDiwan
Updated on 11-Mar-2026 466 Views

Compare two strings using compareTo() method in Java. The syntax is as follows −int compareTo(Object o)Here, o is the object to be compared.The return value is 0 if the argument is a string lexicographically equal to this string; a value less than 0 if the argument is a string lexicographically greater than this string; and a value greater than 0 if the argument is a string lexicographically less than this string.ExampleLet us now see an example −public class Demo {    public static void main(String args[]) {       String str1 = "Strings are immutable";       String ...

Read More

Floor and Ceil from a BST in C++

Arnab Chakraborty
Arnab Chakraborty
Updated on 11-Mar-2026 383 Views

Here we will see, how to find the Floor and Ceiling value from BST. For example, if we want to make a memory management system, where free nodes are arranged in BST. Find best fit for the input request. Suppose we are moving down the tree with smallest data larger than the key value, then there are three possible cases.Root is the key. Then root value is the ceiling valueIf root data < key, then the ceiling value will not be at the left subtree, then proceed to right subtree, and reduce the problem domainIf root data > key, then ...

Read More

C++17 If statement with initializer

Narendra Kumar
Narendra Kumar
Updated on 11-Mar-2026 15K+ Views

C++17 has extended existing if statement’s syntax. Now it is possible to provide initial condition within if statement itself. This new syntax is called "if statement with initializer". This enhancement simplifies common code patterns and helps users keep scopes tight. Which in turn avoids variable leaking outside the scope.ExampleLet us suppose we want to check whether given number is even or odd. Before C++17 our code used to look like this −#include #include using namespace std; int main() {    srand(time(NULL));    int random_num = rand();    if (random_num % 2 == 0) {       cout

Read More

Check for balanced parentheses in an expression in C++

Arnab Chakraborty
Arnab Chakraborty
Updated on 11-Mar-2026 4K+ Views

Suppose we have an expression. The expression has some parentheses; we have to check the parentheses are balanced or not. The order of the parentheses are (), {} and []. Suppose there are two strings. “()[(){()}]” this is valid, but “{[}]” is invalid.The task is simple; we will use stack to do this. We should follow these steps to get the solution −Traverse through the expression until it has exhaustedif the current character is opening bracket like (, { or [, then push into stackif the current character is closing bracket like ), } or ], then pop from stack, ...

Read More

Check for Palindrome after every character replacement Query in C++

Arnab Chakraborty
Arnab Chakraborty
Updated on 11-Mar-2026 261 Views

Consider we have a string and some queries in set Q. Each query contains a pair of integers i and j. and another character c. We have to replace characters at index i and j with the new character c. And tell if the string is palindrome or not. Suppose a string is like “AXCDCMP”, if we use one query like (1, 5, B), then the string will be “ABCDCBP”, then another query like (0, 6, A), then it will be “ABCDCBA”, this is palindrome.We have to create one query using indices i, j, then replace the characters present at ...

Read More

Check for possible path in 2D matrix in C++

Arnab Chakraborty
Arnab Chakraborty
Updated on 11-Mar-2026 698 Views

Consider we have a 2D array. We have to find if we can get a path from topleft corner to bottom-right corner. The matrix is filled with 0s and 1s. 0 indicates open area, 1 indicates blockage. Note that the top-left corner will always be 1.Suppose a matrix is like below −0001010011000101000000100One path is marked as green, there are some other paths also. So the program will return true, if there is a path, otherwise false.We will solve this problem, by changing all accessible node to -1, First change the value of starting point to -1, then get next value ...

Read More

Check given matrix is magic square or not in C++

Arnab Chakraborty
Arnab Chakraborty
Updated on 11-Mar-2026 2K+ Views

Here we will see, if a matrix is magic square or not, a magic square is a square matrix, where the sum of each row, each column, and each diagonal are same.Suppose a matrix is like below −618753294This is a magic square, if we see, the sum of each row, column and diagonals are 15.To check whether a matrix is magic square or not, we have to find the major diagonal sum and the secondary diagonal sum, if they are same, then that is magic square, otherwise not.Example#include #define N 3 using namespace std; bool isMagicSquare(int mat[][N]) {   ...

Read More
Showing 14851–14860 of 25,466 articles
Advertisements