Found 33676 Articles for Programming

First Occurrence of a Digit in a Given Fraction

Vanshika Sood
Updated on 08-Sep-2023 11:11:02

200 Views

The decimal expansion of a fraction is the decimal representation of the fraction's value. In the following article we discuss two approaches to find the first occurrence of c in a/b. Problem Statement Given three integers a, b, c, locate the first instance of c in the fraction a/b after the decimal point. Print-1 if it does not exist. Sample Examples Input a = 5, b = 6, c = 3 Output 2 Explanation $$\mathrm{\frac{a}{b}=\frac{5}{6}=0.83333}$$ So c=3 occurs at the 2nd place after the decimal point. Hence the output is 2. Input a = -10, b = ... Read More

Minimize the Absolute Difference of Sum of Two Subsets

Vanshika Sood
Updated on 08-Sep-2023 11:06:08

653 Views

To Minimize the Absolute Difference of Sum of Two Subsets, we partition a vector into two subsets i.e. we divide the elements of the vector into two smaller vectors such that each element of the original vector belongs to one of the two smaller vectors, and the two smaller vectors are disjoint. For example, if we have a vector v = {1, 2, 3, 4, 5}, then one possible partitioning of v into two subsets is S1 = {1, 3, 4} and S2 = {2, 5}, where each element of v belongs to either S1 or S2, and the ... Read More

Golang Program to Iterate Map Elements using the Range

Akhil Sharma
Updated on 07-Sep-2023 17:15:48

838 Views

Maps in Golang provide a convenient way to store and access the given data in format of key−value pairs. Iterating over a map allows us to process each key−value pair and perform operations on them. In this article, we will explore different methods to iterate map elements using the range, that includes using the range keyword, using keys and values slices, and using the reflect package. Syntax for key, value := range mapName { // Perform operations on key-value pair } This syntax uses the range keyword to iterate over the elements of ... Read More

Golang Program to Create a Copy of the Map

Akhil Sharma
Updated on 07-Sep-2023 17:12:53

988 Views

Maps are reference data types in Golang, which means that assigning one map variable to another creates a reference to the same underlying data structure. To create an independent copy of a map, we need to use different methods. In this article, we will explore two methods that include the manual method and the copy() function method to create a copy of map in golanguage. Syntax newMap := make(map[keyType]valueType) In this syntax, we first declare a new map named newMap using the make() function, specifying the appropriate key and value types. originalMap := map[string]int The syntax originalMap ... Read More

Golang Program to Delete items of Map

Akhil Sharma
Updated on 07-Sep-2023 17:11:09

5K+ Views

Maps in Go provide a convenient way to store and access data in form of key−value pairs. Deleting items from a map is a common operation when working with dynamic data. In this article, we will delete items of map in golanguage using three methods: the delete() function and iterating over the map, to remove items from a map, and creating a new map based on different conditions. Explanation Maps are versatile structures for holding key−value pairs, and managing these pairs is crucial when dealing with dynamic data. We'll use three approaches to removing items from a map, enhancing ... Read More

Golang Program to Create a Simple Map

Akhil Sharma
Updated on 07-Sep-2023 17:08:37

248 Views

Maps in Go are powerful data structures that allow us to store and retrieve key−value pairs. They are useful for various applications, such as storing and manipulating data, implementing dictionaries, and performing efficient lookups. In this article, we will write a program to create a simple map in golanguage using two methods that include the literal method and the make() function method. Algorithm Declare a map variable using the desired key and value types. Initialise the map using the literal notation by enclosing key−value pairs in curly braces {}. Assign values to the respective keys using the colon ':' ... Read More

Golang Program to Perform Slices Permutations of Numbers Entered by the User

Akhil Sharma
Updated on 07-Sep-2023 17:05:12

545 Views

A slice is a portion of data extracted from an array, list or a data structure. Permutations refer to the rearrangement of elements in a specific order.Here slice permutation means to generate all the possible permutations of the number entered by user. In this article, we will explore how to perform slices permutations of numbers entered by user in Golang, using two methods the recursive approach and the iterative approach, to generate all possible permutations of the given slice. Explanation Recursive: Our first trick is the generatePermutationsRecursive() function. It starts simple, handling small batches of numbers like a warm−up. ... Read More

Golang Program to Print all Permutations of a given String

Akhil Sharma
Updated on 07-Sep-2023 16:54:50

2K+ Views

A permutation is an arrangement of the characters of a string in a specific order. In some cases we need to print all the permutations of a string to create anagram games or puzzle games where users need to find out the letters hidden in the string. In this article we are going to print all permutations of a string in golanguage using two different approaches that include the recursive and the iterative approach. These methods allow us to generate all possible permutations of a given string, enabling various applications such as generating anagrams, solving permutation−based problems, and more. Explanation ... Read More

Golang Program to Print a Matrix in Spiral Format

Akhil Sharma
Updated on 07-Sep-2023 16:52:06

439 Views

To Print a matrix in spiral format we need to traverse the matrix in a spiral pattern, starting from the outermost layer and gradually moving inward. This approach provides a visually appealing way to display matrix elements. In this article we will use two methods to print a matrix in spiral format, the first method is by using the iterative approach and the other one is using the recursive approach. The examples below will help you understand these methods. Explanation Let us assume we have a 3 x 3 matrix, to print a matrix in spiral format we need to ... Read More

Golang Program to Perform the Comb Sort Algorithm

Akhil Sharma
Updated on 07-Sep-2023 17:52:48

191 Views

Comb sort algorithm is a simple and efficient comparison−based sorting algorithm, Comb Sort improves upon Bubble Sort by eliminating small values towards the end of the array, resulting in faster sorting times. We can use two methods to implement Comb Sort algorithm in Gzolang, the first method is using a naive approach and the other one is using an optimized approach in which we enhance the efficiency of the algorithm by introducing a flag variable to track swaps. In this article, we will discuss the principle of comb sort algorithm and provide the syntax for the program. Explanation The Comb ... Read More

Advertisements