November 24, 2018

Comparing String with its permutation in java

/**
 * Consumerfed
 */
package com.cfed.logicz;

import java.util.Scanner;

/**
 * @author Consumerfed Regional office kozhikode
 *
 */
public class StringPermutation {

/**
* @param args
*/
public static void main(String[] args) {

Scanner sc = new Scanner(System.in);
int count = sc.nextInt();
sc.nextLine();
String[] inputs = new String[count];
for(int j =0;j<count;j++) {
inputs[j] = sc.nextLine();
}

for(String input : inputs) {
String str1 = input.split("\\s")[0];
String str2 = input.split("\\s")[1];
// String str1 = "konzerntech";
// String str2 = "consumerfed";
int len = 0;
if ((len = str1.length()) != str2.length()) {
System.out.println("NO");
} else {
int val1 = 0;
int val2 = 0;
for (int i = 0; i < len; i++) {
val1 = str1.charAt(i) + val1;
val2 = str2.charAt(i) + val2;
}
if (val1 != val2) {
System.out.println("NO");
} else {
System.out.println("YES");
}
}
}
}
}

November 09, 2018

Static in Java - A Simple Java program

/**
 * Static variable in java
 *
 * Since JDK 1.7, it is not possible to execute a java class without the main method.
 */
package com.cfed.tutorials;

/**
 * @author consumerfed
 *
 */
public class Student {

int rollNo =0;
String studentName;
static int counter = 0; 
int count=0;



public Student(int rollNo, String studentName){
this.rollNo = rollNo;
this.studentName = studentName;
counter ++;
count++;
}

/**
* The static method can not use non static data member or call non-static method directly.
*
* this and super cannot be used in static context.
*
*/
// public static void display() {
// System.out.println(" roll No :"+rollNo);
// print();
// }
/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
Student s1 = new Student(1, "sachin");
Student s2 = new Student(2, "dravid");
Student s3 = new Student(3, "saurav");
s3.print();
// Student.display();

}
public void print() {
// TODO Auto-generated method stub
System.out.println(" counter : "+counter);
System.out.println(" count : " + count);

}

}

November 08, 2018

Find Factorial of a number in java - Consumerfed

/**
 *
 */
package com.hackerearth.basicprograming;

import java.util.Scanner;

/**
 * @author consumerfed
 *
 */
public class FindFactorial {

/**
* @param args
*/
public static void main(String[] args) {


Scanner sc = new Scanner(System.in);
int n = sc.nextInt();
long t = System.currentTimeMillis();
int fact = 1;
while(n>=2) {
fact = fact * n;
n--;
}
System.out.println(fact);
System.out.println(System.currentTimeMillis()-t);
}

}

November 07, 2018

To get the peak value for one dimensional array in java

/**
 * To get the peak value for one dimensional Array
 */
package com.cfed.algorithms;

/**
 * @author consumerfed
 *
 */
public class PeakFinders {

/**
* @param args
*/
public static void main(String[] args) {
int[] ar = {3,8,10,13,5,25,4,8,9,1};
PeakFinders pf = new PeakFinders();
pf.findThePeakValue(ar,ar.length);
}

private void findThePeakValue(int[] ar,int len) {
int peak = 0;
for(int i=0;i<len-1;i++) {
int comp = ar[i+1];
if(comp>ar[i] && comp>ar[(i+2)] && comp > peak) {
peak = comp;
}
}
System.out.println("The peak value is : "+peak);
}
}

November 05, 2018

Magic Square Matrix in Java

/**
 * https://www.instagram.com/the_bulleteer/
 */
package com.cfed.javaTricks;

/**
 * @author nijesh
 *
 */
public class MagicSquareMatrix {

public static void main(String[] args) {

    int n = 9;
    int[][] magicSquare = new int[n][n];

    int number = 1;
    int row = 0;
    int column = n / 2;
    int curr_row;
    int curr_col;
    while (number <= n * n) {
        magicSquare[row][column] = number;
        number++;
        curr_row = row;
        curr_col = column;
        row -= 1;
        column += 1;
        if (row == -1) {
            row = n - 1;
        }
        if (column == n) {
            column = 0;
        }
        if (magicSquare[row][column] != 0) {
            row = curr_row + 1;
            column = curr_col;
            if (row == -1) {
                row = n - 1;
            }
        }
    }

    for (int i = 0; i < magicSquare.length; i++) {
        for (int j = 0; j < magicSquare.length; j++) {
            System.out.print(magicSquare[i][j] + "  ");
        }
        System.out.println();
    }
}


}

SQL Where IN Having and Group by Query Sample

customer table



select * from customer where country = 'IN'



select * from customer where country in ('in','us');


select * from customer having country = 'in'




select * from customer GROUP BY country



select customer_name,sum(credit) as balance from customer group by country;



select customer_name,sum(credit) as balance from customer where country in ('in','us','jp');



select customer_name,sum(credit) as balance from customer group by country having balance > 1800;



Thank you..

November 04, 2018

Find the set of array combination by removing the duplicate

/**
 * Find the possible combinations of max and min number in a given array by removing the duplicates
 *
 * Thanks to sreejith
 */
package com.cfed.logicz;

import java.util.Arrays;

/**
 * @author consumerfed
 *
 */
public class ArrayCombinations {

/**
* @param args
*/
public static void main(String[] args) {
int ar[] = { 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 4, 4, 4, 4, 4, 4, 4, 2, 2, 2, 2, 2, 3, 3, 3, 3, 3,
1, 1, 1, 1, 1, 5, 5, 5, 5, 3, 3, 3 };
int arraySize = ar.length;
Arrays.sort(ar);
System.out.println(ar[0]);
int duplicateCount = 0;
for (int i = 1; i < arraySize; i++) {
if (ar[i - 1] == ar[i]) {
duplicateCount = duplicateCount + 1;
}
}
int actualSize = arraySize - duplicateCount;
int output =(actualSize * (actualSize -1))/2;
System.out.println(output);
}



}

Output

10

Facebook comments