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

7th Apr 2020, 3:48 PM
Sai Deepika Kathiri
Sai Deepika Kathiri - avatar
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
7th Apr 2020, 4:44 PM
Jayakrishna 🇮🇳
+ 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>
7th Apr 2020, 4:55 PM
Mensch
Mensch - avatar
+ 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
7th Apr 2020, 5:14 PM
Sai Deepika Kathiri
Sai Deepika Kathiri - avatar
+ 1
Want code for above one
7th Apr 2020, 5:14 PM
Sai Deepika Kathiri
Sai Deepika Kathiri - avatar
0
Tq
7th Apr 2020, 4:54 PM
Sai Deepika Kathiri
Sai Deepika Kathiri - avatar
0
Tq
7th Apr 2020, 4:58 PM
Sai Deepika Kathiri
Sai Deepika Kathiri - avatar
0
Expecting some more answers plz
7th Apr 2020, 4:58 PM
Sai Deepika Kathiri
Sai Deepika Kathiri - avatar
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.
7th Apr 2020, 5:22 PM
Mensch
Mensch - avatar
0
cnt = 0 for i in arr: if (x == i) and (x+1 in arr): cnt += 1 #replace x by element
8th Apr 2020, 5:31 PM
Varun Vaswani
Varun Vaswani - avatar
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
8th Apr 2020, 5:44 PM
Mukul
Mukul - avatar