Find Maximum Value in an Array Using Go Language

Kiran P
Updated on 04-Feb-2021 10:52:45

643 Views

ExamplesA1 = [2, 4, 6, 7, 8, 10, 3, 6, 0, 1]; Maximum number is 10A2 = [12, 14, 16, 17, 18, 110, 13, 16, 10, 11]; Maximum number is 110Approach to solve this problemStep 1: Consider the number at the 0th index as the maximum number, max_num = A[0]Step 2: Compare max_num with every number in the given array, while iterating.Step 3: If a number is greater than max_num, then assign that number to max_num;Step 4: At the end of iteration, return max_num;ProgramLive Demopackage main import "fmt" func findMaxElement(arr []int) int {    max_num := arr[0]    for i:=0; i ... Read More

Find Minimum Value Element in Array using Go

Kiran P
Updated on 04-Feb-2021 10:52:24

881 Views

ExamplesA1 = [2, 4, 6, 7, 8, 10, 3, 6, 0, 1]; Minimum number is 0;A2 = [12, 14, 16, 17, 18, 110, 13, 16, 10, 11]; Minimum number is 10;Approach to solve this problemStep 1: Consider the number at the 0th index as the minimum number, min_num = A[0].Step 2: Compare min_num with every number in the given array, while iterating.Step 3: If a number is smaller than min_num, then assign that number to min_num.Step 4: At the end of iteration, return min_num;ProgramLive Demopackage main import "fmt" func findMinElement(arr []int) int {    min_num := arr[0]    for i:=0; i

Check Whether a Given Number is Prime in Golang

Kiran P
Updated on 04-Feb-2021 10:52:02

5K+ Views

Definition: A number is that is greater than 2 and divisible only by itself and 1.Examples: Prime numbers are 2, 3, 5, 7, 11, 13, 113, 119, ..., etc.Approach to solve this problemStep 1: Find square root of the given number, sq_root = √numStep 2: If the given number is divisible by a number that belongs to [2, sq_root], then print “Non Prime Number”Step 3: If not divisible by any number, then print “Prime Number”ProgramLive Demopackage main import (    "fmt"    "math" ) func checkPrimeNumber(num int) {    if num < 2 {       fmt.Println("Number must be greater than 2.")       return    }    sq_root := int(math.Sqrt(float64(num)))    for i:=2; i

Adaptive vs Non-Adaptive Routing Algorithms

Moumita
Updated on 03-Feb-2021 08:01:46

6K+ Views

Routing Algorithms − A routing algorithm is a procedure that lays down the route or path to transfer data packets from source to the destination. There can be many paths from the source to the destination; routing algorithm mathematically computes the best path, i.e. “least – cost path”. Routing algorithms are broadly classified into adaptive and non-adaptive routing algorithms.Adaptive Routing Algorithms − Also known as dynamic routing algorithms, they makes routing decisions dynamically depending on the network conditions. It constructs the routing table depending upon the network traffic and topology.Non-Adaptive Routing Algorithms − Also known as static routing algorithms, they ... Read More

Sink Tree in Computer Networks

Moumita
Updated on 03-Feb-2021 07:59:15

7K+ Views

DefinitionSink trees of a given node in a computer network is a tree formed by connecting the node with all the other nodes of in the network with the optimal path, or more precisely the shortest path. Or, in other words, a sink tree is a minimal cost tree formed with the given node as the destination and all other nodes as sources. A popular measure for the path or the cost is the number of hops.ExplanationThe sink tree follows from the optimality principle. According to it, in a network of routers for data transmission, if a router ‘J’ lies ... Read More

Adaptive Routing Algorithms

Moumita
Updated on 03-Feb-2021 07:55:34

8K+ Views

Adaptive routing algorithms, also known as dynamic routing algorithms, makes routing decisions dynamically while transferring data packets from the source to the destination. These algorithms constructs routing tables depending on the network conditions like network traffic and topology. They try to compute computes the best path, i.e. “least – cost path”, depending upon the hop count, transit time and distance.Types of Adaptive Routing AlgorithmsThe three popular types of adaptive routing algorithms are shown in the following diagram −Centralized algorithm − In centralized routing, one centralized node has the total network information and takes the routing decisions. It finds the least-cost ... Read More

Non-Adaptive Routing Algorithms

Moumita
Updated on 03-Feb-2021 07:51:35

5K+ Views

Non-adaptive routing algorithms, also known as static routing algorithms, do not change the selected routing decisions for transferring data packets from the source to the destination. They construct a static routing table in advance to determine the path through which packets are to be sent.The static routing table is constructed based upon the routing information stored in the routers when the network is booted up. Once the static paths are available to all the routers, they transmit the data packets along these paths. The changing network topology and traffic conditions do not affect the routing decisions.Types of Non − adaptive ... Read More

Constraints That Can Be Added to a PostgreSQL Table

Yash Sanghvi
Updated on 02-Feb-2021 13:13:19

216 Views

There are 6 types of constraints that can be generally used with a PostgreSQL table. They are listed and explained below −NOT NULL CONSTRAINTThis is a very common constraint. If there is a particular column that cannot have null values, you add this constraint at the time of table creation. For example, if we create a marks table, which can’t have NULL values for the name, then the table creation command will look like −CREATE TABLE marks(    name VARCHAR NOT NULL,    roll_no INTEGER,    marks_obtained INTEGER );Now, if we try to insert a row into this table, without ... Read More

Create Primary Key on an Existing Table in PostgreSQL

Yash Sanghvi
Updated on 02-Feb-2021 13:09:44

3K+ Views

Although quite infrequent, you may come across situations wherein you need to define the primary key on an existing table. This can be achieved using the ALTER TABLE statement.The syntax is −ALTER TABLE table_name ADD PRIMARY KEY (column_name1, column_name2, …., columns_nameN)As can be seen from the above syntax, you can define PRIMARY KEY on multiple columns. When you have defined the PRIMARY KEY on multiple columns, the condition is that the column pairs should have unique and non-null values. Thus, if the PRIMARY KEY is defined on (column1, column2), the values (value1, value2), (value3, value2), and (value1, value4) are allowed. ... Read More

Extract Day, Hour, Minute, etc. from a Datetime Column in PostgreSQL

Yash Sanghvi
Updated on 02-Feb-2021 13:08:17

1K+ Views

Let us create a new table containing a single timestamp column −CREATE TABLE timestamp_test(    ts timestamp );Now let us populate it with some data −INSERT INTO timestamp_test(ts) VALUES(current_timestamp), (current_timestamp+interval '5 days'), (current_timestamp-interval '18 hours'), (current_timestamp+interval '1 year'), (current_timestamp+interval '3 minutes'), (current_timestamp-interval '6 years');If you query the table (SELECT * from timestamp_test), you will see the following output −ts2021-01-30 19:23:24.0080872021-02-04 19:23:24.0080872021-01-30 01:23:24.0080872022-01-30 19:23:24.0080872021-01-30 19:26:24.0080872015-01-30 19:23:24.008087Now, in order to extract hour, minute, etc. from the timestamp column, we use the EXTRACT function. Some examples are shown below −SELECT EXTRACT(HOUR from ts) as hour from timestamp_testOutput −hour19191191919Similarly −SELECT EXTRACT(MONTH from ts) as ... Read More

Advertisements