00:00
/* C_26 : bubble sort
* given array element
* 36 19 29 12 5
*/
public class C_26 {
public static void main(String[] args) {
int [] a = {36,19,29,12,5};
int temp;
for( int i=0;i<a.length;i++){ /* for round which is one less than element*/
int flag =0;
for( int j=0;j<a.length-1-i;j++){
if(a[j]>a[j+1]){
temp =a[j];
a[j] = a[j+1];
a[j+1] = temp;
flag=1;
}
}
if(flag==0){ // if no swapping , break the main loop
break;
}
}
for( int i=0;i<a.length;i++){
System.out.print(a[i]+" ");
}
}
}
// a.length-1 , swaps starts form 0 to 3
// a.lenght-1-i, in i=1, swaps form 0 to 2
// if flag==0 stop the round loop;
// 5 12 19 29 36 %
/* C_27 : bubble sort using string */
public class C_27 {
public static void main(String[] args) {
String [] a = {"rajat","manoj","vijay","hardeep","deepak"};
String temp;
for( int i=0;i<a.length;i++){ /* for round which is one less than element*/
int flag =0;
for( int j=0;j<a.length-1-i;j++){
if(a[j].compareTo(a[j+1])>0){
temp =a[j];
a[j] = a[j+1];
a[j+1] = temp;
flag=1;
}
}
if(flag==0){ // if no swapping , break the main loop
break;
}
}
for( int i=0;i<a.length;i++){
System.out.print(a[i]+" ");
}
}
}
// a.length-1 , swaps starts form 0 to 3
// a.lenght-1-i, in i=1, swaps form 0 to 2
// if flag==0 stop the round loop;
// deepak hardeep manoj rajat vijay %
/* C_28 selection sort
* searching and sorting
* let int[] a = { 38, 52, 9, 18, 6, 62, 13 };
* check for fisrt element (38) and compair it with the smallest one
* if find , then interchange their positions
38, 52, 9, 18, 6, 62, 13 // for first element check 6 is smaller than 38
6, 52, 9, 18, 38, 62, 13 // check for second element 52, 9 is smaller
6, 9, 52, 18, 38, 62, 13 // check for third element 52, 13 is smaller
6, 9, 13, 18, 38, 62, 52 // check for fourth element 18, no # found
6, 9, 13, 18, 38, 62, 52 // check for fifth element 38, no # found
6, 9, 13, 18, 38, 52, 62 // check for fourth element 62, 52 is smaller
*/
public class C_28{
public static void main(String[] args) {
int[] a = { 38, 52, 9, 18, 6, 62, 13 };
int min;
int temp;
for(int i =0;i<a.length;i++){
min=i;
for(int j=i+1;j<a.length;j++){
if(a[j]<a[min]){
min=j;
}
}
temp=a[i];
a[i]=a[min];
a[min]=temp;
}
for(int k=0;k<a.length;k++){
System.out.print(a[k]+" ");
}
}
}
// code logic
// i=0 ,j=1 if 52<38 =false
// ,j=2 if 9<38 = true , update min = 9
// ,j=3 if 18<9 = false,
// ,j=4 if 6<9 = true, update minn = 6
// ,j=5 if 62<6 = false,
// ,j=6 if 13<6 = false,
// find the lowest numbber
// replace it with the first position number
// save a[i] value in var1 , put a[min] position value in a[i] position value and
// again put var1 positon a[min] position
// 6 9 13 18 38 52 62 %
/* C_29 selection sort for strings array */
public class C_29 {
public static void main(String[] args) {
String[] a1 = { "rajat", "deepak", "kuldeep", "manoj", "ajay"};
int min;
String temp;
for(int i =0;i<a1.length;i++){
min=i;
for(int j=i+1;j<a1.length;j++){
if(a1[j].compareTo(a1[min])<0){
min=j;
}
}
temp=a1[i];
a1[i]=a1[min];
a1[min]=temp;
}
for(int k=0;k<a1.length;k++){
System.out.print(a1[k]+" ");
}
}
}
/*
ramannegi@Ramans-MacBook-Pro x % javac C_29.java
ramannegi@Ramans-MacBook-Pro x % java C_29
ajay deepak kuldeep manoj rajat %
*/
/* C_30 insertion sort
*
5, 1, 6, 2, 4, 3 // start with position 1, 0 is fixed
1, 5, 6, 2, 4, 3 // compaired index 1 element to index 0 element and swap
1, 5, 6, 2, 4, 3 // compaired index 2 element to its previous index element to the 0 index element, if small swap
1, 5, 6, 2, 4, 3 // compaired index 3 element to its previous index to 0 index element
1, 2, 4, 5, 6, 3 // compaired index 4 element to its previous index to 0 index element
1, 2, 3, 4, 5, 6 // compaired index 5 element to its previous index to 0 index element
*/
public class C_30 {
public static void main(String[] args) {
int[] a = {5, 1, 6, 2, 4, 3};
for(int i=1;i<a.length;i++){
int temp=a[i];
int j=i;
while(j>0 && a[j-1]>temp){
a[j]=a[j-1]; //swap
j=j-1; // decrement
}
a[j]=temp;
}
for( int k=0;k<a.length;k++){
System.out.print(a[k]+" ");
}
}
}
// j > 0 check index form current to till
/*
ramannegi@Ramans-MacBook-Pro x % javac C_30.java
ramannegi@Ramans-MacBook-Pro x % java C_30
1 2 3 4 5 6 %
*/
/* C_31 merge sort */
public class C_31 {
}
/* C_32 quick sort */
public class C_32 {
}
/* C_33 heap sort */
public class C_33 {
}
/* C_34 string sorting */
public class C_34 {
}
/* C_35 linear search
*
a = 5, 3, 6, 1, 4, 2 ;
let search =1
start search form 0 index, if equal -ok, if not shift to next index,
if lakh element , consumes time
*/
public class C_35 {
public static void main(String[] args) {
/* for integer */
int[] a = {5, 3, 6, 1, 4, 2};
int searchItem = 2;
int temp = 0 ;
for(int i=0;i<a.length;i++){
if(a[i]==searchItem){
System.out.println("element position is = "+i+"th");
temp =temp+1;
}
}
if(temp==0){
System.out.println("element not found "+temp);
}
}
}
/*
ramannegi@Ramans-MacBook-Pro x % javac C_35.java
ramannegi@Ramans-MacBook-Pro x % java C_35
element position is = 5th
*/
/* C_36 linear search for strings*/
public class C_36 {
public static void main(String[] args) {
String[] a = {"rajat", "kaju", "manoj", "jitesh", "deepak", "kuldeep"};
String finName = "kapil";
int temp=0;
for(int i =0; i<a.length;i++){
if(a[i].equals(finName)){
System.out.println("name present at positon = "+ i );
temp = temp+0;
}
}
if(temp==0){
System.out.println("name is not present in the list");
}
}
}
/*
ramannegi@Ramans-MacBook-Pro x % javac C_36.java
ramannegi@Ramans-MacBook-Pro x % java C_36
name is not present in the list
*/
/* C_37 bianry search
*
fast compair to linear
list already sorted
a = 1, 2, 4, 5, 7, 8, 9 ;
let item = 8;
find 3 variables
li = 0,
hi ( lenght-1 ) = 6,
mi ( li + hi)/2 = 6/2 = 5,
if(a[mi] == item) - Dispaly result and break the loop
elseif(a[mi] < item) - do the above before 5 element
else - do the above after 5 element
till while (li <= hi)
*
*/
public class C_37 {
public static void main(String[] args) {
int[] a = {1, 2, 4, 5, 7, 8, 9};
int item =8, li=0;
int hi=a.length-1;
int mi=(li+hi)/2;
while(li<=hi) {
if(a[mi]==item){
System.out.println("item is at position ="+mi);
break; // w/o it infinite loop
}
else if(a[mi]<item){
li=mi+1;
}
else{
hi=mi-1;
}
mi=(li+hi)/2;
}
}
}
// li=0, hi=6, mi=3, if(5==8), else if ( 5<8 ), else () , u/p = li=4 and mi= 5
// li=4, hi=6, mi=5, if(8==8), print mi ,break operations
// check at middle position, if not found check the larger part if found update the li,
// if not check the lower part and update hi ,till the number equals to item
/*
ramannegi@Ramans-MacBook-Pro x % javac C_37.java
ramannegi@Ramans-MacBook-Pro x % java C_37
item is at position =5
*/
/* C_38 : anonymus array */
/*
* int []a = new int [2]; ( a is array name )
* new int{10,20,30}; ( anonymus array, address analogy example )
* use = single time / instantly /
* must single line create and initilize /
* single multi dimensional array
* can be used only once
* used as an arguments
*/
class C_38{
public static void main(String[] args) {
System.out.println("for single dimensions array");
C_38.sum(new int[]{10,20,30}); /* pass = new int[]{10,20,30} */
System.out.println("for 2d dimensions array");
C_38.add(new int[][]{{10,20,30},{40,50}});
System.out.println("for 3d dimensions array");
C_38.yog(new int[][][]{{{10,20,30},{40,50}}});
}
static void sum(int[] no){ /* no = array name */
int total =0;
for( int i: no){
total = total+i;
}
System.out.println("The sum of array is = "+total);
}
static void add(int[][] no1){
int total1 =0;
for( int j[]: no1){
for( int jj:j){
total1 = total1+jj;
}
}
System.out.println("The sum of array is = "+total1);
}
static void yog(int[][][] no2){
int total2 =0;
for( int k[][]: no2){
for( int m[]:k){
for( int n:m){
total2=total2+n;
}
}
}
System.out.println("The sum of array is = "+total2);
}
}
/*
ramannegi@Ramans-MacBook-Pro x % javac C_38.java
ramannegi@Ramans-MacBook-Pro x % java C_38
for single dimensions array
The sum of array is = 60
for 2d dimensions array
The sum of array is = 150
for 3d dimensions array
The sum of array is = 150
*/
/* C_39 find max min
a = 4, 3, 5, 2, 1, 6 ;
int max =a[0] , assume
for( int i=1;i<a.lengh;i++)
check the max balue for each element and swap
{
if(a[i]>max){
max=a[i];
}
}
sop(max)
}
*/
public class C_39 {
public static void main(String[] args) {
int[] a = {4, 3, 8, 2, 9, 6};
int max =a[0];
for(int i =1;i<a.length;i++){
if(a[i]>max){
max=a[i];
}
}
System.out.println("maximum value in the list is = "+max);
}
}
// max =4 , a[1] =3, 3>4;
// max =4 , a[2] =5, 5>4;
// update max =5,
// max =5 a[3] =2, 2>5;
// max =5 a[4] =1, 1>5;
// max =5 a[5] =6, 6>5;
// update max =6;
// fixed 0 index element , compair all with it ,
// if true update an vairable and check rest with updated element
/*
ramannegi@Ramans-MacBook-Pro x % javac C_39.java
ramannegi@Ramans-MacBook-Pro x % java C_39
maximum value in the list is = 9
*/
/* C_40 find second largest element -1
sort the list decending order, sop a[1];
*/
public class C_40 {
public static void main(String[] args) {
int temp;
int[] a ={6, 8, 2, 4, 3, 1, 5, 7};
for(int i=0;i<a.length;i++){
for(int j =i+1;j<a.length;j++){
if(a[i]<a[j]){
temp=a[i];
a[i]=a[j];
a[j]=temp;
}
}
}
System.out.println(a[1]);
}
}
// sort decending and sopa[1]
/*
ramannegi@Ramans-MacBook-Pro x % javac C_40.java
ramannegi@Ramans-MacBook-Pro x % java C_40
7
*/
/* C_41 find second largest element -2
two variables
largest = Integer.MIN_VALUE
second_ largest = Integer.MIN_VALUE
a ={6, 8, 2, 4, 3, 1, 5, 7};
i=0, 6 > -2147483648, l = 6, sl = -2147483648
i=1, 8 > 6, l = 8, sl = 6
i=2, 2 > 8, l = 8, sl = 6
i=3, 4 > 8, l = 8, sl = 6
*/
public class C_41 {
public static void main(String[] args) {
int[] a ={6, 8, 2, 4, 3, 1, 5, 7};
int largest = Integer.MIN_VALUE;
int second_largest = Integer.MIN_VALUE;
for(int i=0;i<a.length;i++){
if(a[i]>largest){
second_largest=largest;
largest=a[i];
}
else if(a[i]>second_largest && a[i]!=largest){
second_largest=a[i];
}
}
System.out.print(second_largest);
}
}
// a[i]!= larget , suppose same value two element , dont update
// elseif vlock , value whch is smaller than the largest but
// bigger than the second largest value
/*
ramannegi@Ramans-MacBook-Pro x % javac C_41.java
ramannegi@Ramans-MacBook-Pro x % java C_41
7%
*/
/* C_42 find the kth largest /smallest element
lets say fourth element , sort till 4th position ,decending position
*/
public class C_42 {
public static void main(String[] args) {
int[] a = {5, 8, 12, 7, 6, 2, 4};
int k = 3;
for(int i=0;i<a.length;i++){
for( int j=i+1;j<a.length;j++){
if(a[i]<a[j]){
int temp=a[i];
a[i]=a[j];
a[j]=temp;
}
}
if(i==k-1){
System.out.println(a[i]);
break;
}
}
}
}
// for i=1, sort the highest elemt among whole list,
// and when desired value sort , stop the sorting
/*
ramannegi@Ramans-MacBook-Pro x % javac C_42.java
ramannegi@Ramans-MacBook-Pro x % java C_42
7
*/
/* C_43 - find duplicate value in array - Brute force */
public class C_43 {
public static void main(String[] args) {
int[] a = {3,5,4,3,2,2,1};
System.out.print("Duplicate Values are = ");
for(int i=0;i<a.length-1;i++){
for(int j=i+1;j<a.length;j++){
if(a[i]==a[j] && (i!=j)){
System.out.print(a[j]+ ", ");
}
}
}
}
}
/*
0. if duplicate is more than two , not suitable
1. match first element with rest upto last
2. j=1 , pahle se ek jyada se shuru
3. a[i]==a[j] , i position ka element baki ke sabhe elements se ek ek
karke check karan aur phir match karna .
4. i!=j, kabhi bhe koi element khud se compair na kare .
5. i<a.lenght-1, galti se 7 index position na aajaye , usse bachne ke leeaye
aur end ka element hum kabhe match nahe karege
*/
/*
ramannegi@Ramans-MacBook-Pro x % javac C_43.java
ramannegi@Ramans-MacBook-Pro x % java C_43
Duplicate Values are = 3, 2, %
*/
/* C_44 - find duplicate value in array - SET interface */
import java.util.HashSet;
import java.util.Set;
public class C_44 {
public static void main(String[] args) {
int[] a= {3,5,4,3,2,2,1,3};
Set<Integer> s = new HashSet<>(); // can't create object of SET,coz - interface
for(int no:a){
if(s.add(no)==false){
System.out.println(no);
}
}
}
}
/*
1. SET interface cannot save duplicate values.
2. if element is repeate more than 2 tiems , not work properly
3. fast then brute force
*/
/*
ramannegi@Ramans-MacBook-Pro x % javac C_44.java
ramannegi@Ramans-MacBook-Pro x % java C_44
3
2
3
*/
/* C_45 - Find Duplicate value in array - HASH Table */
import java.util.HashMap;
import java.util.Map;
import java.util.Set;
public class C_45 {
public static void main(String[] args) {
int[] a = {3, 5, 4, 3, 2, 2, 1};
Map<Integer,Integer> hm = new HashMap<>();
System.out.println("duplicate elements are=");
for(int no: a){
Integer count = hm.get(no); //get, HashMap m/t, initially = get = null
if(count == null){
hm.put(no,1); // put, m/t , takes two parameters
}
else{
count =count+1;
hm.put(no,count);
}
}
Set<Map.Entry<Integer,Integer>> es = hm.entrySet();
for(Map.Entry<Integer,Integer> me:es){
if(me.getValue()>1){
System.out.println(me.getKey());
}
}
}
}
/*
1. store data key and values pairs.
2. get element form array and check it in the key, not present , integer value return = null.
3. if null call put method with two arguments.
4. in hash map key =3, value=1;
5. for next value , get =5, check it in key , not preset, integer value return null.
6. in hash map key =5, value=1;
7. for 4, key =4, value=1;
7. get element =3, check , present , integer value return =1
8. key =3, value = 1+1 = 2;
9. for 2 , key =2, value=1
10. for 2 , key =2, value=2;
11. for 1 , key =1, value=1;
12. use set , coz it will not save duplicate values.
*
*/
/*
ramannegi@Ramans-MacBook-Pro x % javac C_45.java
ramannegi@Ramans-MacBook-Pro x % java C_45
duplicate elements are=
2
3
*/
/* C_46 - Only First Duplicate element- simple way */
public class C_46 {
public static void main(String[] args) {
int[] a ={6,5,3,2,5,1,2,4};
int temp=0;
for( int i=0;i<a.length-1;i++){
for(int j =i+1;j<a.length;j++){ // start from seond
if(a[i]==a[j] && (i!=j)){
System.out.print("first duplicate value = "+a[i]);
temp=temp+1;
break; // stop checking further values of j, stop the current loop
}
if(temp>0){
break; // stop checking further value of i
}
}
}
}
}
// code logic
/*
1. Take first element and match it with the rest , if match it is the first duplicate element.
2. Then second if previous not match.
3. a.lenght-1, out of bound exception.
4. j=i+1, start from second element.
5. i!=j , element should not match itself.
*/
/*
ramannegi@Ramans-MacBook-Pro x % javac C_46.java
ramannegi@Ramans-MacBook-Pro x % java C_46
first duplicate value = 5%
*/
/*C_47 - Only First Duplicate element- collections way */
import java.util.HashSet;
public class C_47 {
public static void main(String[] args) {
int[] a = {6,5,3,2,5,1,2,4};
int temp=-1;
HashSet<Integer> hs = new HashSet<>();
for(int i=a.length-1;i>=0;i--){
if(hs.contains(a[i])){
temp=i;
}
else{
hs.add(a[i]);
}
}
if(temp!=-1){
System.out.println("First duplicate value is = "+a[temp]);
}
else{
System.out.println("No First duplicate element found");
}
}
}
// code logic
/*
00. by using hashset contains method
01. temp = -1 ; hs variable .
02. get elemennt form last , check hs contain last element or not ?
03. same with others elements .
04. if element contains any element , temp= index position
05. hs contains ? 4 , false , temp= -1;
06. hs contains ? 2 , false , temp= -1;
07. hs contains ? 1 , false , temp= -1;
08. hs contains ? 5 , false , temp= -1;
09. hs contains ? 2 , false , temp= 3; ( index position )
10. hs contains ? 3 , false , temp= 3;
11. hs contains ? 5 , false , temp= 1;
12. hs contains ? 6 , false , temp= 1;
13. print 1 index numbber value
*/
/*
ramannegi@Ramans-MacBook-Pro x % javac C_47.java
ramannegi@Ramans-MacBook-Pro x % java C_47
First duplicate value is = 5
*/
/*C_48 - find element presennt once in the list, unique element, XOR*/
public class C_48 {
public static void main(String[] args) {
int[] a = {5,3,4,5,2,3,4};
int res =a[0];
for(int i =1;i<a.length;i++){
res=res^a[i]; // start form first , XOR the rest
System.out.println("for position "+i+"="+res);
}
System.out.println("single repeated element is ="+res);
}
}
/*
01. commutative 5^3 = 5+3=8 or 3+5=8 .
02. assocoiative 5,3 = 5*3=15 or 3*5=15 .
03. table
0 0 0
0 1 1
1 0 1
1 1 0
04. i=1 , 5 ^ 3 ^ 4 ^ 5 ^ 2 ^ 3 ^ 4 = can rearrange it to
5 ^ 5 ^ 3 ^ 3 ^ 4 ^ 4 ^ 2 ( coz it is commutative )
0 ^ 0 ^ 0 ^ 2
0 ^ 0 ^ 2
0 ^ 2
2
*/
/*
ramannegi@Ramans-MacBook-Pro x % javac C_48.java
ramannegi@Ramans-MacBook-Pro x % java C_48
for position 1=6
for position 2=2
for position 3=7
for position 4=5
for position 5=6
for position 6=2
single repeated element is =2
*/
/* C_49 - find even odd number in array, using array list*/
import java.util.ArrayList;
public class C_49 {
public static void main(String[] args) {
int[] a = {8, 5, 10, 12, 3, 1, 4};
ArrayList<Integer> al1 = new ArrayList<>();
ArrayList<Integer> al2 = new ArrayList<>();
for(int i =0;i<a.length;i++){
if(a[i]%2==0){
al1.add(a[i]);
}
else{
al2.add(a[i]);
}
}
int temp1=0;
int temp2=0;
System.out.print("\neven no = ");
for(int no:al1){
temp1=temp1+no;
System.out.print(no+" ");
}
System.out.print(", and count is = "+al1.size());
System.out.print(", with the sum of = "+temp1);
System.out.print("\nodd no = ");
for(int no:al2){
temp2=temp2+no;
System.out.print(no+" ");
}
System.out.print(", and count is = "+al2.size());
System.out.print(", with the sum of = "+temp2);
}
}
/*
1. create 2 object of array list, find mod
2. even nub in first object
3. odd num. in second object
4. size m/t use for getting the total number
5. use temp for getting the sum.
*/
/*
ramannegi@Ramans-MacBook-Pro x % javac C_49.java
ramannegi@Ramans-MacBook-Pro x % java C_49
even no = 8 10 12 4 , and count is = 4, with the sum of = 34
odd no = 5 3 1 , and count is = 3, with the sum of = 9%
*/
/* C_50 - find the missing number in array - simple way, for small list - ok */
public class C_50 {
public static void main(String[] args) {
int[] a = {1,2,3,4,6};
int expected_no_elements =a.length+1;
int total_sum = ( expected_no_elements*(expected_no_elements+1) ) /2; // expected
//System.out.println(total_sum);
int sum=0;
for(int i=0;i<a.length;i++){
sum=sum+a[i]; // actual
}
System.out.println(total_sum-sum);
}
}
/*
1. find all the sum of expected elements.
2. subtarct the sum of real elements.
3. print
4. total_sum = ( exp.noelements*(exp.noelements+1) )/2
*/
/*
ramannegi@Ramans-MacBook-Pro x % javac C_50.java
ramannegi@Ramans-MacBook-Pro x % java C_50
5
*/
/* C_51 - find the missing number in array - XOR way, for big small */
public class C_51 {
public static void main(String[] args) {
int[] a ={1, 2, 3, 4, 6};
int XOR1 = a[0];
for(int i=1;i<a.length-1;i++){
XOR1=XOR1^a[i];
}
int XOR2 = 1; // imagianry array with lenght+1 of a
for(int i=2;i<a.length+1;i++){
XOR2=XOR2^i;
}
System.out.println("Missing Number is = " + (XOR1 ^ XOR2) );
}
}
/*
1. xor1 = expected list
2. xor2 = actual list
3. xor both xor , (1 ^ 2 ^ 3 ^ 4 ^ 6) ^ (1 ^ 2 ^ 3 ^ 4 ^ 5 ^ 6 ) - rearragne all
*/
/*
ramannegi@Ramans-MacBook-Pro x % javac C_51.java
ramannegi@Ramans-MacBook-Pro x % java C_51
Missing Number is = 5
*/
/* C_52 - Common elements in 2 arrays in java- for loop */
public class C_52 {
public static void main(String[] args) {
int[] arr1 ={4, 3, 7, 8, 2};
int[] arr2 ={5, 1, 4, 8, 3};
for(int i=0;i<arr1.length;i++){
for( int j=0;j<arr2.length;j++){
if(arr1[i]==arr2[j]){
System.out.print(arr1[i]+" ");
}
}
}
}
}
/*
1. match first element with all element of second array.
2. if arr1[i] == arr[j] , true , print .
3. not for duuplicate values,
*/
/*
ramannegi@Ramans-MacBook-Pro x % javac C_52.java
ramannegi@Ramans-MacBook-Pro x % java C_52
4 3 8 %
*/
/* C_53 Common elements in 2 arrays in java- Hashset */
import java.util.HashSet;
public class C_53 {
public static void main(String[] args) {
int[] arr1 ={4, 3, 7, 8, 2, 4};
int[] arr2 ={5, 1, 4, 8, 3, 8};
HashSet<Integer> hs = new HashSet<>();
for(int i=0;i<arr1.length;i++){
for( int j=0;j<arr2.length;j++){
if(arr1[i]==arr2[j]){
hs.add(arr1[i]);
break;// further not check ,
}
}
}
for( int no :hs){
System.out.print(no+" ");
}
}
}
/*
1. use hashset for add all duplicate number , as hashset only save unique elements
2.
*/
/*
ramannegi@Ramans-MacBook-Pro x % javac C_53.java
ramannegi@Ramans-MacBook-Pro x % java C_53
3 4 8 %
*/
/* C_54 - Common elements in 2 arrays in java- Hashset -2 */
import java.util.HashSet;
public class C_54 {
public static void main(String[] args) {
int[] arr1 = { 4, 3, 7, 9, 2};
int[] arr2 = { 5, 1, 4, 8, 3};
// int[] arr2 = { 5, 1, 4, 8, 3, 5 }; it will give 5 also but 5 is not common in both,
HashSet<Integer> hs = new HashSet<>();
for( int no : arr1){
hs.add(no); // boolean o/p
}
for( int no : arr2){
boolean b = hs.add(no);
if(b==false){
System.out.print(no+" ");
}
}
}
}
/*
1. Assume no duplicate values.
2. make hashset object and start add both array elements
3. if the elements already present, it will not add and we will print it.
*/
/*
ramannegi@Ramans-MacBook-Pro x % javac C_54.java
ramannegi@Ramans-MacBook-Pro x % java C_54
4 3 %
*/
/* C_55 - - Common elements in 2 arrays in java- Hashset - 3 */
import java.util.HashSet;
public class C_55 {
public static void main(String[] args) {
int[] arr1 = { 4, 3, 7, 9, 2, 4};
int[] arr2 = { 5, 1, 4, 8, 3, 5};
HashSet<Integer> hs1 = new HashSet<>();
HashSet<Integer> hs2 = new HashSet<>();
for( int no : arr1){
hs1.add(no); // unique number save with in arr1 array
}
for( int no : arr2){
hs2.add(no); // unique number save with in arr2 array
}
for( int no : hs2){
boolean b = hs1.add(no); // only unique number save in hs2 object
if(b==false){
System.out.println(no+" ");
}
}
}
}
/*
1. if duplicate also works
2. make two object of hashset
3. add arr1 in first object, it only save unique elements form arr1 ,
4. same thing is arr2.
5. now add element form one hash object to another ,so it will save only the unique value
*/
/*
ramannegi@Ramans-MacBook-Pro x % javac C_55.java
ramannegi@Ramans-MacBook-Pro x % java C_55
3
4
*/
/* C_56 - common elements in three sorted array */
import java.util.ArrayList;
public class C_56 {
public static void main(String[] args) {
int[] a1 = {2, 4, 8};
int[] a2 = {2, 3, 4, 8, 10, 16};
int[] a3 = {2, 8, 14, 40};
int x=0,y=0,z=0;
ArrayList<Integer> al = new ArrayList<Integer>();
while(x<a1.length && y<a2.length && z<a3.length){
if(a1[x]==a2[y] && a2[y]==a3[z]){
al.add(a1[x]);
x++;
y++;
z++;
}
else if (a1[x] < a2[y]){
x++; // sorted array , means 4 is not present in a2 hence we will check for a1=8 , if above true
}
else if(a2[y] < a3[z]){
y++; // means 3 is not present in a3, skip three for a3=4 ,
}
else{
z++; // none above match , skip to next position
}
}
for(int no:al){
System.out.print(no+" ");
}
}
}
/*
1. take 3 variables for all 3 array index positions x y z respectively
2. case = check x[0]= y[0], if true, check y[0]=z[0] , print or save the element ;
3. case = check for x[0]!=y[0], with sorted list, and element is less, than shift to next element
4. if next elemnt if equal , then check the element in z list,
5. code logic
6. if x[0] = y[0] = z[0], add the elemnt in array list , increment all index
7. if x[1] != y[1], check
*/
/*
ramannegi@Ramans-MacBook-Pro x % javac C_56.java
ramannegi@Ramans-MacBook-Pro x % java C_56
2 8 %
*/
/* C_57 - Longest Consecutive Sequence in Array */
import java.util.HashSet;
public class C_57 {
public static void main(String[] args) {
int[] a ={3, 9, 1, 10, 4, 20, 2};
HashSet<Integer> hs = new HashSet<>();
for( int i=0;i<a.length;i++){
hs.add(a[i]); // savc array elements in hasset
}
int long_leng=0;
for( int i=0;i<a.length;i++){
if(!hs.contains(a[i]-1)){ // is element se 1 kam koi element present hai ki nahe ,not operator
int no=a[i]; //
while(hs.contains(no)){ // if present , then check the next number
no++;
}
if(long_leng < no-a[i]){ // abhi 2 lenght hai
long_leng=no-a[i];
}
}
}
System.out.println("longest sun seq lenght is ="+long_leng);
}
}
/* logic
01. add in hashset, remove duplicate
02. position traverse form 0 index
03. find less than index[0] element (i.e=3)
04. coz list me 3 se chota elemtn present hai to humko 3 se to shuru karna hee hai
05. now check for element 9 and find the less than value which is 8 not preset
then i will startthe lsietform 9 and start traverse the list
06. ab mai list ko 9 se shuru kaarwauga
07. ab 9+1, check karuga pir 10+1 = 11, nahe hai to switch karuga next element par.
08. 1 ke leeaye karuga kam element , agar nahe to
09. 1+1 = chaeck karuga ,if true then
2+1 = check , if true then
3+1 = check , if found, then
4+1 = check , if found , then
5+1 = check , if not found , then find length
10. mana a[i] =3, par choti value milti hai to jo 4 hai to
a[i] =4, vavlue par aage ki value check karege aur true hoen par no ki value ko increment
n0 = a[i] ki value ko increment karege
maan lete ai ki 6 tak hai to a[i] = 4 hameysha raheyge ,
lakin no= no+1= 5 aur phir no = no+1=6 me jayege |
tab longest vulue hogi ( no-a[i]6 - 4 ) = 2
*/
/*
ramannegi@Ramans-MacBook-Pro x % javac C_57.java
ramannegi@Ramans-MacBook-Pro x % java C_57
longest sun seq lenght is =4
*/
/* C_58 - maximun sum subarray kadane's algorithem */
public class C_58 {
public static void main(String[] args) {
int[] a ={4, -2, -3, 4, -1, -2, 1, 5, -3};
int max_num = Integer.MIN_VALUE;
int add_result = 0;
int s=0; int start=0; int end=0;
for(int i=0; i<a.length;i++){
add_result = add_result +a[i];
if(max_num < add_result){ //if result is big then
max_num = add_result; // update max_num
start = s; end = i;
}
if(add_result < 0){ // if resut is < 0 then
add_result = 0; // reset result=0, for adding next elements
s=i+1; // starting position for next sub array
}
}
System.out.println("the max number is = "+ max_num);
System.out.println("Starting Position is = "+start);
System.out.println("Ending Position is = "+end);
}
}
/*
0. Code Logic
First element ko add karege ad_result me
agar add result > max_num se , update max_num
agar add result < max_num se , max_num = unchanged , search for next number
1. max_number = sabse bada sum of sub array , initially min int value ,
2. add_result = element ka sum karta hai
2. agar 3 sub array hai aur meh = 3 4 2 hai to , msf me 4 save hoga
max_result = -value;
add_result = 0;
start=0
end=0
i=0 , 4 , add_result = 4+0=4, > max_num , true , max_num = 4, s=0, end= 0.
i=1 ,-2 , add_result = 4+-2=2, > max_num 4, false, max_num = 4, s=0, end= 1,
i=2 ,-3 , add_result = 2+-3=-1, > max_num 4, false, max_num = 4, s=0, end= 2,
but add_result =-1 so reset max_num = 0, and s= 3+1 = 4.
i=3 , 4 , add_result = 0+4=4, > max_num 4, false, max_num = 4, s=3, end= 3,
i=4 ,-1 , add_result = 4+-1=3, > max_num 4, false, max_num = 4, s=3, end= 4,
i=5 ,-2 , add_result = 3+-2=1, > max_num 6, false, max_num = 6, s=3, end= 5,
i=6 ,1 , add_result = 1+1=2, > max_num 6, false, max_num = 6, s=3, end= 6,
i=7 ,5 , add_result = 2+5=7, > max_num 6, true , max_num = 7, s=3, end= 7,
i=8 ,-3 , add_result = 2+-3=1, > max_num 6, false, max_num = 4, s=0, end= 2,
but add_result =-1 so reset max_num = 0, and s= 4+1 = 5.
*/
/*
ramannegi@Ramans-MacBook-Pro x % javac C_58.java
ramannegi@Ramans-MacBook-Pro x % java C_58
the max number is = 7
Starting Position is = 3
Ending Position is = 7
*/
/* C_59 - insert an element into an array */
public class C_59 {
public static void main(String[] args) {
int[] a = {10,20,30,40,50};
int pos=3;
int element =100;
for( int i =a.length-1 ; i>pos-1 ; i--){ // ( int i =4 ; i>(3-1) ; i--)
a[i]=a[i-1];
}
a[pos-1] = element; // update the value
for( int i=0;i<a.length;i++){
System.out.print(a[i]+ " ");
}
}
}
/*
1. Traverse form back.
2. Put second last value in last position.
3. till the position reached
4. a.length-1, second last value
5. i>pos-1, till 40 element,
*/
/*
ramannegi@Ramans-MacBook-Pro x % javac C_59.java
ramannegi@Ramans-MacBook-Pro x % java C_59
10 20 100 30 40 %
*/
/* C_60 delete an element from an array */
public class C_60 {
public static void main(String[] args) {
int[] a ={10, 40, 30, 80, 60, 20};
int del_ele = 30;
for(int i=0;i<a.length;i++){
if(del_ele == a[i]){
for(int j=i; j<a.length-1; j++){ // j<a.length-1 , prevents index outofBoundArray
a[j] = a[j+1];
}
break; // if found, break the current loop
}
}
for(int i=0;i<a.length-1;i++){
System.out.print(a[i]+ " ");
}
}
}
/*
1. Serach the selected element in the array
2. When found ,start traversing form that element and put next element to its position
*/
/*
ramannegi@Ramans-MacBook-Pro x % javac C_60.java
ramannegi@Ramans-MacBook-Pro x % java C_60
10 40 80 60 20 %
*/
/* C_61 - remove duplicate element form sorted, unsorted arrray */
public class C_61 {
public static void main(String[] args) {
int[] a = {1, 2, 2, 3, 4, 5, 5, 5, 5};
int[] temp = new int[a.length];
int j=0;
for(int i=0;i<a.length-1;i++){ // i<a.length-1, prevents outofBound array
if(a[i]!=a[i+1]){ // not equals, then insert
temp[j]=a[i];
j++;
}
}
temp[j]=a[a.length-1]; // for last array
// print all element
for( int i=0;i < temp.length;i++){
System.out.print(temp[i]+ " ");
}
System.out.println("\n w/o using temp variables");
/* without using temp */
int k=0;
for(int m=0;m<a.length-1;m++){ // i<a.length-1, prevents outofBound array
if(a[m]!=a[m+1]){ // not equals, then insert
a[k]=a[m];
m++;
}
}
a[k]=a[a.length-1]; // for last array
// print all element
for( int i=0;i < a.length;i++){
System.out.print(a[i]+ " ");
}
}
}
/*
1. create temp array of length of origianl array size
for sorted array
2. if current element is equal to next element , then insret the value, otherwise not
*/
/*
ramannegi@Ramans-MacBook-Pro x % javac C_61.java
ramannegi@Ramans-MacBook-Pro x % java C_61
1 2 3 4 5 0 0 0 0
w/o using temp variables
5 2 2 3 4 5 5 5 5 %
*/
/* C_62 merge two array in java*/
public class C_62 {
public static void main(String[] args) {
int[] a = {10,20,30};
int[] b = {40,50,60,70,80};
int a_leng = a.length;
int b_leng = b.length;
int c_leng = a_leng + b_leng;
int[] c = new int[c_leng];
for( int i=0 ; i<a_leng ; i++){
c[i] = a[i]; // start form c=0 index
}
for( int i=0 ; i<b_leng ; i++){
c[a_leng+i] = b[i]; // start form 4th position
}
for( int i=0 ; i<c.length ; i++){
System.out.print(c[i]+" " );
}
}
}
/*
1. Create another array with lenght of array1 + array2
2. Fisrt put first array element in c array
3. Then put second array element in c array
4.
*/
/*
ramannegi@Ramans-MacBook-Pro x % javac C_62.java
ramannegi@Ramans-MacBook-Pro x % java C_62
10 20 30 40 50 60 70 80 %
*/