Server Side Programming Articles - Page 887 of 2650

C++ Program to remove items from a given vector

Arnab Chakraborty
Updated on 07-Oct-2021 08:21:08

11K+ Views

Suppose we have a set of elements present inside a vector. We shall have to perform some remove operation using erase() function of vector class type to remove using indices, and finally display rest of elements. The erase function does not take the index directly. We shall have to pass its address by passing v.begin()+index, here v is the vector and v.begin() is the address of the first element (0th element). Now by adding index with it, it will move towards the element that is present at given index.So, if the input is like v = [5, 8, 6, 3, ... Read More

Program to find value of K for K-Similar Strings in C++

Arnab Chakraborty
Updated on 07-Oct-2021 08:38:04

279 Views

Suppose we have two strings s and t. These two strings are K-similar when we can swap the positions of two letters in s exactly K times so that the resulting string is t. We have two anagrams s and t, and we have to find the smallest K for which s and t are K-similar.So, if the input is like s = "abc", t = "bac", then the output will be 1.To solve this, we will follow these steps −Define a function swapp(), this will take string s, i, j, x := s[i], y := s[j]s[i] := y, s[j] ... Read More

Program to find maximum XOR with an element from array in Python

Arnab Chakraborty
Updated on 07-Oct-2021 08:15:55

340 Views

Suppose we have an array called nums with non-negative values. We also have another array called queries where queries[i] has a pair (xi, mi). The answer of ith query is the maximum bitwise XOR value of xi and any element of nums that less than or equal to mi. If all elements in nums are larger than mi, then the answer is -1. So we have to find an array answer where size of answer is same as size of query and answer[i] is the answer to the ith query.So, if the input is like nums = [0, 1, 2, ... Read More

C++ program to create boxes and calculate volume and check using less than operator

Arnab Chakraborty
Updated on 07-Oct-2021 08:08:29

3K+ Views

Suppose we shall have to define a box class with few conditions. These are as follows −There are three attributes l, b and h for length, breadth and height respectively, (these are private variables)Define one non-parameterized constructor to set l, b, h to 0, and one parameterized constructor to set values initially.Define getter methods for each attributeDefine a function calculateVolume() get the volume of the boxOverload less than operator (

Program to find minimum adjacent swaps for K consecutive ones in Python

Arnab Chakraborty
Updated on 07-Oct-2021 08:12:07

407 Views

Suppose we have one binary array nums, and a value k. In one move, we can select two adjacent indices and swap their values. We have to find the minimum number of moves required so that nums has k consecutive 1's.So, if the input is like nums = [1, 0, 0, 1, 0, 1, 0, 1], k = 3, then the output will be 2 because in one swap we can make array from [1, 0, 0, 1, 0, 1, 0, 1] to [1, 0, 0, 0, 1, 1, 0, 1], then [1, 0, 0, 0, 1, 1, 1, 0].To ... Read More

Program to check existence of edge length limited paths in Python

Arnab Chakraborty
Updated on 07-Oct-2021 08:07:53

168 Views

Suppose we have one undirected weighted graph with n nodes using one edgeList, where edgeList[i] has three parameters (u, v, w) denotes there is a path from u to v whose distance is w. We also have another query array where query[i] has (p, q, lim). This query is trying to ask whether there is a path (direct or via some other node) from p to q whose distance is less than lim. We have to return an array holding True/False results for each query.So, if the input is likethen the output will be [True, False, True]. Because to go ... Read More

C++ program to check how many students have greater score than first one

Arnab Chakraborty
Updated on 07-Oct-2021 07:46:48

796 Views

Suppose we have n students score on five subjects. The first scores are for Kamal, and there are n-1 more scores for other students and each student has five subjects. We shall have to count number of students who have scored more than Kamal. Here we shall define one class called students to load scores for each students. The class has one Input() function to take input and calculateTotalScore() function to calculate score of a student from given five marks.So, if the input is like n = 4 scores = [[25, 45, 32, 42, 30], [22, 25, 41, 18, 21], ... Read More

Program to minimize deviation in array in Python

Arnab Chakraborty
Updated on 07-Oct-2021 07:54:25

513 Views

Suppose we have an array nums. We can perform two types of operations on any element of the array any number of timesFor even elements, divide it by 2For odd elements, multiply it by 2.Now the deviation of the array is the maximum difference between any two elements in the array. We have to find the minimum deviation the array can have after performing some number of operations. So, if the input is like nums = [6, 3, 7, 22, 5], then the output will be 5 because we can make our array in one operation [6, 6, 7, 22, ... Read More

C++ program to hold student info using data hiding and encapsulation

Arnab Chakraborty
Updated on 07-Oct-2021 07:44:04

562 Views

Suppose we want to make student data type with data hiding and encapsulation. Students must have first_name, last_name, age and class items, but these variables cannot be accessible directly. We shall have to define some functions like get_firstname() set_firstname(), get_age() set_age() etc to retrieve and update variable values, and a to_string() function to display students details in this format (age, first_name, last_name, class). From the console take four parameters as input and set them using setter methods that we have defined, and show each items using getter methods and finally using to_string() method.So, if the input is likepriyam kundu 16 ... Read More

C++ program to make student type data and display in proper format

Arnab Chakraborty
Updated on 07-Oct-2021 07:39:53

216 Views

Suppose we have provided the first name, last name, age and class of a student in different lines. We shall have to write a program using structs in C++ to read them all and show in this format (age, first_name, last_name, class). The age and class will be of type integer, and first_name and last_name are of time string.So, if the input is likepriyam kundu 16 10then the output will be (16, priyam, kundu, 10)To solve this, we will follow these steps −define a structure with first_name, last_name of type string and age, cl of type integerread each line and ... Read More

Advertisements