Ayush Gupta

Ayush Gupta

433 Articles Published

Articles by Ayush Gupta

Page 22 of 44

Count smaller elements in sorted array in C++

Ayush Gupta
Ayush Gupta
Updated on 11-Mar-2026 329 Views

In this tutorial, we will be discussing a program to count smaller elements in sorted array in C++.In this we will be given a number and our task is to count all the elements present in the sorted array which are smaller than the given number.Example#include using namespace std; int countSmaller(int arr[], int n, int x){    return upper_bound(arr, arr+n, x) - arr; } int main(){    int arr[] = { 10, 20, 30, 40, 50 };    int n = sizeof(arr)/sizeof(arr[0]);    cout

Read More

Count smaller elements on right side using Set in C++ STL

Ayush Gupta
Ayush Gupta
Updated on 11-Mar-2026 334 Views

In this tutorial, we will be discussing a program to count smaller elements on right side using set in C++ STL.For this we will be provided with an array. Our task is to construct a new array and add the number of smaller elements on the right side of the current element at its position.Example#include using namespace std; void count_Rsmall(int A[], int len){    set s;    int countSmaller[len];    for (int i = len - 1; i >= 0; i--) {       s.insert(A[i]);       auto it = s.lower_bound(A[i]);       countSmaller[i] = distance(s.begin(), ...

Read More

Count the number of 1's and 0's in a binary array using STL in C++

Ayush Gupta
Ayush Gupta
Updated on 11-Mar-2026 457 Views

In this tutorial, we will be discussing a program to count the number of 1’s and 0’s in a binary array using STL in C++.For this we will be provided with an array. Our task is to count the number of 0’s and 1’s present in the array.Example#include using namespace std; // checking if element is 1 or not bool isOne(int i){    if (i == 1)       return true;    else       return false; } int main(){    int a[] = { 1, 0, 0, 1, 0, 0, 1 };    int n = sizeof(a) / sizeof(a[0]);    int count_of_one = count_if(a, a + n, isOne);    cout

Read More

Counting Inversions using Set in C++ STL

Ayush Gupta
Ayush Gupta
Updated on 11-Mar-2026 311 Views

In this tutorial, we will be discussing a program to count inversions using set in C++ STL.Inversion count is a measure of how near the array is to be completely sorted. If the array is already sorted, the inversion count will be 0.Example#include using namespace std; //returning inversion count int get_Icount(int arr[],int n){    multiset set1;    set1.insert(arr[0]);    int invcount = 0; //initializing result    multiset::iterator itset1;    for (int i=1; i

Read More

Counts of distinct consecutive sub-string of length two using C++ STL

Ayush Gupta
Ayush Gupta
Updated on 11-Mar-2026 176 Views

In this tutorial, we will be discussing a program to count distinct consecutive sub-string of length two using C++ STL.For this we will provided with a string. Our task is to count and print all the unique substrings of length two from the given string.Example#include using namespace std; void calc_distinct(string str){    map dPairs;    for (int i=0; i

Read More

Correct the Random Pointer in Doubly Linked List in C++

Ayush Gupta
Ayush Gupta
Updated on 11-Mar-2026 241 Views

In this tutorial, we will be discussing a program to correct the random pointer in a doubly linked list.For this we will be provided with a doubly linked list with one node having a random pointer. Our task is to rectify the element to whom the pointer should be pointing i.e the element next to it.Example#include using namespace std; //node structure for doubly linked list struct node {    int data;    node* next;    node* prev; }; //new node creation node* newNode(int data){    node* temp = new node;    temp->data = data;    temp->next = temp->prev = ...

Read More

Cost of painting n * m grid in C++

Ayush Gupta
Ayush Gupta
Updated on 11-Mar-2026 224 Views

In this tutorial, we will be discussing a program to find the cost of painting n*m grid.For this we will be provided with two integers n and m. Our task is to calculate the minimum cost of painting a n*m grid is the cost of painting a cell is equal to the number of painted cells adjacent to it.Example#include using namespace std; //calculating the minimum cost int calc_cost(int n, int m){    int cost = (n - 1) * m + (m - 1) * n;    return cost; } int main(){    int n = 4, m = 5;    cout

Read More

Cost to Balance the parentheses in C++

Ayush Gupta
Ayush Gupta
Updated on 11-Mar-2026 161 Views

In this tutorial, we will be discussing a program to find the cost to balance the parentheses.For this we will be provided with a sequence of brackets. Our task is to balance those parentheses in the equation by shifting their position by one and print -1 if balancing them isn’t possible.Example#include using namespace std; int costToBalance(string s) {    if (s.length() == 0)       cout

Read More

Cost to make a string Panagram in C++

Ayush Gupta
Ayush Gupta
Updated on 11-Mar-2026 200 Views

In this tutorial, we will be discussing a program to find the cost of making a string panagram.For this we will be provided with an array of integers. Our task is to convert the given string into a panagram and calculate the cost of doing that with the help of the array provided with the costs of adding characters.Example#include using namespace std; //calculating the total cost of //making panagram int calc_cost(int arr[], string str) {    int cost = 0;    bool occurred[26] = { false };    for (int i = 0; i < str.size(); i++)     ...

Read More

Count 'd' digit positive integers with 0 as a digit in C++

Ayush Gupta
Ayush Gupta
Updated on 11-Mar-2026 182 Views

In this tutorial, we will be discussing a program to find the numbers having ‘d’ digits with 0 as a digit.For this we will be provided with a number ‘d’. Our task is to count and print the number of positive integers having ‘d’ digits and 0 as one of their digit.Example#include using namespace std; //counting the number of 'd' digit numbers int count_num(int d) {    return 9*(pow(10,d-1) - pow(9,d-1)); } int main(){    int d = 1;    cout

Read More
Showing 211–220 of 433 articles
« Prev 1 20 21 22 23 24 44 Next »
Advertisements