Delete Array Element in Given Index Range in C++

AmitDiwan
Updated on 16-Jan-2021 06:58:22

567 Views

Let us first define the original array and the exclusive range for deletion of the array elements and also find the original array length −int arr[] = { 2, 4, 6, 8, 10, 12, 14, 16, 18, 20}; int L = 2, R = 6; int length = sizeof(arr) / sizeof(arr[0]);Now we loop in the array and if index position (i) is greater than L or R we increment the variable k which will be used to shift positions(delete) of array element once the index value (i) lies between the range L and R. Also, the new length of the ... Read More

Delete an Element from Array Using Two Traversals and One Traversal in C++

AmitDiwan
Updated on 16-Jan-2021 06:48:41

107 Views

Two Traversals Let us first define the original array and the element to be searched and deleted from the array −int ele = 5; int arr = [1,2,3,4];Now we loop in the array to find the given element −for (i=0; i

Check Equivalence of Two Strings in Python

Arnab Chakraborty
Updated on 16-Jan-2021 05:09:48

388 Views

Suppose we have two strings s and t of same size. We have to check whether s and t are equivalent or not. There are few conditions to check:They both are equal. Or, If we divide the s into two contiguous substrings of same size and the substrings are s1 and s2 and also divide t same like, s into t1 and t2, then one of the following should be valid:s1 is recursively equivalent to t1 and s2 is recursively equivalent to t2s1 is recursively equivalent to t2 and s2 is recursively equivalent to t1So, if the input is like ... Read More

Check if Two Strings are Anagram of Each Other in Python

Arnab Chakraborty
Updated on 16-Jan-2021 05:08:38

421 Views

Suppose we have two strings s and t we have to check whether they are anagram of each other or not.So, if the input is like s = "bite" t = "biet", then the output will be True as s ad t are made of same characters.To solve this, we will follow these steps −if size of s is not same as size of t, thenreturn Falsesort characters of s and treturn true if s is exactly same as t, otherwise falseLet us see the following implementation to get better understanding −Example CodeLive Demodef solve(s, t):    if len(s) != ... Read More

Check Validity of Triangle in Python

Arnab Chakraborty
Updated on 16-Jan-2021 05:06:08

905 Views

Suppose we have three sides. We have to check whether these three sides are forming a triangle or not.So, if the input is like sides = [14,20,10], then the output will be True as 20 < (10+14).To solve this, we will follow these steps −sort the list sidesif sum of first two sides

Check Vowels in Alphabetical Order in Python

Arnab Chakraborty
Updated on 16-Jan-2021 05:05:30

417 Views

Suppose we have a string s. We have to check whether the vowels present in s are in alphabetical order or not.So, if the input is like s = "helloyou", then the output will be True as vowels are e, o, o, u all are in alphabetical order.To solve this, we will follow these steps −character := character whose ASCII is 64for i in range 0 to size of s - 1, doif s[i] is any one of ('A', 'E', 'I', 'O', 'U', 'a', 'e', 'i', 'o', 'u'), thenif s[i] < character, thenreturn Falseotherwise, character := s[i]return TrueLet us see ... Read More

Check If Two Numbers Differ at One Bit Position in Python

Arnab Chakraborty
Updated on 16-Jan-2021 05:04:43

491 Views

Suppose we have two numbers x and y. We have to check whether these two numbers differ at one-bit position or not.So, if the input is like x = 25 y = 17, then the output will be True because x = 11001 in binary and y = 10001. Only one-bit position is different.To solve this, we will follow these steps −z = x XOR yif number of set bits in z is 1, thenreturn Truereturn FalseLet us see the following implementation to get better understanding −Example CodeLive Demodef bit_count(n):    count = 0    while n:       ... Read More

Check If Sum of Prime Elements in Array is Prime in Python

Arnab Chakraborty
Updated on 16-Jan-2021 05:03:01

174 Views

Suppose we have an array nums. We have to check whether the sum of all prime elements in the given array is also prime or notSo, if the input is like nums = [1, 2, 4, 5, 3, 3], then the output will be True as sum of all primes are (2+5+3+3) = 13 and 13 is also prime.To solve this, we will follow these steps −MAX := 10000sieve := a list of size MAX and fill with trueDefine a function generate_list_of_primes()sieve[0] := False, sieve[1] := Falsefor i in range 2 to MAX - 1, doif sieve[i] is true, thenfor ... Read More

Check If Sum of Absolute Difference of Adjacent Digits Is Prime in Python

Arnab Chakraborty
Updated on 16-Jan-2021 05:01:44

214 Views

Suppose we have a number n. We have to check whether the sum of the absolute difference of adjacent digit pairs is prime or not.So, if the input is like n = 574, then the output will be True as |5-7| + |7-4| = 5, this is prime.To solve this, we will follow these steps −num_str := n as stringtotal := 0for i in range 1 to size of num_str - 1, dototal := total + |digit at place num_str[i - 1] - digit at place num_str[i]|if total is prime, thenreturn Truereturn FalseLet us see the following implementation to get ... Read More

Check If Point Lies on a Given Line in Python

Arnab Chakraborty
Updated on 16-Jan-2021 05:00:59

2K+ Views

Suppose we have a straight line in the form y = mx + b, where m is slope and b is y-intercept. And have another coordinate point (x, y). We have to check whether this coordinate point lies on that straight line or not.So, if the input is like m = 3 b = 5 point = (6, 23), then the output will be True as if we put the given x and y coordinate values on the straight line equation then it will satisfy.To solve this, we will follow these steps −if y of point is same as (m ... Read More

Advertisements