Skip to content

Instantly share code, notes, and snippets.

View gauravbansal74's full-sized avatar
💻
Typing...

Gaurav Kumar gauravbansal74

💻
Typing...
View GitHub Profile
@gauravbansal74
gauravbansal74 / CountingValleys.java
Created July 9, 2019 13:27
Counting Valleys || HackerRank
// Complete the countingValleys function below.
static int countingValleys(int n, String s) {
int noOfValleys = 0;
int currentLevel = 0;
for(char c: s.toCharArray()){
if(c == 'U') ++currentLevel;
if(c == 'D') --currentLevel;
if(currentLevel == 0 && c == 'U') ++noOfValleys;
}
return noOfValleys;
@gauravbansal74
gauravbansal74 / SockMerchant.java
Created June 30, 2019 08:55
Sock Merchant || HackerRank
static int sockMerchant(int n, int[] arr) {
Set<Integer> colors = new HashSet<>();
int pairs = 0;
if(n == arr.length){
for (int i = 0; i < n; i++) {
if (colors.remove(arr[i])) {
pairs++;
} else {
colors.add(arr[i]);
}
@gauravbansal74
gauravbansal74 / BinarySearch.Recursive.js
Created January 16, 2018 13:19
Binary Search Recursive
var input = `1
30 8 10 15 16 18 30`;
function start(input){
if(input !== ""){
var inputSplittedByLine = input.split("\n");
var noOfStatements = inputSplittedByLine[0];
if(noOfStatements == inputSplittedByLine.length-1){
for(var i =1; i < inputSplittedByLine.length; i++){
var currentStatementSplitted = inputSplittedByLine[i].split(" ");
@gauravbansal74
gauravbansal74 / LinearSearch.js
Last active January 15, 2018 15:34
Linear Search
var input = `2
5 16
9 7 2 16 4
7 98
1 22 57 47 34 18 66`;
function searchNumber(digit, input){
var output = -1;
for(var i=0; i <input.length; i++){
if(digit == input[i]){
// you can also use imports, for example:
import java.util.*;
// you can write to stdout for debugging purposes, e.g.
// System.out.println("this is a debug message");
class Solution {
public int solution(int[] A) {
// write your code in Java SE 8
int len = A.length;
import java.util.*;
// you can write to stdout for debugging purposes, e.g.
// System.out.println("this is a debug message");
class Solution {
public int solution(int[] A) {
// write your code in Java SE 8
int length = A.length;
int min = 0;
// you can also use imports, for example:
import java.util.*;
// you can write to stdout for debugging purposes, e.g.
// System.out.println("this is a debug message");
class Solution {
public int solution(int N) {
// write your code in Java SE 8
int count = 0;
@gauravbansal74
gauravbansal74 / IsAnagramPalindrome.java
Created January 13, 2018 07:19
IsAnagramPalindrome
import java.util.*;
class IsAnagramPalindrome{
public static boolean isAnagramOfPalindrome(String s){
if(s.length() == 0){
return false;
}
if(s.length() == 1){
class FizzBuzz {
public static void solution(int N) {
// write your code in Java SE 8
int count = 0;
int contCount = 0;
for(int i = 1; i<=N;i++){
if((i % 3 == 0) && (i % 5 == 0) && (i % 7 == 0)){
System.out.println("FizzBuzzWoof");
@gauravbansal74
gauravbansal74 / BinarySearchIndex.java
Created January 13, 2018 07:17
BinarySearchIndex
import java.util.Arrays;
public class BinarySearchIndex {
public static int binarySearchIndex(int[] arr, int item) {
int length = arr.length;
int hi = length-1;
int lo = 0;