0
Explain me how to write code forthis problem in python?
Given integer array arr,count element x such that x+1 is also in arr If there are duplicates count separately
10 Respostas
+ 1
In python, elements stored in list as array of elements.
And list has lot of functions to operate on...
And for this 'in' function works...
(x in list) return True if x is in list..
In same way, you can check x+1 in list
count() function return how many times repeated a element.
Edit:
Ex:
lst=[2, 3, 2, 4, 5 , 2, 2]
print(lst.count(2))
print(5 in lst)
output :
4
True
+ 1
Pseudo code:
arr = [] #filled with elements
x = >element<
if arr.count(x) and arr.count(x) == 1 and (x+1) in arr:
<perform operation>
else:
<perform other operation>
+ 1
If arr =[1,2,3]
Output 2
Because if 1 is x x+1=2 so count =1
If x is 2 x+1 =3
Count 2
+ 1
Want code for above one
0
Tq
0
Tq
0
Expecting some more answers plz
0
Sample:
arr = [1,2,3]
x = 1
count = 0
while True:
if not (x+1 in arr):
break
count += 1
x += 1
print(count)
That should do it.
0
cnt = 0
for i in arr:
if (x == i) and (x+1 in arr):
cnt += 1
#replace x by element
0
LeetCode problem 🤔
Here is the solution..
def countElements(arr):
st=set(arr)
cnt=0
for x in arr:
If(x+1 in st):
cnt++
return cnt