Absolute Difference of all pairwise consecutive elements in an arrayGiven an array of integers of N elements. The task is to print the absolute difference of all of the pairwise consecutive elements. Show Recommended: Please try your approach on {IDE} first, before moving on to the solution. Approach: The solution is to traverse the array and calculate and print the absolute difference of every pair (arr[i], arr[i+1]). C++
Java
Python 3
C#
PHP
Javascript
Output:
6 5 10 1
Time complexity : O(n)
Article Tags :
Arrays Mathematical School Programming Technical Scripter
Technical Scripter 2018 Practice Tags :
Arrays Mathematical Count consecutive pairs of same elementsGiven an array arr[], the task is to count the number of pairs formed by consecutive elements in which both of the elements in a pair are same.
Recommended: Please try your approach on {IDE} first, before moving on to the solution. Approach: Initialize count = 0 and traverse the array from arr[0] to arr[n – 2]. If the current element is equal to the next element in the array then increment the count. Print the count in the end. C++
Java
Python3
C#
PHP
Javascript
Output:
5
Article Tags :
Arrays Mathematical
Constructive Algorithms Practice Tags :
Arrays Mathematical Check for pair in an array with a given sum - Interview ProblemDifficulty Level : Medium Asked in : Google, Facebook, Amazon Understanding the problemProblem Description: Given an array of n integers and given a number K, determines whether there is a pair of elements in the array that sums to exactly K. For example : Input : A[] = [-5, 1, -40, 20, 6, 8, 7 ], K=15 Output: true ( 7, 8 and -5, 20 are the pairs with sum 15) Input : A[] = [-5, 4, -2, 16, 8, 9], K=15 Output: false (There is no pair of elements whose sum is equal to 15) Possible follow-up questions to ask the interviewer :
Designing efficient solutionsWe are discussing four ways to solve this problem :
1. Brute Force Approach: Using two loopsUse two loops and check A[i] + A[j] == K for each pair (i, j) in A[]. If there exists a pair with sum equals to K then return true. By end of both loops, If you didn’t find such a pair then return false. Pseudo Codeint find_sumPair (A[], n, K) { for( i = 0 to n-1 ) { for(j = i+1 to n-1) { if(A[i]+A[j] == K) return 1 } } return -1 } Complexity Analysis The total no. of comparison in worst case = Total no. of possible pairs = nC2 = n(n-1)/2 = O(n²) Time Complexity = O(n²) and Space Complexity = O(1). Can we improve the time complexity? Let's try to think about it. 2. Sorting and Binary SearchThe idea of binary search works perfectly in the case of the sorted array. We can sort the A[] and for each value A[i], search whether there is another value K-A[i] present in the array or not. Binary search performs searching in O(logn) which could help us to improve the time complexity. Solution Steps
Pseudo Codeint find_sumPair (A[], n, K) { Sort ( A, n) for( i = 0 to n-1 ) { x = binarySearch ( A, 0, n-1, K-A[i] ) if ( x ) return 1 } return -1 } Complexity Analysis Suppose we are using heap sort/merge sort and iterative binary search for the implementation. Time Complexity = Time complexity of sorting + n. Time complexity of Binary Search = O(nlogn) + n. O(logn) = O(nlogn) Space Complexity = Space Complexity of sorting + Space complexity of the binary search (Iterative Implementation) If merge sort ,Space Complexity = O(n) + O(1) = O(n) If Heap Sort, Space Complexity = O(1) + O(1) = O(1) Critical Ideas to think!
3. Sorting and two Pointer approachSort the array A[] then walk two pointers inward from the ends of the array, at each point looking at their sum. If it is exactly k, then we are done. If it exceeds k, then any sum using the larger element is too large, so we walk that pointer inwards. If it is less than k, then any sum using the lower element is too small, so we walk that pointer inwards. Solution Steps
3) Run a loop while i< j.
4) If didn’t find such a pair in the whole array - return false. Solution Visualization Pseudo Codeint find_sumPair( A[], n, K) { Sort(A, n) i = 0 j = n -1 while (i < j) { if(A[i] + A[j] == K) return 1 else if(A[i] + A[j] < K) i = i+1 else j = j-1 } return -1 } Complexity Analysis Suppose we are using Heap Sort or merge sort. Time Complexity = Time complexity of sorting + Time complexity of two pointer approach (while loop) = O (n log n) + O(n) = O (n log n) If merge sort, Space Complexity = O(n) If Heap Sort, Space Complexity = O(1) Critical Ideas to think!
4. Using a Hash TableIdea is to use the Hash Table to improve the time complexity because it performs searching and insertion efficiently in O(1) average. Solution Steps
3. If you didn’t find such a pair by end of the loop then return false. Pseudo Codebool checkPair(int A[], int n, int K) { Take Hash Table H of size O(n) for (i = 0 to n-1) { int x = K - A[i] if (H.search(x) is true) return 1 H.insert(A[i]) } return -1 } Complexity Analysis In worst case scenario, we scan the whole array and didn't find any such pair. Time Complexity = Time complexity of inserting n elements in hash table + Time complexity of searching (K-A[i]) n times in hash table = n. O(1) + n . O(1) = O(n) Space Complexity = Extra space of Hash Table = O(n) Critical Ideas to think!
Comparison of the different solutionsSimilar Problems to solve
Please write comments if you find any error or bug. Happy Coding! Enjoy Algorithms! AfterAcademy Data Structure And Algorithms Online Course - Admissions Open |