Charmve Coding | Integer V lies strictly between integers U and W

Perface

Since last week, Apr 17. 2020, I had been starting to hold a Data Structs & Algorithms series training course. So I am going to make coding tests as a push study as much as possible, so that more excelent skills or methods can be relized and be meet your guys’ need.


Hereby, the following articles are all writen originally by myself, named Charmve Coding. This is a try for me to write contents completely in English, I hopefully wanna bring some special reading matters to you, my friends, to improve our English technolog reading ability.


To be honest, if those content appeals to you, a small click, themb-up, would make me happy. If you have any questions, feel free to reach out to me.

Good greeting to every Maiwei(邁微)members, I’m a FRIEND of yours, Charmve.

Today, we are going to explorle a few Codility test, which all have representativeness in Data Struct & Algorithms. At first, posting an interesting Codility test as usual, and then I will share different ways to solve this question. To be specific, it’s worth mentioning that a value-fulled summary will be shared to you.

Let’s begin! The test direction is followed.

Integer V lies strictly between integers U and W if U < V < W or if U > V > W.


A non empty zero indexed array A consisting of N integers is given.


A pair of indices (P, Q), where 0 <= P < Q < N, is said to have ‘adjacent values’. if no value in the array lies strictly between values A[P] and A[Q], and in addition A[P] != A[Q]


For example, in array A such that:
    A[0] = 0
    A[1] = 3
    A[2] = 3
    A[3] = 7
    A[4] = 5
    A[5] = 3
    A[6] = 11
    A[7] = 1


the following pairs of indices have adjacent values:
    (0,7), (1,4), (1,7)
    (2,4), (2,7), (3,4)
    (3,6), (4,5), (5,7)


For example, indices 4 and 5 have adjacent values because the values a[4] = 5 and A[5] = 3 are different , and there is no value in array A that lies strictly between them. the only such value could be the number 4, which is not present in the array.


Given two indices P and Q, their distance is defined as abs(P-Q), where abs(X) = X for X>=0 and abs(X) = -X for X<=0


For example the distance between indices 4 and 5 is 1 because abs(4-5) = abs(5-4) = 1.


Write a function that given a non-empty zero-indexed array A consisting of N integers,
returns the maximum distance between indices of this array that have adjacent values
The function should return -1 if no adjacent indices exist


For example given array A such that:
    A[0] = 1
    A[1] = 4
    A[2] = 7
    A[3] = 3
    A[4] = 3
    A[5] = 5

the function should return 4 because:

  • indices 0 and 4 are adjacent because A[0] != A[4]
  • and the array does not contain any value that lies strictly between A[0] = 1 and A[4] = 3
  • the distance between these indices is abd(0-4) = 4
  • no other pair of adjacent indices that has a larger distance exists

Assume that

  • N is an integer within the range [1 … 40,000]
  • each element of array A is an integer within the range [-2,147,483,648 to 2,147,483,647]

Sadly? wkwkwk…

Though this direction is so long, we may have diffcult to understand it, THE EXAMPLE is so important and easyly-understood that we just read it enough.

For example given array A such that:
    A[0] = 1
    A[1] = 4
    A[2] = 7
    A[3] = 3
    A[4] = 3
    A[5] = 5


the function should return 0 because:

  • indices 0 and 4 are adjacent because A[3] != A[4]
  • and the array does not contain any value that lies strictly between A[3] = 3 and A[4] = 3
  • the distance between these indices is abd(3-3) = 0
  • no other pair of adjacent indices that has a larger distance exists

Attention: Given two indices P and Q, their distance is defined as abs(P-Q), where abs(X) = X for X>=0 and abs(X) = -X for X<=0

OK, we got it now.

If we wanna find the smallest distance, we could find the smallest distance between neighbours in a sorted queue, which two elements are no other pair of adjacent indices. As this method can simplify this question, the process is above presented.

  1. sort the array;
  2. find the smallest distance

To be specific, there are no pair of adjacent indices that should return -2, such as sequence {1,2,3,4,5,6,7,8}.

Solution:

Given C++ version is referred:

// you can use includes, for example:
#include <algorithm>
#include <iostream>
#include <vector>
using namespace std;

// you can write to stdout for debugging purposes, e.g.
// cout << "this is a debug message" << endl;
class Solution
{
public:
	int solution(vector<int> &A)
	{
		int len = A.size();		
		sort(A,A+len);

		int min = 1000000;
		int flag = 1;
		for(int i = 0; i < len-1; i++)
		{
			if(A[i+1] - A[i] <= min)
			{
				min = A[i+1] - A[i];
				if(min == 1 && flag == 0)
				{
					flag = 1;
					return -2;
				}
				flag = 0;
			}
		}
		
		if(min > 100000000)
			return -1;
		else 
			return min;
	}
};

THANKS for your attention!

Worthly Recommended:
[1] Charmve Coding | the smallest positive integer that does not occur in Array A

[2] 數據結構與算法 | 你知道快速排序,那你知道它的衍生應用嗎?Partition函數

[3] 數據結構與算法 | 二分查找:劍指offer53 在排序數組中查找數字

[4] 數據結構與算法 | 數據結構中到底有多少種“樹”?一文告訴你


Follow my WeChat Office Account 邁微電子研發社,to get more exciting content. First published in the personal public account.
在這裏插入圖片描述

△Scan the QC to follow MaiweiE-com

Knowledge Planet (in Charge):The community aims to share the autumn / spring preparation strategies (including brush questions), face-to-face and internal referral opportunities, learning routes, knowledge question banks, etc.

在這裏插入圖片描述

△Scan the QC to entry the community of MaiweiE-com

在這裏插入圖片描述

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章