Article Categories
- All Categories
-
Data Structure
-
Networking
-
RDBMS
-
Operating System
-
Java
-
MS Excel
-
iOS
-
HTML
-
CSS
-
Android
-
Python
-
C Programming
-
C++
-
C#
-
MongoDB
-
MySQL
-
Javascript
-
PHP
-
Economics & Finance
Programming Articles
Page 1182 of 2547
How to convert multiple columns into single column in an R data frame?
To convert multiple columns into single column in an R data frame, we can use unlist function. For example, if we have data frame defined as df and contains four columns then the columns of df can be converted into a single by using data.frame(x=unlist(df)).Example1y1
Read MoreSum of XOR of sum of all pairs in an array in C++
In this problem, we are given an array arr[] of size n. Our task is to create a program to find the sum of XOR of sum of all pairs in an array.Let’s see an example to understand the problem, Input: arr[5, 7, 9]Output: 22Explanation: (5+5) ^ (5+7) ^ (5+9) ^ (7+5) ^ (7+7) ^ (7+9) ^ (9+5) ^ (9+7) ^ (9+9) = 22A simple solution to the problem is by using a nested loop. And creating all possible pairs from the array. And calculate the XOR of the sum of each pair.Algorithm: Initialise XorSum = 0Step 1: iterate from 0 to n. Follow:Step 1.1: iterate ...
Read MoreWrite a Golang program to find the frequency of an element in an array
ExamplesIn the Input array, arr = [2, 4, 6, 7, 8, 1, 2]Frequency of 2 in given array is 2Frequency of 7 is 1Frequency of 3 is 0.Approach to solve this problemStep 1: Define a function that accepts an array and a numStep 2: Declare a variable count = 0.Step 3: Iterate the given array and increase the count by 1 if num occurs in the array.Step 4: Print count for the given num.Programpackage main import "fmt" func findFrequency(arr []int, num int){ count := 0 for _, item := range arr{ if item == num{ ...
Read MoreHow to replace vector values less than 2 with 2 in an R vector?
If we have a vector that contains values with less than, equal to, and greater than 2 and the value 2 is the threshold. If this threshold value is defined for lower values and we want to replace the values that are less than 2 with 2 then pmax function can be used. For example, for a vector x, it will be done as pmax(x,2).Example1x1
Read MoreSum over Subsets - Dynamic Programming in C++
In this problem, we are given an array arr[] of size 2n. Our task is to create a program to find the sum over subset using dynamic programming to solve it.We need to calculate function, F(x) = Σ Ai such that x&i == i for all x. i.e. i is a bitwise subset of x.Let’s take an example to understand the problem, Input: A[] = {5, 7, 1, 9} , n = 2Output: 5 12 6 22Explanation: For n = 2 , there are 4 values of x. They are 0, 1, 2, 3.Now, calculating values of the function:F(0) = A0 ...
Read MoreWrite a Golang program to find prime numbers in a given range
ExamplesInput num1=3 and num2=8 => Prime numbers are: 3, 5, 7Input num1=8 and num2=23 => Prime numbers are: 11, 13, 17, 19, 23Approach to solve this problemStep 1: Define a function that accepts two numbers, num1 and num2, type is int.Step 2: Iterate between num1 and num2.Step 3: If the number is prime, then print that number, else break.Programpackage main import ( "fmt" "math" ) func printPrimeNumbers(num1, num2 int){ if num1
Read MoreHow to count the number of duplicate rows in an R data frame?
To count the number of duplicate rows in an R data frame, we would first need to convert the data frame into a data.table object by using setDT and then count the duplicates with Count function. For example, if we have a data frame called df then the duplicate rows will be counted by using the command − setDT(df)[,list(Count=.N),names(df)].Example1y1
Read MoreSum of the series 1 + (1+3) + (1+3+5) + (1+3+5+7) + ...... + (1+3+5+7+...+(2n-1)) in C++
In this problem, we are given a number n. Our task is to create a program to find the sum of series 1 + (1+3) + (1+3+5) + (1+3+5+7) + …… + (1+3+5+7+…+(2n-1)). Let’s take an example to understand the problem, Input: n = 5Output: 55So, according to the question suppose a user gives us a number ‘n’ and we have to add series 1 + (1+3) + (1+3+5) + (1+3+5+7) + …… + (1+3+5+7+…+(2n-1)).Let's understand the meaning of this series better first.We take n=1, then the series becomes 1.We take n=2, then the series becomes 1+ (1+3) because the ...
Read MoreWrite a Golang program to sort an array using Bubble Sort
Definition: Bubble Sort is the simplest sorting algorithm that works by repeatedly swapping the adjacent elements if they are in the wrong order.ExamplesInput arr = [7, 5, 1, 6, 3]1st iteration => swap(7, 5) => swap(7, 1) => swap(7, 6) => swap(7, 3) => [5, 1, 6, 3, 7]2nd iteration => [1, 5, 3, 6, 7]3rd iteration => [1, 3, 5, 6, 7]4th iteration => [1, 3, 5, 6, 7]5th iteration => [1, 3, 5, 6, 7]Approach to solve this problemStep 1: Iterate the array from 0th index to n-1.Step 2: Iterate the array from the 0th index to n-1-i, ...
Read MoreHow to remove the first and last character in a string in R?
To remove the first and last character in a string, we can use str_sub function of stringr package. For example, if a word say tutorialspoint is mistakenly typed as ttutorialspointt and stored in a vector called x then to remove the first and last “t”, we can use the command str_sub(x,2,−2).Example1library(stringr) x1
Read More