Close

Couple elimination problem

Given a string of some symbols, write a program to transform a string by repeatedly eliminating two same symbols occurring together.For example if the input string is ‘RGGB’ it will be reduced to ‘RB’ because ‘GG’ can be eliminated. Here are some more examples of input – outputInput   | Output——————-GBRRBR  | GRBGRGRR  | BGRGGGBB    |…

Common elements between two sorted arrays

Given two sorted arrays, how do you efficiently find the common elements between those two?Method#1: Simple and Brute-force approachIterate through one array, and try to find the element in another array using linear search. If it is found, then print it. This approach takes O(n^2) time as for each element in the first array we…

Rotating an array

Rotating an array: In this post we will discuss two solutions for rotating an array by a given number of times. One is a simple solution and another uses the reversal operation (discusses in the previous post). Method#1: Simple and intuitive In this method, we will copy the last element into a variable, and shift…

Reversing an array

In this post we will discuss a simple problem on Arrays. Given an array of numbers, how do we reverse it efficiently? Here is the simple and effective approach. We start with one index pointing to the start, and another index pointing to the end of the array. We swap the two elements at these…

Bracket matching problem

Stack is one of the most widely used data structure. In this post we will learn how to use the stack container (data structure) provided by C++ STL with an example. Let us consider the following problem. We have a string which contains only ‘(‘ and ‘)’ characters. We have to write a program to…

Finding count of a number in a sorted array

In Competitive programming, learning to use the standard library than coding from scratch saves a lot of time. In this post we will use C++ STL(Standard Template Library) binary search algorithm with an example.Given a sorted array of numbers, How to find the frequency of a given number? Here is an efficient algorithm to do…