Posts

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); ...

Round Upto 2 decimal places in java : using DecimalFormat

Image
 How to Round Number upto n decimal places in java. import java.util.*; import java.text.DecimalFormat; class MyDecimalFormater { static Scanner input = new Scanner(System.in); public static void main(String[] args) { try { double price = 1234567890.9876; // round up to 2 decimal places // fist method System.out.println("Using Math.round() : "+Math.round(price*100.0)/100.0); // second method System.out.println("By Using DecimalFormat :"); DecimalFormat f = new DecimalFormat("###.00"); System.out.println("round upto 2 places : "+f.format(price)); // exploring DecimalFormat DecimalFormat g = new DecimalFormat("$###.000"); System.out.println("round upto 3 places and using dollar symbol : "+g.format(price)); DecimalFormat h = new DecimalFormat("\u00A5###,###.000"); System.out...

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){ ...

Find all array elements occurring more than ⌊N/3⌋ times.

Image
  Q. Find all array elements occurring more than ⌊N/3⌋ times. Solution in java : // find elements in the array that occurs more than n/3 times in array // 19 march 2021 // T.U.F Question import java.util.*; class cf { static void calc(int[] arr ) { int len = arr.length; //first solution // HashMap<Integer,Integer> map = new HashMap<Integer,Integer>(); // for(int i=0;i<len;i++) { // if(!map.containsKey(arr[i])){ // map.put(arr[i],1); // } // else{ // map.put(arr[i],map.get(arr[i])+1); // } // } // System.out.println(map); // map.forEach((key,value)-> { // if(value>len/3) // System.out.println(key); // }); int cnt1 = 0; int cnt2 = 0; int num1 = -1; int num2 = -1; for(int i=0;i<len;i++) { if(arr[i] == num1){ cnt1++; } else if(arr[i] == num2){ ...

Simple Socket Programming in java

Image
 In this Java network programming tutorial, you will learn how to develop a simple socket server program. Server.java package simple_tcp; import java.io.BufferedReader; import java.io.InputStreamReader; import java.io.OutputStreamWriter; import java.io.PrintWriter; import java.net.ServerSocket; import java.net.Socket; public class Server { public Server() throws Exception { ServerSocket server_socket = new ServerSocket(2020); //opening a new port System.out.println("Port 2020 is open."); Socket socket = server_socket.accept(); System.out.println("Client " + socket.getInetAddress() + " has connected."); // I/O buffers: BufferedReader in_socket = new BufferedReader(new InputStreamReader (socket.getInputStream())); PrintWriter out_socket = new PrintWriter(new OutputStreamWriter (socket.getOutputStream()), true); out_socket.println("Welcome!...

Popular Posts

Family Tree Project in Java

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

Creating basic tic tac toe android app using java