Narendra Kumar

Narendra Kumar

180 Articles Published

Articles by Narendra Kumar

Page 18 of 18

Mersenne Prime Number in C++.

Narendra Kumar
Narendra Kumar
Updated on 31-Oct-2019 763 Views

DescriptionIn mathematics, a Mersenne prime is a prime number that is one less than a power of two. That is, it is a prime number of the form Mn = 2n − 1 for some integer n.Write a C++ program to print all Mersenne Primes smaller than an input positive integer n.The exponents n which give Mersenne primes are 2, 3, 5, 7, ... and the resulting Mersenne primes are 3, 7, 31, 127Algorithm1. Generate all the primes less than or equal to the given number n 2. Iterate through all numbers of the form 2n-1 and check if they ...

Read More

Minimize the total number of teddies to be distributed in C++

Narendra Kumar
Narendra Kumar
Updated on 22-Oct-2019 191 Views

Problem statementGiven N number of students and an array which represent the mark obtained by students. School has dicided to give them teddy as a price. Hoever, school wants to save money, so they to minimize the total number of teddies to be distrubuted by imposing following constrain −All students must get atleast one teddyIf two students are sitting next to each other then student with the higher marks must get moreIf two students have same marks then they are allowed to get different number of teddiesExampleLet us suppose there are 3 students and marks obtained are represented in array ...

Read More

Minimize the sum of squares of sum of N/2 paired formed by N numbers in C++

Narendra Kumar
Narendra Kumar
Updated on 22-Oct-2019 191 Views

Problem statement Given an array of n elements. The task is to create n/2 pairs in such a way that sum of squares of n/2 pairs is minimal. Example If given array is − arr[] = {5, 10, 7, 4} then minimum sum of squares is 340 if we create pairs as (4, 10) and ( 5, 7) Algorithm 1. Sort the array 2. Take two variables which point to start and end index of an array 3. Calulate sum as follows: sum = arr[start] + arr[end]; sum = sum * sum; ...

Read More

Minimization of ER Diagram

Narendra Kumar
Narendra Kumar
Updated on 22-Oct-2019 2K+ Views

Problem statementER diagram is pictorial representation of shows various tables and relations amongst them. ER diagram we can reduce the number of database.One to one cardinalityLet us consider below diagram with one to one cardinality −Above ER diagram represents 3 entities −Employee entity has 2 attributes namely emp_name. emp_id is the primary keyCompany entity has 2 attributes namely cmp_name. cmp_id is the primary keyPrimary key of Work entity can be emp_id or cmp_idWe cannot combine 3 tables into single one can either merge Work into Employee or Company. minimum 2 tables are required in one to one cardinality scenario.One to ...

Read More

Minimax Algorithm in Game Theory (Alpha-Beta Pruning) in C++

Narendra Kumar
Narendra Kumar
Updated on 22-Oct-2019 3K+ Views

Description Alpha-Beta pruning is a optimization technique used in minimax algorithm. The idea behind this algorithm is cut off the branches of game tree which need not to be evaluated as better move exists already. This algorithm introduces two new fields − Alpha − This is best value(maximum) that maximizer player can guarantee at current level or its above level Beta − This is the best value(minimum) that minimizer player can guarantee at the current level or its above level. Example If game tree is − arr [] = {13, 8, 24, -5, 23, 15, -14, -20} ...

Read More

Minimum flips required to maximize a number with k set bits in C++.

Narendra Kumar
Narendra Kumar
Updated on 23-Sep-2019 241 Views

Problem statementGiven two numbers n and k, we need to find the minimum number of flips required to maximize given number by flipping its bits such that the resulting number has exactly k set bits. Please note input must satify condition that k < number of bits in n.ExampleLet us suppose n = 9 and k = 2Binary representation of 9 is − 1001. It contains 4 bits.Largest 4 digit binary number with 2 set bits is − 1100 i.e. 12To convert 1001 to 1100 we have to flip highlighed 2 bitsAlgorithm1. Count the number of bits in n. Let ...

Read More

Minimum flips in two binary arrays so that their XOR is equal to another array in C++.

Narendra Kumar
Narendra Kumar
Updated on 23-Sep-2019 265 Views

Problem statementGiven three arrays with 0’s and 1’s of size n, the task is to find minimum flip of bits in the first and second array such that the XOR of i’th index bit of first and second array is equal to i’th index bit of the third array.Please note that we can only flip at most p bits of array 1 and at most q bits of array 2. Also rearranging array elements is not allowed.Let us suppose p = 2 and q = 5arr1[] = {1, 0, 1, 1, 0, 1, 0} arr2[] = {0, 1, 0, 1, ...

Read More

Minimum element in a max heap in C++.

Narendra Kumar
Narendra Kumar
Updated on 23-Sep-2019 462 Views

Problem statementFind the element with the least value in max heap.Let us consider below max heap.In max heap value of the root node is always greater than its childer. Because of this property, we can conclude that value will be present in one of the leaf nodes. If heap contains n nodes then there will be ceil(n/2) leaves.Max heap is a complete binary tree hence it can be represented in an array. In such heap the first leaf will be present after floor(n/2) index. So in our example, the first leave will be present at index 5.AlgorithmWe can use the ...

Read More

Minimum delete operations to make all elements of array same in C++.

Narendra Kumar
Narendra Kumar
Updated on 23-Sep-2019 267 Views

Problem statementGiven an array of n elements such that elements may repeat. We can delete any number of elements from array. The task is to find a minimum number of elements to be deleted from array to make it equal.arr[] = {10, 8, 10, 7, 10, -1, -4, 12}We have to delete highlighted 5 elements to make all array elements the same.Algorithm1. Count frequency of each element 2. Find maximum frequecy among the frequencies. Let us call this as maxFrequncy 3. Elements to be deleted: n – maxFrequecy where n is size of an arrayExample#include #include #include ...

Read More

Minimum and Maximum Prime Numbers of a Singly Linked List in C++.

Narendra Kumar
Narendra Kumar
Updated on 23-Sep-2019 230 Views

Problem statementGiven a linked list of n positive integers. We have to find the prime number with minimum and maximum value.If the given list is −10 -> 4 -> 1 -> 12 -> 13 -> 7 -> 6 -> 2 -> 27 -> 33 then minimum prime number is 2 and maximum prime number is 13Algorithm1. Find maximum number from given number. Let us call it maxNumber 2. Generate prime numbers from 1 to maxNumber and store them in a dynamic array 3. Iterate linked list and use dynamic array to find prime number with minimum and maximum valueExample#include ...

Read More
Showing 171–180 of 180 articles
« Prev 1 14 15 16 17 18 Next »
Advertisements