Found 26504 Articles for Server Side Programming

INT_MAX and INT_MIN in C/C++ and Applications

Ayush Gupta
Updated on 01-Apr-2020 06:30:53

3K+ Views

In this tutorial, we will be discussing a program to understand INT_MAX and INT_MIN in C/C++.INT_MIN and INT_MAX are macros that are defined to set the minimum and maximum value for a variable/element.Example Live Demo#include int main(){    printf("%d", INT_MAX);    printf("%d", INT_MIN);    return 0; }Output2147483647 -2147483648ApplicationCalculating MIN value in an arrayExample Live Demo#include //calculating minimum element in an array int compute_min(int arr[], int n){    int MIN = INT_MAX;    for (int i = 0; i < n; i++)    MIN = std::min(MIN, arr[i]);    std::cout

Insertion sort using C++ STL

Ayush Gupta
Updated on 01-Apr-2020 06:20:53

799 Views

In this tutorial, we will be discussing a program to understand the insertion sort using C++ STL.In this we use std::upper_bound to find the element at the wrong position, then rotate the unsorted part of the array to make it sorted.Example Live Demo#include //function to perform insertion sort void insertionSort(std::vector &vec){    for (auto it = vec.begin(); it != vec.end(); it++){       auto const insertion_point =       std::upper_bound(vec.begin(), it, *it);       std::rotate(insertion_point, it, it+1);    } } //printing the array void print(std::vector vec){    for( int x : vec)    std::cout

Insertion and Deletion in STL Set C++ program

Ayush Gupta
Updated on 01-Apr-2020 06:18:48

448 Views

In this tutorial, we will be discussing a program to understand the insertion and deletion in STL set in C++.The set is a container element. The properties that make it unique is that it can only contain unique elements and they can be looped over in a sorted manner.ExampleInsertion Live Demo#include #include using namespace std; int main(){    set st;    //declaring iterators    set::iterator it = st.begin();    set::iterator it1, it2;    pair< set::iterator,bool> ptr;    //inserting a single element    ptr = st.insert(20);    if (ptr.second)       cout

Input Iterators in C++

Ayush Gupta
Updated on 01-Apr-2020 06:16:13

373 Views

In this tutorial, we will be discussing a program to understand input iterators in C++.Input iterators are one of the five iterators in STL being the weakest and simplest of all. They are mostly used in serial input operations where each value is read one and then the iterator moves to the next one.Example Live Demo#include #include using namespace std; int main(){    vector v1 = { 1, 2, 3, 4, 5 };    //declaring iterator    vector::iterator i1;    for (i1 = v1.begin(); i1 != v1.end(); ++i1) {       //looping over elements via iterator       cout

Print Immutable Linked List in Reverse in C++

Arnab Chakraborty
Updated on 31-Mar-2020 09:19:35

464 Views

Suppose we have an immutable linked list, we have to print out all values of each node in reverse with the help of the following interface −ImmutableListNode − This is an interface of an immutable linked list, we are given the head of the list.We have to use the following functions to access the linked list −ImmutableListNode.printValue() − This will print value of the current node.ImmutableListNode.getNext() −This will return the next node.So if the list is like: [0, -4, -1, 3, -5], then the output will be [-5, 3, -1, -4, 0]To solve this, we will follow these steps −Define ... Read More

Get Equal Substrings Within Budget in C++

Arnab Chakraborty
Updated on 31-Mar-2020 09:16:56

198 Views

Suppose we have given two strings s and t of the same length. We want to change s to t. Changing the i-th character of s to i-th character of t will assign cost as |s[i] - t[i]| that is, the absolute difference between the ASCII values of the characters. We have also given an integer maxCost. We have to find the maximum length of a substring of s that can be changed to be the same as the corresponding substring of t with a cost less than or equal to maxCost.So if the input is like s = “abcd” ... Read More

Smallest String With Swaps in C++

Arnab Chakraborty
Updated on 31-Mar-2020 09:15:52

400 Views

Suppose we have given a string s, and an array of pairs of indices in the string pairs where pairs[i] = [a, b] indicates 2 indices(0-indexed) of the string. We can swap the characters at any pair of indices in the given pairs any number of times as we want. We have to find the lexicographically smallest string that s can be changed to after using the swaps. So if the input is like s = “dcab” and pairs = [[0, 3], [1, 2]], then the output will be “bacd”. Exchange s[0] and s[3], s = "bcad", then exchange s[1] ... Read More

Ugly Number III in C++

Arnab Chakraborty
Updated on 29-Apr-2020 16:38:57

235 Views

Suppose we have to write a program to find the n-th ugly number. The Ugly numbers are positive integers which are divisible by a or b or c. So for example, if n = 3 and a = 2, b = 3 and c = 5, then the output will be 4, as the ugly numbers are [2, 3, 4, 5, 6, 8, 9, 10], the third one is 4.To solve this, we will follow these steps −make a method called ok(), this will take x, a, b, c, this will act like below −return (x/a) + (x/b) + (x/c) ... Read More

Find Smallest Common Element in All Rows in C++

Arnab Chakraborty
Updated on 29-Apr-2020 16:35:19

211 Views

Suppose we have a matrix mat where every row is sorted in non-decreasing order, we have to find the smallest common element in all rows. If there is no common element, then return -1. So if the matrix is like −1234524581035791113579The output will be 5To solve this, we will follow these steps −Define a map m, n := row count of matrix, if n is not 0, then x = column size, otherwise 0for i in range 0 to n – 1for j in range 0 to x – 1if m[mat[i, j]] + 1 = i + 1, then increase ... Read More

Minimum Knight Moves in C++

Arnab Chakraborty
Updated on 29-Apr-2020 16:32:23

873 Views

Suppose we have an infinite chessboard with coordinates from -infinity to +infinity, and we have a knight at square [0, 0]. A knight has 8 possible moves it can make, as shown below. Each move is two squares in a cardinal direction, then one square in an orthogonal direction.We have to find the minimum number of steps needed to move the knight to the square [x, y]. It is guaranteed the answer exists.So if the input is like x = 5 and y = 5, then the output will be 4. This will be like [0, 0] → [2, 1] ... Read More

Advertisements