Categories for

HackerRank solutions

HackerRank NEW YEAR CHAOS Constructive Algorithm Challenge [SOLVED]

NEW YEAR GIFT !!! IT IS ALSO SOLVED IN JAVA !!!!

    static void minimumBribes(int[] q) {
        boolean sorted = false;
        int bribes = 0;
        if (q.length < 2) 
        System.out.println(bribes);
        while (!sorted) {
            sorted = true;
            for (int i = 0; i < q.length - 1; i++) {
                if (q[i] - i > 3) {
                    System.out.println("Too chaotic");
                    return;
                }
                if (q[i] > q[i+1]) {
                    sorted = false;
                    q = swap(q, i, i+1);
                    bribes++;
                }
            }
        }
        System.out.println(bribes);
    }
        private static int[] swap(int[] q, int i, int j) {
        int tmp = q[i];
        q[i] = q[j];
        q[j] = tmp;
        return q;
    }

Read more...

The Pads HackerRank Advanced SQL Questions [Solved]

Stuck with Advanced Select problem in SQL?
https://www.hackerrank.com/challenges/the-pads/problem

NO WORRIES! HERE IS THE SOLUTION!

SELECT CONCAT(NAME,'(',SUBSTR(OCCUPATION,1,1),')') AS N
 FROM OCCUPATIONS
 ORDER BY N;
 SELECT CONCAT('There are a total of ',COUNT(OCCUPATION),' ',LOWER(OCCUPATION),'s.')
 FROM OCCUPATIONS
 GROUP BY OCCUPATION
 ORDER BY COUNT(OCCUPATION), OCCUPATION;

Read more...

HackerRank Minimum Swaps 2 Problem [Solved]

https://www.hackerrank.com/challenges/minimum-swaps-2/problem No worries! It is as simple as HELL!!

    static int minimumSwaps(int[] arr) {
            int swapCount = 0;
            for (int index = 0; index < arr.Length - 1; index++)
            {
                if (arr[index] != (index + 1))
                {
                    for (int swapIndex = index + 1; swapIndex < arr.Length; swapIndex++)
                    {
                        if (arr[swapIndex] == (index + 1))
                        {
                            int aux = arr[swapIndex];
                            arr[swapIndex] = arr[index];
                            arr[index] = aux;
                            swapCount++;
                            break;
                        }
                    }
                }
            }
            return swapCount;   
}

Read more...

HackerRank Solutions Grading Students Problem [SOLVED]

It is as simple as HELL!!

https://www.hackerrank.com/challenges/grading/problem

SO EASY!!

    public static List<int> gradingStudents(List<int> grades)
    {
         for (int i = 0; i < grades.Count; i++)
        {
            var item = grades[i];
            if (item >= 38)
            {
                var diff = 5 - (item % 5);
                if (diff < 3)
                    grades[i] = item + diff;
            }
        }
      return grades;
    }

Read more...

HackerRank Solutions Special String Again Problem [Solved]

CONGRATS SPECIAL STRING AGAIN PROBLEM SOLVED !!!

using System.CodeDom.Compiler;
using System.Collections.Generic;
using System.Collections;
using System.ComponentModel;
using System.Diagnostics.CodeAnalysis;
using System.Globalization;
using System.IO;
using System.Linq;
using System.Reflection;
using System.Runtime.Serialization;
using System.Text.RegularExpressions;
using System.Text;
using System;

class Solution {

    // Complete the substrCount function below.
    static long substrCount(int n, string str) {
          long result = 0L;  
   int[] sameChar = new int[n]; 
    for(int v = 0; v < n; v++) 
    sameChar[v] = 0;   
    int i = 0;  
    // traverse string character 
    // from left to right 
    while (i < n)  
    {  
        // store same character count 
        int sameCharCount = 1; 
        int j = i + 1; 
        // count smiler character 
        while (j < n && str[i] == str[j]) 
        { 
            sameCharCount++; 
            j++; 
        }  
        // Case : 1 
        // so total number of  
        // substring that we can 
        // generate are : K *( K + 1 ) / 2 
        // here K is sameCharCount 
        result += (sameCharCount *  
                  (sameCharCount + 1) / 2); 
  
        // store current same char  
        // count in sameChar[] array 
        sameChar[i] = sameCharCount;  
        // increment i 
        i = j; 
    } 
  
    // Case 2: Count all odd length 
    //         Special Palindromic 
    //         substring 
    for (int j = 1; j < n; j++) 
    { 
        // if current character is  
        // equal to previous one  
        // then we assign Previous  
        // same character count to 
        // current one 
        if (str[j] == str[j - 1]) 
         sameChar[j] = sameChar[j - 1];  
        // case 2: odd length 
if(j > 0 && j < (n - 1) && (str[j - 1] == str[j + 1] && str[j] != str[j - 1])) 
             result += Math.Min(sameChar[j - 1], 
                              sameChar[j + 1]); 
    } 
     return result;
    }

    static void Main(string[] args) {
        TextWriter textWriter = new StreamWriter(@System.Environment.GetEnvironmentVariable("OUTPUT_PATH"), true);

        int n = Convert.ToInt32(Console.ReadLine());
        string s = Console.ReadLine();
        long result = substrCount(n, s);
        textWriter.WriteLine(result);
        textWriter.Flush();
        textWriter.Close();
    }
}

Read more...