Check If a Matrix Is Invertible in R

Nizamuddin Siddiqui
Updated on 05-Feb-2021 08:15:19

1K+ Views

If the matrix is singular then it is not invertible and if it is non−singular then it is invertible. Therefore, we can check if a matrix is singular or not. We can use is.singular.matrix function of matrixcalc for this purpose. For example, if we have a matrix called M then to check whether it is invertible or not, we can use is.singular.matrix(M).Example1Loading matrixcalc package and creating a matrix −library(matrixcalc) M1

Majority Element in Python

Dev Prakash Sharma
Updated on 05-Feb-2021 08:10:17

3K+ Views

Let’s suppose we have an array of integers. The task is to find the index of a particular element in the given array. For example, Input-1 −N = 8 A[ ] = { 1, 2, 4, 3, 3, 1, 1, 5}Output −1Explanation − In the given array of integers, the most appearing number is ‘1’. Thus the output is ‘1’.Input-2 −N = 6 A[ ] = {1, 5, 4, 4, 1, 1}Output −1Explanation − In the given array of integers, the most appearing number is ‘1’. Thus we can return the output ‘1’.Approach to solve this problemThe given array contains ... Read More

Remove Repeated Column Names in Data Table Object in R

Nizamuddin Siddiqui
Updated on 05-Feb-2021 08:00:04

489 Views

In data analysis, we sometimes deal with duplicated data or just the representation of the data with same name. One such situation is column names are same for two columns in a data.table object. For this purpose, we can make use of which function with the combination of duplicated function and set the output of that duplicate to NULL to remove the repeated column names.Example1Loading data.table package and creating a data.table object −library(data.table) x1

Counting Elements in Two Arrays Using C++

Dev Prakash Sharma
Updated on 05-Feb-2021 07:16:38

714 Views

Let us assume that we have given two unsorted arrays arr1[] and arr2[]. The task is to count the total number of elements in arr2[] for which each element of arr1[] are less than or equal to the elements present in arr2[]. However, the element in both the array may contain duplicates too.For example, Input-1 −N = 6 M = 7 arr1[N] = {1, 2, 5, 0, 6, 3} arr2[M] = {0, 0, 1, 2, 1, 3, 4, 6, 8}Output −4 5 7 2 8 6The approach used to solve this problemTo count every element of arr1[] and check if ... Read More

Maximum Erasure Value in C++

Kiran P
Updated on 04-Feb-2021 12:01:30

258 Views

Given an array of positive integers, the task is to erase a subarray containing all the unique elements. What you get by erasing the subarray is equal to the sum of its elements.Return the maximum sum of the current subarray by erasing the terms before or after it, we can get the maximum sum by erasing exactly one subarray.An array arr is called to be a subarray of a if it forms a contiguous subsequence of a that is if it is equal to a[l], a[l+1], ..., a[r] for some (l, r). For example, Input-1 −arr[ ] = { 1, 2, ... Read More

Find Odd and Even Numbers Using Bit Operation in Go

Kiran P
Updated on 04-Feb-2021 11:31:15

537 Views

ExamplesInput num = 5 => 101 & 1 = 001 => True, i.e., Odd; else num would be Even.Approach to solve this problemStep 1: Define a method that accepts a number.Step 2: Perform & operation with that number.Step 3: If the & operator returns a non-zero value, then that number would be odd.Step 4: Else, the number would be even.ProgramLive Demopackage main import "fmt" func oddEven(num int){    if num & 1 != 0 {       fmt.Println("ODD")    } else {       fmt.Println("EVEN")    } } func main(){    oddEven(13)    oddEven(50)    oddEven(0) }OutputODD EVEN EVEN

Sort a Binary Array in Linear Time using Go

Kiran P
Updated on 04-Feb-2021 11:30:28

332 Views

There are two methods in which we can solve this problem. Let’s check the first method.Method 1ExamplesInput Array = [1, 0, 1, 0, 1, 0, 0, 1] => [0, 0, 0, 0, 1, 1, 1, 1]Approach to solve this problemStep 1: Define a method that accepts an array.Step 2: Count number of 0.Step 3: Store 0 till count becomes 0 and store 1 at the remaining indexes.Step 4: At the end, return the array.ProgramLive Demopackage main import "fmt" func binarySort(arr []int) []int{    count := 0    for i:=0; i

Find Duplicate Elements in an Array using Go

Kiran P
Updated on 04-Feb-2021 11:30:03

6K+ Views

ExamplesInput Array = [1, 3, 5, 6, 1] => Duplicate element is 1;Input Array = [1, 3, 5, 6, 7] => return -1Approach to solve this problemStep 1: Define a method that accepts an array.Step 2: Declare a visited map.Step 3: Iterate the given array. If the element exists in the visited map, then return that element.Step 4: Else, return -1.ProgramLive Demopackage main import "fmt" func duplicateInArray(arr []int) int{    visited := make(map[int]bool, 0)    for i:=0; i

Find Duplicate Elements in a Given Range using Go

Kiran P
Updated on 04-Feb-2021 11:29:49

633 Views

We can solve this problem in two different ways. Let’s check the first method.Method 1: ExamplesInput Array = [1, 2, 3, 4, 4] => Range is from 1 to 5 but 4 is a duplicate element in this range.Approach to solve this problemStep 1: Define a method that accepts an array.Step 2: Declare a visited map.Step 3: Iterate the given array. If the element exists in the visited map, then return that element.Step 4: Else, return -1.ProgramLive Demopackage main import "fmt" func duplicateInArray(arr []int) int{    visited := make(map[int]bool, 0)    for i:=0; i Range is from 1 to 5 but 4 ... Read More

Find Pairs with Given Sum in an Array in Go

Kiran P
Updated on 04-Feb-2021 11:23:55

663 Views

ExamplesInput Array = [1, 3, 5, 7, 8, 9], sum = 11 => (3, 8)Approach to solve this problemStep 1: Define a method that accepts an array and sum.Step 2: Define a mapping variable, type map[int]int.Step 3: Iterate the given array as i.Step 4: If key in mapping sum-arr[i] is not present, then mapping[arr[i]]=i.Step 5: If present, then print “pair is found”.Step 6: At the end, print that “pair not found”.ProgramLive Demopackage main import "fmt" func findSumPair(arr []int, sum int){    mapping := make(map[int]int)    for i:=0; i

Advertisements