Found 7347 Articles for C++

Count all distinct pairs with difference equal to k in C++

Ayush Gupta
Updated on 05-Feb-2020 07:41:45

160 Views

In this tutorial, we will be discussing a program to find the distinct pairs with difference equal to k.For this we will be provided with an integer array and the value k. Our task is to count all the distinct pairs that have the difference as k.Example Live Demo#include using namespace std; int count_diffK(int arr[], int n, int k) {    int count = 0;    //picking elements one by one    for (int i = 0; i < n; i++) {       for (int j = i+1; j < n; j++)          if (arr[i] - ... Read More

Count all 0s which are blocked by 1s in binary matrix in C++

Ayush Gupta
Updated on 05-Feb-2020 07:38:38

211 Views

In this tutorial, we will be discussing a program to find the count of 0s which are blocked by 1s in a binary matrix.For this we will be provided with a binary matrix. Our task is to find and count all the 0s in the matrix that are blocked by 1s.Example Live Demo#include using namespace std; #define Row 4 #define Col 5 int r[4] = { 0, 0, 1, -1 }; int c[4] = { 1, -1, 0, 0 }; bool isSafe(int x, int y, int M[][Col]) {    if (x >= 0 && x = 0 &&       y

Count 1’s in a sorted binary array in C++

Ayush Gupta
Updated on 05-Feb-2020 07:32:28

99 Views

In this tutorial, we will be discussing a program to find the 1’s in a sorted binary array.For this we will be provided with an array containing only 1 and 0. Our task is to count the number of 1’s present in the array.Example Live Demo#include using namespace std; //returning the count of 1 int countOnes(bool arr[], int low, int high){    if (high >= low){       int mid = low + (high - low)/2;       if ( (mid == high || arr[mid+1] == 0) && (arr[mid] == 1))          return mid+1;   ... Read More

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

Ayush Gupta
Updated on 05-Feb-2020 07:29:56

105 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 Live Demo#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

Cost to make a string Panagram in C++

Ayush Gupta
Updated on 05-Feb-2020 07:27:08

116 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 Live Demo#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

Cost to Balance the parentheses in C++

Ayush Gupta
Updated on 05-Feb-2020 07:23:37

83 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 Live Demo#include using namespace std; int costToBalance(string s) {    if (s.length() == 0)       cout

Cost of painting n * m grid in C++

Ayush Gupta
Updated on 05-Feb-2020 07:17:06

119 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 Live Demo#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

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

Ayush Gupta
Updated on 05-Feb-2020 07:14:03

97 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 Live Demo#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

Practice questions on Arrays in C++

sudhir sharma
Updated on 04-Feb-2020 07:48:37

853 Views

Array is a data structure that store data in the contagious memory location.Declaring arraysDeclaring arrays is done by the following syntax : int 1D[] - for 1-D array int 2D[][] - for 2-D arrayIf you initialize an array with lesser number of elements, rest are initialized with 0.Memory address of elements of the array1-D array : address[i] = baseAddress + i*size 2-D array (row major) : address[i][j] = baseAddress + (i*n + j) * sizeNow, let’s see some practice problemPredict the output of the following code snippetint arr[5] = {6, 9}; for(int i = 0; i

Practice questions on Strings in C++

sudhir sharma
Updated on 04-Feb-2020 07:46:23

512 Views

String is an important part of programming. Strings are the array of character types. In competitive exams like GATE also it is an important topic. So let’s discuss some key points about string and then we will proceed to some questions that will help you clear your concepts about string.String in a programming language can be stored in two different ways. They are using character array (char str[size]) and using a pointer that will point to the string (char * ch = “Hello”). There are some important things related to the use of the character array and pointer to a ... Read More

Advertisements