Found 33676 Articles for Programming

Write a program in C++ to find the most frequent element in a given array of integers

Dev Prakash Sharma
Updated on 05-Feb-2021 12:21:32

4K+ Views

Let’s suppose we have an array of integers of size N. The task is to find the most frequent element present in the given array of integers. 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, 4, 4, 4, 1, 1}Output-a −1Output-b −4Explanation: In the given array of integers, the most appearing number is ‘1’ and ‘4’. Thus we can return the output to any one ... Read More

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

Dev Prakash Sharma
Updated on 05-Feb-2021 12:15:59

554 Views

Let’s suppose we have given an array of unsorted integers. The task is to find the positive missing number which is not present in the given array in the range [0 to n]. For example, Input-1 −N = 9 arr = [0, 2, 5, 9, 1, 7, 4, 3, 6]Output −8Explanation − In the given unsorted array, ‘8’ is the only positive integer that is missing, thus the output is ‘8’.Input-2 −N = 1 arr = [0]Output −1Explanation − In the given array, ‘1’ is the only one positive integer which is missing, thus the output is ‘1’.Approach to solve ... Read More

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

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

536 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

473 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

631 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

331 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

662 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

276 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

Advertisements