Programming Articles - Page 1026 of 3363

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

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

440 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

187 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

809 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

518 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

577 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

234 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

C++ program to get length of strings, perform concatenation and swap characters

Arnab Chakraborty
Updated on 07-Oct-2021 07:37:09

384 Views

Suppose we have two strings s and t, we shall have to find output in three lines, the first line contains lengths of s and t separated by spaces, the second line is holding concatenation of s and t, and third line contains s and t separated by space but their first characters are swapped.So, if the input is like s = "hello", t = "programmer", then the output will be5 10 helloprogrammer pello hrogrammerTo solve this, we will follow these steps −display length of s then print one space and length of tdisplay s + ttemp := s[0]s[0] := ... Read More

C++ Program to take out integer from comma separated string

Arnab Chakraborty
Updated on 07-Oct-2021 07:35:03

4K+ Views

Suppose we have a string where we have few integers which are separated by comma. We shall have to separate them out and display each integer in different line. To do this we shall use stringstream (under sstream library) in C++. This is one string based stream class present in C++. We can use extraction operator () to insert something and str() functions to set contents of underlying string device object.So, if the input is like s = "56, 9, 85, 256, 47", then the output will be56 9 85 256 47To solve this, we will follow these steps −Define ... Read More

C++ program to reverse an array elements (in place)

Arnab Chakraborty
Updated on 07-Oct-2021 07:32:30

22K+ Views

Suppose we have an array with n different elements. We shall have to reverse the elements present in the array and display them. (Do not print them in reverse order, reverse elements in place).So, if the input is like n = 9 arr = [2, 5, 6, 4, 7, 8, 3, 6, 4], then the output will be [4, 6, 3, 8, 7, 4, 6, 5, 2]To solve this, we will follow these steps −for initialize i := 0, when i < quotient of n/2, update (increase i by 1), do:temp := arr[i]arr[i] := arr[n - i - 1]arr[n - ... Read More

C++ program to find addition and subtraction using function call by address

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

2K+ Views

Suppose we have two numbers a and b. We shall have to define a function that can calculate (a + b) and (a - b) both. But using a function in C++, we can return at most one value. To find more than one output, we can use output parameters into function arguments using pointers, and call that function using addresses of those variables. Here in this problem we shall update a with a+b and b with a-b. When we call the function we shall have to pass the address of these two variables.So, if the input is like a ... Read More

Advertisements