Posts

Showing posts with the label cp

Codechef April challange : Chef and Dice solution

Image
 Solving codechef April challange :     Chef and Dice Solution in Java. Problem statement - Chef has  N N   6-sided standard dice . Each die has dimensions  1 × 1 × 1 1 × 1 × 1 . Since Chef is bored during the quarantine, he decides to stack dice for fun. First, Chef forms four vertical stacks of dice (not necessarily with the same height; empty stacks are allowed) on his table, which together make up a pile of dice with base area up to  2 × 2 2 × 2 . Among all such structures, the total visible surface area of Chef's structure must be the smallest possible. Then, Chef calculates the number of  pips  on the visible faces of all dice in the structure. A face of a die is visible if it does not touch the table or another die. Now, he is wondering: among all possible arrangements of dice, what is the maximum possible total number of visible pips? Since he is busy cooking, he ...

Codechef crypthon contest.

Image
  Problem Link Codechef crypthon contest : hard code crypt problem solution in java. import java.util.*; class cf { static void calc(int[] arr , int n) { int count = 0; for(int i=0;i<n-3;i++) { for(int j=i+1;j<n-2;j++) { int diff = arr[j] - arr[i]; for(int k=j+1;k<n-1;k++) { for(int l=k+1;l<n;l++) { if(arr[l] - arr[k] == diff && arr[k] - arr[j] == diff) { System.out.println(arr[i]+" : "+arr[j]+" : "+arr[k]+" : "+arr[l]); count ++; } } } } } System.out.println(count); } static Scanner input = new Scanner(System.in); public static void main(String[] args) { try { int n = input.nextInt(); int[] arr = new int[n]; for(int i=0;i<n;i++) { arr[i] = input.nextInt(); } calc(arr,n); ...

LeetCode : Reverse Words in a String III (Java Solution).

Image
 Java Solution for reverse words in a string form leetcode. Given a string  s , reverse the order of characters in each word within a sentence while still preserving whitespace and initial word order. Example 1: Input: s = "Let's take LeetCode contest" Output: "s'teL ekat edoCteeL tsetnoc" Example 2: Input: s = "God Ding" Output: "doG gniD" class Solution { public String reverseWords(String s) { return Arrays.stream(s.split(" ")) .map(word -> new StringBuilder(word).reverse().toString()) .collect(Collectors.joining(" ")); } } Another Solution class Solution { public String reverseWords(String s) { String[] arr = s.split(" "); String ans = ""; for(int i=0;i<arr.length;i++) ans += reverse(arr[i])+((i==arr.length-1)?"":" "); return ans; } static...

Codechef : Program to find if given very large number is divisible by 3 or not

Image
Multiple of 3 Problem Code: MULTHREE Consider a very long  K -digit number  N  with digits  d 0 , d 1 , ..., d K-1  (in decimal notation;  d 0  is the most significant and  d K-1  the least significant digit). This number is so large that we can't give it to you on the input explicitly; instead, you are only given its starting digits and a way to construct the remainder of the number. Specifically, you are given  d 0  and  d 1 ; for each  i  ≥ 2,  d i  is the sum of all preceding (more significant) digits, modulo 10 — more formally, the following formula must hold:  Determine if  N  is a multiple of 3. Example 3 5 3 4 13 8 1 760399384224 5 1 NO YES YES Java Solution : //codechef very very long number Multiple of three // https://www.codechef.com/LRNDSA01/problems/MULTHREE ...

Maze Traveller Problem solved using Dynamic programming

Image
 Maze Traveller Solution in Java solved using recursion. import java.util.*; class mazeTraveller { static HashMap<String, Long> map = new HashMap<>(); static long calculate(int row , int col) { return _calculate(row, col , map); } static long _calculate(int row ,int col,HashMap<String,Long> map) { String key = row+":"+col; if(map.containsKey(key)) return map.get(key); if(row == 0 || col == 0 ) return 0; if(col == 1 || row == 1) return 1; long res = _calculate(row-1,col,map) + _calculate(row,col-1,map); map.put(key,res); return map.get(key); } static Scanner input = new Scanner(System.in); public static void main(String[] args) { try { int row = input.nextInt(); int col = input.nextInt(); long paths = calculate(row,col); System.out.println("There are "+paths ...

ThreeSum Solution in Java

Image
ThreeSum in java. Given an array nums of n integers , are there elements a , b , c in nums such that a + b + c = 0 Find all unique triplets in the array which gives the sum of zero. Note the solution set must not contain duplicate triplets. Example : Given array nums = [ -1 , 0 , 1 , 2 , -1 , -4 ] A solution set is : [ [-1 , 0 , 1], [ -1, -1, 2], ] import java.util.*; class ThreeSum { static List<List<Integer>> calculate(int[] arr) { if(arr == null || arr.length < 3) { return Collections.emptyList(); } int n = arr.length; List<List<Integer>> list = new ArrayList<List<Integer>>(); Map<Integer,Integer> map = new HashMap<>(); for(int i=0;i<n-2;i++) { int first = arr[i]; for(int j=i+1;j<n;j++) { int remain = first+arr[j]; remain = (remain<0)?Ma...

TwoSum Solution in Java

Image
 Two sum solution in Java . Question : Given an array of integers nums and an integer target , return indices of the two numbers such that they add up to target. You may assume that each input would have exactly one solution, and you may not use the same element twice. You can return the answer in any order.  Input  : nums = [2,7,11,15], target = 9 Output: [0,1] Output: Because nums[0] + nums[1] == 9, we return [0, 1]. import java.util.*; class TwoSum { static int[] calculate(int[] arr , int target) { Map<Integer,Integer> map = new HashMap<>(); for(int i=0;i<arr.length;i++) { int remain = target - arr[i]; if(map.containsKey(remain)) { return new int[] {map.get(remain),i}; } map.put(arr[i],i); // System.out.println(map); } throw new IllegalArgumentException("No two sum solution"); } pu...

Todays Challange :

Image
 Solved Challange : Java Code :   import java.util.*; class Temp { static void add(String[] ans , String str, int index) { ans[index] = str; } static Scanner input = new Scanner(System.in); public static void main(String[] args) { try { int n = input.nextInt(); String[] ans = new String[n]; String s = ""; int first = 0; int last = n-1; int flag = 0; for(int i=1;i<= n*n;i++) { if(i%n==0){ s += "" +i; if(flag == 0) { flag = 1; add(ans,s,first++); } else{ flag = 0; add(ans,s,last--); } s = ""; continue; } s+= "" + i + "*"; } for(String sss:ans) System.out.println(sss); } catch(Exception e){ ...

Java Factorial Using BigInteger

Image
Solving Factorial of number by using BigInteger in Java import java.util.*; import java.math.BigInteger; class cf { static HashMap<BigInteger,BigInteger> list = new HashMap<>(); static BigInteger extraLongFactorials(int n) { BigInteger N = new BigInteger(String.valueOf(n)); if(list.containsKey(N)) return list.get(N); if(n==1 || n==2 || n==0) return N; else { BigInteger ans = N.multiply(extraLongFactorials(n-1)); list.put(N,ans); return list.get(N); } } static Scanner input = new Scanner(System.in); public static void main(String[] args) { try { int n = input.nextInt(); System.out.println(extraLongFactorials(n)); } catch(Exception e){ return; } } } Output : 100 9332621544394415268169923885626670049071596826438162146859296389521759999322991560894146397...

Popular Posts

java:17: error: local variables referenced from a lambda expression must be final or effectively final count ++ ;

Family Tree Project in Java

Creating basic tic tac toe android app using java