Minimum Distances HackerRank Solution in C, C++, Java, Python

Function Description

Complete the beautifulTriplets function in the editor below. It must return an integer that represents the number of beautiful triplets in the sequence.

beautifulTriplets has the following parameters:

d: an integer
arr: an array of integers, sorted ascending
Input Format

The first line contains space-separated integers and , the length of the sequence and the beautiful difference.
The second line contains space-separated integers .

Output Format

Print a single line denoting the number of beautiful triplets in the sequence.

Sample Input

7 3
1 2 4 5 7 8 10

Sample Output

3

Minimum Distances HackerRank Solution in C#

using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
class Solution {

    static void Main(String[] args) {
        int n = Convert.ToInt32(Console.ReadLine());
        string[] A_temp = Console.ReadLine().Split(' ');
        int[] A = Array.ConvertAll(A_temp,Int32.Parse);
        
        int min = int.MaxValue;
        for(int i=0;i<n-1;i++){
            for(int j=i+1;j<n;j++){
                if(A[i]==A[j]){
                    int d = j-i;
                    if(d<min)min=d;
                }
            }
        }
        if(min==int.MaxValue) Console.WriteLine(-1);
        else Console.WriteLine(min);
    }
}

 

Attempt Minimum Distances HackerRank Challenge

Link – https://www.hackerrank.com/challenges/minimum-distances/

Next HackerRank Challenge Solution 

Link – https://exploringbits.com/halloween-sale-hackerrank-solution/

Leave a Comment