Programming Articles - Page 1450 of 3366

Write a Golang program to find odd and even numbers using bit operation

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

545 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

Write a program in C++ to find the missing positive number in a given array of unsorted integers

Nishu Kumari
Updated on 20-Aug-2025 17:19:07

482 Views

We are given an array of unsorted integers, and the task is to find the missing positive integer. The given array may contain negative numbers, zeros, and duplicate values, all in any random order. Let's look at some example scenarios to understand the problem clearly- Scenario 1- Input: arr = [3, -2, 5, 1, -7, 4, -1, 8] Output: 2 Explanation: The number 2 is not present in the array, so the missing positive is 2 Scenario 2- Input: arr = [0] Output: 1 Explanation: In the given array, '1' is the only positive ... Read More

Write a Golang program to find duplicate elements in a given range

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

643 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

Write a Golang program to find duplicate elements in a given array

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

Write a program in C++ to find the maximum and second maximum in a given unsorted array of integers

Nishu Kumari
Updated on 20-Aug-2025 17:22:31

4K+ Views

We are given an array of unsorted integers of size N. The task is to find the distinct maximum and second maximum elements which are present in the array. The array may contain duplicate elements also, so we have to find only distinct elements. If there is no second maximum, we will return -1 for the second maximum. Let's look at some example scenarios to understand the problem clearly - Scenario 1- Input: N = 5, A[] = {2, 2, 1, 3, 4} Output: 4 and 3 Explanation: From the given array, we can see that '4' is ... Read More

Write a Golang program to sort a binary array in linear time

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

338 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

Write a Golang program to find pairs with the given sum in an array(O(n))

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

681 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

Write a Golang program to find pairs with given sum in an array(O(nlogn))

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

282 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: Sort the given array, declare low:=0 and high:=size-1 variables.Step 3: Iterate a for loop until low sum, then high--.Step 5: At the end, print “pair not found”.ProgramLive Demopackage main import (    "fmt"    "sort" ) func findSumPair(arr []int, sum int){    sort.Ints(arr)    low := 0    high := len(arr) - 1    for low

Write a Golang program to find pairs with the given sum in an array(O(n2))

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

528 Views

ExamplesInput Array = [4, 1, 6, 8, 7, 2, 3], sum = 11 => (4, 7) or (8, 3)Approach to solve this problemStep 1: Define a method that accepts an array and sum.Step 2: Iterate from 0 to n as i.Step 3: Again, iterate a for loop from i+1 to n-1 as j.Step 4: If arr[i] + arr[j] == sum, then return arr[i] and arr[j].Step 5: At the end, print that pair not found.ProgramLive Demopackage main import (    "fmt" ) func findSumPair(arr []int, sum int){    for i:=0; i

Find the Kth Node from the end in a given singly Linked List using C++

Dev Prakash Sharma
Updated on 05-Feb-2021 12:08:35

490 Views

A linked list is a linear data structure that has multiple nodes that are connected with each other. Each node consists of two fields Data Field and address of the next node. Let us assume we have given a singly linked list the task is to find the kth node from the end in a given singly linked list. For example, Input −1→2→3→4→7→8→9 K= 4Output −Node from the 4th Position is − 4Explanation − Since in the given singly linked list ‘4th’ node from the end is ‘4’, we will return the output as ‘4’.Approach to solve this problemInitially, we ... Read More

Advertisements