0% found this document useful (0 votes)
79 views50 pages

Datastrures CC

The document contains solutions to several coding challenges in Java, including algorithms for rearranging an array in a meandering pattern, counting distinct digit numbers within a range, checking if two strings are twin strings, and determining if it is possible to reach a target pair of numbers from a starting pair through addition. Solutions utilize techniques like sorting, recursion with memoization, and traversing arrays or strings to solve the given problems in an optimal way. A variety of data structures like arrays, lists, and hashmaps are used.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
79 views50 pages

Datastrures CC

The document contains solutions to several coding challenges in Java, including algorithms for rearranging an array in a meandering pattern, counting distinct digit numbers within a range, checking if two strings are twin strings, and determining if it is possible to reach a target pair of numbers from a starting pair through addition. Solutions utilize techniques like sorting, recursion with memoization, and traversing arrays or strings to solve the given problems in an optimal way. A variety of data structures like arrays, lists, and hashmaps are used.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 50

https://allhackerranksolutionsbykaira.blogspot.

com/search/label/HackerRank

https://awesomeopensource.com/project/Java-aid/Hackerrank-Solutions#data-structures

MEANDERING ARRAY

// Java program to print the array in given order

import java.util.Arrays;

public class GFG {

// Function which arrange the array.

static void rearrangeArray(int arr[], int n)

// Sorting the array elements

Arrays.sort(arr);

int[] tempArr = new int[n]; // To store modified array

// Adding numbers from sorted array to

// new array accordingly

int ArrIndex = 0;

// Traverse from begin and end simultaneously

for (int i = 0, j = n-1; i <= n / 2 || j > n / 2;

i++, j--) {

if(ArrIndex < n)

tempArr[ArrIndex] = arr[i];

ArrIndex++;

if(ArrIndex < n)

tempArr[ArrIndex] = arr[j];

ArrIndex++;

}
}

// Modifying original array

for (int i = 0; i < n; i++)

arr[i] = tempArr[i];

// Driver Code

public static void main(String args[])

int arr[] = { 5, 8, 1, 4, 2, 9, 3, 7, 6 };

int n = arr.length;

rearrangeArray(arr, n);

for (int i = 0; i < n; i++)

System.out.print(arr[i]+" ");

DISTINCT DIGIT NUMBER

// Java implementation of brute

// force solution.

import java.util.LinkedHashSet;

class GFG

// Function to check if the given

// number has repeated digit or not

static int repeated_digit(int n)

LinkedHashSet<Integer> s = new LinkedHashSet<>();

// Traversing through each digit

while (n != 0)

int d = n % 10;
// if the digit is present

// more than once in the

// number

if (s.contains(d))

// return 0 if the number

// has repeated digit

return 0;

s.add(d);

n = n / 10;

// return 1 if the number has

// no repeated digit

return 1;

// Function to find total number

// in the given range which has

// no repeated digit

static int calculate(int L, int R)

int answer = 0;

// Traversing through the range

for (int i = L; i < R + 1; ++i)

// Add 1 to the answer if i has

// no repeated digit else 0

answer = answer + repeated_digit(i);

return answer;

}
// Driver Code

public static void main(String[] args)

int L = 80, R = 120;

// Calling the calculate

System.out.println(calculate(L, R));

TWIN STRING PROBLEM

import java.io.IOException;
import java.util.Scanner;

public class TwinStringProblem {


//creating method for checking two string
static boolean[] twins(String[] a, String[] b) {
boolean[] result = new boolean[a.length];

for (int i = 0; i < a.length; i++) {


String aVal = a[i].toLowerCase();
String bVal = b[i].toLowerCase();
String[] aValArray = aVal.split("");
String[] bValArray = bVal.split("");

for (String s : aValArray) {


for (int index = 0; index < aValArray.length; index++) {
if (bValArray[index].equals(s)) { // checking whether the
index match or not
if ((s.indexOf(s) % 2 == 0 && index % 2 == 0) || // ch
ecking even values
(s.indexOf(s) % 2 != 0 && index % 2 != 0)) {
result[i] = false;
} else if ((s.indexOf(s) % 2 == 0 && index % 2 != 0)
|| (s.indexOf(s) % 2 != 0 && index % 2 == 0))
{
result[i] = true;
break;
}
}
}
}
}
return result;
}

//main Method
public static void main(String[] args) throws IOException {
Scanner in = new Scanner(System.in);

int n = Integer.parseInt(in.nextLine().trim());
String[] a = new String[n];
for (int i = 0; i < n; i++) {
a[i] = in.nextLine();
}

int m = Integer.parseInt(in.nextLine().trim());
String[] b = new String[m];
for (int i = 0; i < m; i++) {
b[i] = in.nextLine();
}

// call twins function


boolean[] results = twins(a, b);

for (int i = 0; i < results.length; i++) {


System.out.println(results[i] ? "Yes" : "No");
}
}
}

ARRAY GAME
PRODUCT SORT
ANOTHER SOLUTION
VOWELS

RESTRUCTURED ARRAY
(another solution also available)
IS IT POSSIBLE
static LinkedList<Pair<Integer,Integer>> pairs = new LinkedList<Pair<Integer, Integer>>();

public static String isItPossible(Integer a, Integer b, Integer c, Integer d){

pairs.addLast(new Pair<Integer, Integer>(a,b));

while (!pairs.isEmpty()){

Pair<Integer,Integer> pair = pairs.poll();

Integer key = pair.getKey();

Integer value = pair.getValue();

if(key.equals(c) &&
value.equals(d)){

return "YES";

int sum=key+value;

if (sum<=c){

pairs.addLast(new Pair<Integer, Integer>(sum,value));

if (sum<=d){

pairs.addLast(new Pair<Integer, Integer>(key,sum));

return "NO";

VOWELS
ANOTHER SOLUTION
AUTOSCALE POLICY
MINIMUM SWAPS
ANOTHER SOLUTION
ARE ALMOST EQUIVALENT
ARRANGING COINS
COUNT DUPLICATE ELEMENTS

ANOTHER SOLUTION

public static int countDuplicate(List<int> numbers)

Dictionary<int,int> frequency = new Dictionary<int, int>();

for(int i=0; i<numbers.Count;i++){


if(frequency.ContainsKey(numbers[i])){

frequency[numbers[i]] = frequency[numbers[i]]+1;

else{

frequency.Add(numbers[i],1);

List<int> result = new List<int>();

foreach(var keys in frequency.Keys){

if(frequency[keys]>1){

result.Add(keys);

return result.Count;

BINARY NUMBER IN A LINKED LIST


DISTINCT NUMBERS
Competitive gaming
competetive gaming soln 2
Consolidated partitions

consolicadate partitions soln 2


Construction management
SLOWEST KEY PRESS
AREA OF THE BOX
CAR INHERITANCE

class WagonR extends Car{

int mileage;

public WagonR(Integer mileage){

super(false, "4");

this.mileage = mileage;

@Override

public String getMileage(){

String mil=Integer.toString(mileage);

return mileage+" kmpl";

class HondaCity extends Car{

int mileage;

public HondaCity(Integer mileage){

super(true, "4");

this.mileage = mileage;

@Override

public String getMileage(){

String mil=Integer.toString(mileage);

return mil+" kmpl";

}
class InnovaCrysta extends Car{

int mileage;

public InnovaCrysta(Integer mileage){

super(false, "6");

this.mileage = mileage;

@Override

public String getMileage(){

String mil=Integer.toString(mileage);

return mil+" kmpl";

}
PRISON BREAK

Check this also: https://leetcode.com/discuss/interview-question/1002082/twillio-oa-prison-break


COUNT STRING PERMUTATIONS
LIST INHERITANCE

https://allhackerranksolutionsbykaira.blogspot.com/2020/08/list-inheritance-hackerrank-
solution.html

DEVICE NAME SYSTEM

https://www.homeworklib.com/question/1486956/in-java-6-device-name-system-suggested-
problem

SPORT INHERITANCE

https://www.goeduhub.com/2885/program-inherit-cricketplayer-footballplayer-hockeyplayer
MERGE 2 ARRAYS
CONDENSED LIST

WAYS TO SUM
BALANCED ARRAY

EMPLOYEE IMPLEMENTATION

https://github.com/vadym-usatiuk/Hackerrank-Employee-
Implementation/blob/master/src/Solution.java
WORK SCHEDULE
COUNT OPTIONS

Count the number of ways to divide N in k s incrementally


https://www.geeksforgeeks.org/count-the-number-of-ways-to-divide-n-in-k-groups-
incrementally/
PERFECT SUBSTRING
LOAD BALANCING

https://leetcode.com/discuss/interview-question/719447/wish-online-assessment-question

CONSTRUCTION MANAGEMENT
IS POSSIBLE

ANOTHER SOLUTION

static LinkedList<Pair<Integer,Integer>> pairs = new LinkedList<Pair<Integer, Integer>>();

public static String isItPossible(Integer a, Integer b, Integer c, Integer d){

pairs.addLast(new Pair<Integer, Integer>(a,b));

while (!pairs.isEmpty()){

Pair<Integer,Integer> pair = pairs.poll();

Integer key = pair.getKey();

Integer value = pair.getValue();


if(key.equals(c) &&

value.equals(d)){

return "YES";

int sum=key+value;

if (sum<=c){

pairs.addLast(new Pair<Integer, Integer>(sum,value));

if (sum<=d){

pairs.addLast(new Pair<Integer, Integer>(key,sum));

return "NO";

}
GROUP DIVISION

SORT AN ARRAY
EFFICIENT SHIPPING
THE RESTRUCTURED ARRAY
PRODUCT OF MAXIMUM AND MINIMUM IN A DATA SET

You might also like