Programming Articles - Page 2117 of 3366

Find First and Last Position of Element in Sorted Array in Python

Arnab Chakraborty
Updated on 27-Apr-2020 12:59:33

1K+ Views

Suppose we have an array of integers A. This is sorted in ascending order, we have to find the starting and ending position of a given target value. When the target is not found in the array, return [-1, -1]. So if the array is like [2, 2, 2, 3, 4, 4, 4, 4, 5, 5, 6], and target is 4, then the output will be [4, 7]To solve this, we will follow these steps −Initially res := [-1, -1], set low := 0, high := length of array Awhile low < highmid := low + (high – low)/2if A[mid] ... Read More

Search in Rotated Sorted Array in Python

Arnab Chakraborty
Updated on 27-Apr-2020 12:55:30

1K+ Views

Consider we have an array sorted in ascending order, and that is rotated at some pivot unknown to you beforehand. For example, [0, 1, 2, 4, 5, 6, 7] might become [4, 5, 6, 7, 0, 1, 2]. We have given a target value to the search. If we can get it in the array, then return its index, otherwise return -1. We can assume no duplicate exists in the array. So if the array is like [4, 5, 6, 7, 0, 1, 2], then the output will be 4. as the index of this element is present at index ... Read More

Divide Two Integers in C++

Arnab Chakraborty
Updated on 27-Apr-2020 12:51:59

2K+ Views

Suppose we have two integers dividend and divisor. We have to divide two integers without using multiplication, division, and mod operator. Return the quotient after dividing the dividend by divisor. The integer division should truncate toward zero. Both of the inputs are integersSo if the given inputs are dividend = 7, divisor = -3, then output will be -2.To solve this, we will follow these steps −Taking two arguments x and y it indicates x divides yif x < -Infinity and y = 1, then return infinitya := |x|, b := |y| and ans := 0while a – b >= ... Read More

Swap Nodes in Pairs in C++

Arnab Chakraborty
Updated on 27-Apr-2020 12:39:29

860 Views

Consider we have a linked list. We have to swap every two adjacent nodes and return its head. The constraint is that we cannot modify the value of the nodes, only the node itself can be changed. So if the list is like [1, 2, 3, 4], then the resultant list will be [2, 1, 4, 3]To solve this, we will follow these steps −if head is not present, then return headfirst := head, second := next of head, dummy is one new node with value -1next of dummy := first, and prev := dummywhile second is not nulltemp := ... Read More

Generate Parentheses in Python

Arnab Chakraborty
Updated on 27-Apr-2020 12:34:31

3K+ Views

Suppose we have a value n. We have to generate all possible well-formed parentheses where n number of opening and closing parentheses are present. So if the value of n = 3, then the parentheses set will be ["()()()", "()(())", "(())()", "(()())", "((()))"]To solve this, we will follow these steps −Define method called genParenthesisRec(). This takes left, right, temp string and result array. initially result array is emptyThe function genParenthesisRec, will work like belowif left = 0 and right := 0, then insert temp into result, and returnif left > 0getParenthesisRec(left – 1, right, temp + “(”, result)if right > ... Read More

Remove Nth Node From End of List in Python

Arnab Chakraborty
Updated on 27-Apr-2020 12:27:41

709 Views

Suppose we have a linked list. We have to remove the Nth node from the end of the list, then return its head. So if the list is like [1, 2, 3, 4, 5, 6] and n = 3, then the returned list will be [1, 2, 3, 5, 6].To solve this, we will follow these steps −If there is no node after head, then return Nonefront := head, back := head, counter := 0 and fount := falsewhile counter

4Sum in C++

Akansha Kumari
Updated on 23-Jul-2025 15:44:35

622 Views

4SUM ProblemThe 4Sum is one of the problem variations in which we need to find the number of quadruplets present in the array such that their sum is equal to the given target. We are given an array of n integers called nums and a target value, and we have to find the number of index quadruplets (i, j, k, l) where i, j, k, and l are all indices in the range 0 to n - 1, such that they satisfy the condition: nums[i] + nums[j] + nums[k] + nums[l] = target Scenario 1 Inputs: arr= [-1, 0, 1, 2, ... Read More

Iswctype() function in C++ STL

Sunidhi Bansal
Updated on 30-Jan-2020 09:50:37

99 Views

In C++ standard template libraray(STL), iswctype() function is used to check if the given wide character has property specified by desc.Iswctype() is an in built function whose header file is “ctype.h”.Syntax of Iswctype() is as followsint iswctype(wint_t c, wctype_t desc); iswctype () / Checks whether whether c has the property specified by desc. /Synopsisint iswctype(wint_t c, wctype_t desc);ParametersC − To check the wide characters which are casted to the integral type wint_tDesc − It is a value which is returned by the call to wctype, which is a scalar type which is used as return type for wctype(Wide character type).Return ... Read More

iswlower() function in C++ STL

Sunidhi Bansal
Updated on 27-Feb-2020 05:35:46

143 Views

In C++ standard template library(STL), iswlower() function is used to check if the given wide character is in lowercase or not, if not then the function will return a zero value. The characters with ASCII value from 97 to 122 i.e. a-z are the lowercase alphabetic letters. Iswlower() function is present in cctype header file in C/C++.Syntax of iswlower () is as followsint iswlower (wint_t c)Parameters − c is a wide character to be checked, casted to a wint_t, or WEOF where wint_t is an integral type.Return Value − islower() function return non-zero value when the string is in lowercase ... Read More

iswdigit() function in C++ STL

Sunidhi Bansal
Updated on 30-Jan-2020 09:44:13

155 Views

In C++ STL, iswdigit() function is a built-in function that is used to check if the given wide character is a decimal digit character or some other character. This function is present in a cwctype header file in C/C++.What are the decimal digit characters?Decimal digit character are the numeric values that starts from 0 i.e. 0, 1, 2, 3, 4, 5, 6, 7, 8, 9 .Syntax of iswcntrl() function is as followsint iswdigit() (wint_t c)Parameters − c is a wide character to be checked, casted to a wint_t, or WEOF where wint_t is an integral type.Return Value − A value ... Read More

Advertisements