2006 Count Number of Pairs With Absolute Difference K | Zero to FAANG Kunal | Leetcode | Shapnesh
Vložit
- čas přidán 23. 09. 2021
- 2006 Count Number of Pairs With Absolute Difference K | Zero to FAANG Kunal | Leetcode | Shapnesh
Problem link :
leetcode.com/problems/count-n...
Course link:
github.com/kunal-kushwaha/DSA...
Complete Java + DSA + Interview Preparation + CP Course
• Java + DSA + Interview...
Hi, thanks for watching our video about Arrays in Java
In this video we’ll walk you through:
- Brute Force
- Efficient approach
- Solution Code
TIMESTAMPS
Intro
Explanation begins
IDE solution
ABOUT OUR CHANNEL
Our channel is all about Programming. We cover lots of cool stuff such as solution to problems, concept explanation and tricks to master CP
Check out our channel here:
/ @programmerszone
Don’t forget to subscribe!
CHECK OUT OUR OTHER Playlists
GeeksforGeeks playlist:
• GeeksforGeeks problems
Complete C++ Course playlist:
• Complete C++ playlist
CodeChef Playlist:
• CodeChef Problems
HackerRank playlist:
• HackeRank problems
LeetCode problems
• Leetcode problems Solu...
FIND US AT
www.waadanibhao.wordpress.com
GET IN TOUCH
Contact us on hopesalivenow@gmail.com
2006. Count Number of Pairs With Absolute Difference K
Easy
72
0
Add to List
Share
Given an integer array nums and an integer k, return the number of pairs (i, j) where i j such that |nums[i] - nums[j]| == k.
The value of |x| is defined as:
x if x = 0.
-x if x 0.
Example 1:
Input: nums = [1,2,2,1], k = 1
Output: 4
Explanation: The pairs with an absolute difference of 1 are:
- [1,2,2,1]
- [1,2,2,1]
- [1,2,2,1]
- [1,2,2,1]
Example 2:
Input: nums = [1,3], k = 3
Output: 0
Explanation: There are no pairs with an absolute difference of 3.
Example 3:
Input: nums = [3,2,1,5,4], k = 2
Output: 3
Explanation: The pairs with an absolute difference of 2 are:
- [3,2,1,5,4]
- [3,2,1,5,4]
- [3,2,1,5,4]
Constraints:
1 = nums.length = 200
1 = nums[i] = 100
1 = k = 99
Accepted
9,606
Submissions
11,228
#codechef #coding #python #hackerrank #programming #java #computerscience #coder #code #competitiveprogramming #machinelearning #pythonprogramming #algorithms #cpp #javascript #hackerearth #webdeveloper #geeksforgeeks #codinglife #leetcode #c #codeforces #engineering #codingmemes #programmer #hacker #programmingmemes #topcoder #webdevelopment #bhfyp #DSAwithKunal
BruteForce approach will have time complexity O(N^2) as number of operations performed is n * ( n - 1) ~ n^2
Thank you
Welcome :)
For brute force approach Time complexity will be O(n^2)
Array size has to be 101 bcz the largest element possible in that array is 100 so array size have to be 1 more than largest element
Yepp so that we can directly store count of 100 in 100th index else we will have to subtract 1 everytime to store that numbers count into our array
@@ProgrammersZone exactly
How to come up with Counting sort approach why ans+=arr[i]*arr[i-k]
Simply just try out 15-20 examples and soon you'll observe all this also if you aren't able to just start learning from discuss section from leetcode soon you'll get all this :)
ans+=arr[i]*arr[i-k]
could you explain this part?