Posts

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!...

Family Tree Project in Java

Image
Creating Family tree in java .  Trees are such an important structure in computer science. But what more important tree, than our family tree? ❤️   👨‍👩‍👧‍👦   ❤️    Made this simple program in java to implement family tree. Java File import java.util.*; class Person { String name; boolean married; String wife; int noOfChild; Person[] childs; public Person(String name){ this.name = name; } public Person(String name,boolean married,String wife,Person[] childs,int noOfChild) { this.name = name; this.married = married; this.wife = wife; this.childs = childs; this.noOfChild = noOfChild; } public Person(String name , boolean married,String wife) { this.name = name; this.married = married; this.wife = wife; } } class familyTree { static int population; static Scanner input = new Scanner...

How to take input from user in sublime 3 in JavaScript? [ SOLVED ]

Image
 [SOLVED] Take console (cmd) input in sublime 3 text editor in javascript Open Sublime  goto   >   Tools   >  Build System   >  Build New System       paste the following code in that file and save it as  anyName.sublime-build { "cmd":["start", "cmd", "/k" ,"node","$file_base_name"], "selector": "source.jW", "working_dir": "${file_path}", "shell": true } And now in your js file add these lines. Help from official website : click here const readline = require('readline').createInterface({ input: process.stdin, output: process.stdout }) readline.question(`What's your name?`, name => { console.log(`Hi ${name}!`) readline.close() })

How to find Max and Min value in array java.

Image
Finding min or max value in premative array import java.util.*; class cf { static Scanner input = new Scanner(System.in); public static void main(String[] args) { Integer[] arr = {2,3,1,5,3,61,12}; int max = Collections.max(Arrays.asList(arr)); int min = Collections.min(Arrays.asList(arr)); System.out.println(max); System.out.println(min); } } Output : 61 1

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