Found 1860 Articles for Data Structure

Minimize the Absolute Difference of Sum of Two Subsets

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

652 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

Sum of an array using pthreads

Divya Sahni
Updated on 28-Sep-2023 15:20:41

1K+ Views

Pthreads is an execution model that helps use multiple processors to work at the same time for solving a problem. It is independent of the programming language. Problem Statement Given an array of integers. Find the sum of all the elements of the array using pthreads. Need for Multithreading for Calculating sum The problem is to add the elements in an array. Although it is a simple problem where a linear traversal of the array can do the work very easily with a time complexity of O(n) where n is the number of elements in the array. But if we ... Read More

Print numbers in the range 1 to n having bits in an alternate pattern

Divya Sahni
Updated on 28-Sep-2023 14:26:29

180 Views

Alternate bit pattern implies the positioning of 0’s and 1’s in a number at an alternate position i.e. no two 0s or 1’s are together. For example, 10 in binary representation is (1010)2 which has an alternate bit pattern as 0’s and 1’s are separated by each other. Problem Statement Given an integer, N. Find all the integers in the range 1 to N where the bit pattern of the integer is alternating. Example 1 Input: 10 Output: 1, 2, 5, 10 Explanation $\mathrm{(1)_{10} = (1)_2, (2)_{10} = (10)_2, (5)_{10} = (101)_2, (10)_{10} = (1010)_2}$ Example 2 Input: ... Read More

Jacobsthal and Jacobsthal-Lucas Numbers

Divya Sahni
Updated on 28-Sep-2023 14:19:17

391 Views

Jacobsthal Numbers Lucas sequence 𝑈𝑛(𝑃, 𝑄) where P = 1 and Q = -2 are called Jacobsthal numbers. The recurrence relation for Jacobsthal numbers is, $$\mathrm{𝐽_𝑛 = 0\: 𝑓𝑜𝑟 \: 𝑛 = 0}$$ $$\mathrm{𝐽_𝑛 = 1\: 𝑓𝑜𝑟 \: 𝑛 = 1}$$ $$\mathrm{𝐽_𝑛 = 𝐽_𝑛−1 + 2𝐽_{𝑛−2}\: 𝑓𝑜𝑟 \: 𝑛 > 1}$$ Following are the Jacobsthal numbers − 0, 1, 1, 3, 5, 11, 21, 43, 85, 171, 341, 683, 1365, …. Jacobsthal-Lucas Numbers Complementary Lucas sequence $\mathrm{𝑉_𝑛(𝑃, 𝑄)}$ where P = 1 and Q = -2 are called JacobsthalLucas numbers. The recurrence relation for Jacobsthal-Lucas numbers is, $\mathrm{𝐽_𝑛}$ = ... Read More

Increment a number by 1 by manipulating the bits

Divya Sahni
Updated on 28-Sep-2023 14:03:06

2K+ Views

Bit manipulation applies logical operations on a bit stream using bitwise operators like AND(&), OR(|), NOT(~), XOR(^), Left Shift() to get a required result. Using bitwise operators is beneficial as we can manipulate individual bits and they are faster than other operators. Problem Statement Given a number. Increment or add the number by 1 using bitwise operators only. (Don’t use arithmetic operators like ‘+’ , ‘-’, ‘*’ or’/’ ) Approach 1: Using One’s Complement / NOT Operator Bitwise complement / One’s complement is implemented using the NOT(~) Operator. For a number n, a bitwise complement of n i.e. ~n = ... Read More

Sum of Fourth Powers of first N natural numbers

Divya Sahni
Updated on 28-Sep-2023 12:07:35

1K+ Views

The fourth power of a number x is x raised to the power 4 or x4. Natural numbers are all positive integers excluding zero. Thus, the sum of the fourth powers of the first N natural numbers is − $\mathrm{Sum = 1^4 + 2^4 + 3^4 + 4^4 + … + N^4}$ This article describes some approaches for finding the sum using minimum time and space complexity. Problem Statement Given the number N, find the sum $\mathrm{1^4 + 2^4 + 3^4 + 4^4 + … + N^4}$. Example 1 Input: 3 Output: 98 Explanation $\mathrm{Sum = 1^4 + ... Read More

Numbers having difference with digit sum more than s

Prabhdeep Singh
Updated on 01-Sep-2023 10:04:48

276 Views

The digit sum for a given number is the sum of all the digits present in the given number. We will be given a number n and s and we have to find all the numbers which are in the range 1 to n and have the difference between the number and the sum of its digits greater than s. We will implement two approaches with the code and the discussion on time and space complexity. Input N = 15, S = 5 Output 6 Explanation For all the numbers in the range 0 to 9, the difference ... Read More

Sort 1 to N by swapping adjacent elements

Prabhdeep Singh
Updated on 01-Sep-2023 10:15:24

399 Views

An array is a linear data structure that stores the elements and a sorted array contains all the elements in increasing order. Sorting an array by swapping the adjacent elements means we can swap the adjacent elements any number of times and we have to sort the array. We will be given two array’s first array is the array to be sorted and another array is a Boolean array that represents whether the current element is swappable or not. If the given array is of the length N then all the elements present will be from 1 to N. ... Read More

Back-Face Detection Method

Prabhdeep Singh
Updated on 01-Sep-2023 09:54:53

7K+ Views

Programming languages are used for many purposes such as making websites, developing mobile applications, etc. Graphic designing is one of the things that we can do by using the programming language. While graphic designing, we can face a problem where we have to project a 3-D object on the 2-D plane and due to which one dimension will be reduced or one face will be hidden. In this problem, we have to detect that hidden face. Back-Face detection is also known as the name Plane Equation method, and it is a method for the object space methods in which objects ... Read More

Largest number in [2, 3, .. n] which is co-prime with numbers in [2, 3, .. m]

Prabhdeep Singh
Updated on 01-Sep-2023 09:49:52

152 Views

Co-prime numbers are numbers that do not have any common divisor other than 1. We will be given two numbers n and m. We have to find the largest number in the range of 2 to n (both inclusive) which is co-prime with all the elements in the range of 2 to m (both inclusive). If none of the elements in the given range is co-prime with all the elements of the second range then we have to return or print -1. We will implement the approach, and code, and will discuss the time and space complexity of the program. ... Read More

Advertisements