Pages

Rajan Array

. A Rajan array has the following property.
   a[0] = a[1] + a[2] = a[3] + a[4] + a[5] = a[6] + a[7] + a[8] + a[9] = ...
The length of a Rajan array must be n*(n+1)/2 for some n.

Write a method named isRajanArray that returns 1 if its array argument is a Rajan array, otherwise it returns 0. If you are programming in Java or C# the function signature is
   int isRajanArray(int[ ] a)

If you are programming in C or C++, the function signature is
   int isRajanArray(int a[ ], int len) where len is the number of elements in a.

Examples
if a is
return
Reason
{2, 1, 1}
1
2 + 1 + 1
{2, 1, 1, 4, -1, -1}
1
2 = 1 + 1, 2 = 4 + -1 + -1
{6, 2, 4, 2, 2, 2, 1, 5, 0, 0}
1
6 = 2 + 4, 6 = 2 + 2 + 2, 6 = 1 + 5 + 0 + 0
{18, 9, 10, 6, 6, 6}
0
18 != 9 + 10
{-6, -3, -3, 8, -5, -4}
0
-6 != 8 + -5 + -4
{0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1, -2, -1}
1
0 = 0 + 0, 0 = 0 + 0 + 0, 0 = 0 + 0 + 0 + 0,
0 = 1 + 1 + 1 + -2 + -1
{3, 1, 2, 3, 0}
0
The length of the array is 5, but 5 does not equal n*(n+1)/2 for any value of n.



Solution:



int isRajanArray(int[] arr){
 
 int sumLength = 0;
 
 for(int i=1; sumLength < arr.length; i++){
  sumLength += i;
 }
 
 if(sumLength != arr.length){
  return false;
 }
 
 //logic to check the sum
 int sum = arr[0];
 
 sumLength = 0;
 int index = 0;
 
 int sum2 = 0;
 for(int j =1; sumLength < arr.length; j++){
  sum2 = 0;
  
  for(int k=0; k<j; k++){
   sum2+=arr[index++];
  }
  if(sum != sum2){
     return false;
     }
 }
 
 return true;
 
 
}

Enjoy

Prime count between two numbers | java

I was facing an algorithm problem where I have to find the total number of integers between two given integer. The two integers (start, end) count be any integer where start < end and they are both positive integers.

So here's what i did.

public static int primeCount(int start, int end){
  
  int count = 0;
  int mod = 0;
 
  firstloop:
  for(int i=start; i<=end; i++){
   
   if(i<=1){
    continue;
   }else if(i==2){
    count++;
    System.out.println(i);
    continue;
   }
   
   //secondloop:
   for(int j=2; j<=(int)(i/2); j++){
    mod = i % j;
    //System.out.println(i + "%" + j + " = " + mod);
    if(mod==0){
     //count++;
     //System.out.println(i);
     continue firstloop ;
    }
   }
   count++;
   System.out.println(i);
  }
  
  return count;
 }

one interesting thing here what is used is the labeled break continue feature of java which even many experienced professionals do not normally remember. You count specify the label in the break or continue statement to target which for loop you want to break or continue.

So you learned new thing. Enjoy..

Wonderful world of CSS Sprites

http://www.smashingmagazine.com/2009/04/27/the-mystery-of-CSS-sprites-techniques-tools-and-tutorials/

Portfolio in the web| Things to learn

http://www.adhamdannaway.com/blog/web-design/imitation-truly-is-the-sincerest-form-of-flattery

Reverse search in shell

I didn't know about it earlier. So I thought of writing about it.

If you are or shell user, there have been some situations where you have solved some problems with some shell commands. Then you remember only a part of the command and when you need the same command again, you can't remember exactly what the command was. Well, this is for people like you.

Reverse search helps you find the command you have typed in the past, with a little bit of help from you. It takes small fragment of command how much you remember, and suggests similar command you have applied in the past.

How to use it?

  • Press Ctrl + R in the shell
  • Type how much of the command you could remember
  • Use the arrow key, or Ctrl + R again to find the correct command
  • Press Enter finally when you found it.
  • Press Ctrl + G to get out of the search mode.