Posts

Showing posts with the label practice

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

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