C,C++/JAVA/BASH/ASM ARENA

वह प्रदीप जो दीख रहा है झिलमिल दूर नही है थक कर बैठ गये क्या भाई मन्जिल दूर नही है चिन्गारी बन गयी लहू की बून्द गिरी जो पग से चमक रहे पीछे मुड देखो चरण-चिनह जगमग से बाकी होश तभी तक, जब तक जलता तूर नही है थक कर बैठ गये क्या भाई मन्जिल दूर नही है अपनी हड्डी की मशाल से हृदय चीरते तम का, सारी रात चले तुम दुख झेलते कुलिश का। एक खेय है शेष, किसी विध पार उसे कर जाओ; वह देखो, उस पार चमकता है मन्दिर प्रियतम का। आकर इतना पास फिरे, वह सच्चा शूर नहीं है; थककर बैठ गये क्या भाई! मंज़िल दूर नहीं है। दिशा दीप्त हो उठी प्राप्त कर पुण्य-प्रकाश तुम्हारा, लिखा जा चुका अनल-अक्षरों में इतिहास तुम्हारा। जिस मिट्टी ने लहू पिया, वह फूल खिलाएगी ही, अम्बर पर घन बन छाएगा ही उच्छ्वास तुम्हारा। और अधिक ले जाँच, देवता इतन क्रूर नहीं है। थककर बैठ गये क्या भाई! मंज़िल दूर नहीं है।

TJU 2199. A+B Problem May 30, 2010

Filed under: C,C++ Programs,SEARCHING,SORTING,TJU — whoami @ 23:30
Tags: ,

TJU 2199. A+B Problem
AC
This problem requires Sorting and Searching(Binary) approch

#include<stdio.h>
#include<string.h>
#include<stdlib.h>
#include<math.h>
#include<iostream>
#include<algorithm>
using namespace std;


int main()
{
 int i,j,k;
 int a[1009],n;
 int tmp,diff;
 int low,mid,high,flag,num;
 while(1){
    scanf("%d",&n);
    if(n==0) break;
    for(i=0;i<n;i++)
       scanf("%d",&a[i]);
    
    sort(a,a+n);//sort
    
    for(i=n-1;i>=0;i--){
       tmp=a[i];
       for(j=i-1;j>=0;j--){
         diff=tmp-a[j];
         high=j-1;low=0;
         flag=0;
         mid=(low+high)/2;
         while(low<=high){//binary search
            mid=(low+high)/2;
            
            if(a[mid]==diff){ flag=1;num=tmp;goto down;}
            else if(diff<a[mid]){high=mid-1;}
            else if(diff>a[mid]){low=mid+1;}
         }
       }
      
    }
     down:
          if(flag==1) printf("%d\n",tmp);
          else printf("-1\n");
   }    

return 0;
}


 

TJU 1547. To and Fro

Filed under: C,C++ Programs,TJU — whoami @ 22:39
Tags: ,

After a long time , playing TJU contest
TJU 1547. To and Fro
AC

#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#include<math.h>

int main()
{
 int i,j,k;
 int n;
 char s[205];
 char str[100][21],tmp[100][21];
 while(1){

 scanf("%d",&n);
 if(n==0) break;
 scanf("%s",s);
 k=0;j=0;
 for(i=0;s[i]!='\0';){
  tmp[j][k]=s[i];
  i++;
  if(i%n==0)
     j=j+1;
    
  if(j%2==0&&i%n!=0) k=k+1;
  else if(j%2!=0&&i%n!=0) k=k-1;
  
  }
     
  for(i=0;i<n;i++){
      for(k=0;k<j;k++){
          printf("%c",tmp[k][i]);
      }
   }
    printf("\n");
      
 }


return 0;
}



 

Reverse
 the
 order 
of
 words 
in 
a
 string in place May 23, 2010

Filed under: C,C++ Programs,GOOGLE — whoami @ 07:47
Tags: , ,

Q
Classic
Question
#:
Reversing
the
words
in
a
string

Write
a
function
to
reverse
the
order
of
words
in
a
string
in
place.


Ans:
Reverse 
the 
string
 by
 swapping 
the
 first
 character
 with
 the
 last
 character,
the
 second 
character 
with 
the 
second to‐last
 character,
 and
 so
on.

 Then,
go 
through
 the
 string
 looking
 for
 spaces,
so 
that
 you
 find
 where
 each
 of 
the 
words
 is.

Reverse
 each
 of 
the 
words
 you
 encounter
 by 
again 
swapping
 the
 first 
character 
with 
the 
last

character,
the
 second
 character 
with
 the
se cond‐to‐last
 character,
 and
 so
on.


//Mysolution inplace reversal of order of words
//inplace reversal of order of words 
#include<stdio.h>
#include<string.h>

int main()
{
  char s[1000],c;
  int i,j,k,l;
  
  gets(s);
  l=strlen(s);
  for(i=0;i<l/2;i++){
    c=s[i];
    s[i]=s[l-i-1];
    s[l-i-1]=c;
    }
 
     for(i=0;s[i]!='\0';){
      j=i;
     
      while(s[i]!=' ') { 
          if(s[i]=='\0') {break;}
          i++;
          }
      for(k=j,l=i-1;k<=l;k++,l--){
          c=s[k];
          s[k]=s[l];
          s[l]=c;
          }
       if(s[i]==' ') i=i+1;
    
      }     


  puts(s);

return 0;
}    

Sample

input: This world is beautiful
ouput: beautiful is world This

input: This is a sample input output
output: output input sample a is This

 

Minimum Scalar Product May 21, 2010

Filed under: C,C++ Programs,Google Code Jam — whoami @ 20:31
Tags: , ,

GOOGLE CODE JAM 2008
Round 1A 2008

AC for both Smaller and Bigger Data

#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#include<math.h>
#include<algorithm>

using namespace std;
int main()
{
  FILE *p=fopen("a.txt","w");
  FILE *in=fopen("A-large-practice.in","r");
  long long int t,n,i,j,k;
  long long int x[10000];
  long long int y[10000];
  long long int total;
  long long int c=0;
  fscanf(in,"%lld",&t);
  while(t--){
    fscanf(in,"%lld",&n);
    for(i=0;i<n;i++)
      fscanf(in,"%lld",&x[i]);
    for(i=0;i<n;i++)
      fscanf(in,"%lld",&y[i]);

    sort(x,x+n);
    sort(y,y+n);
    total=0;
    for(i=0,j=n-1;i<n;i++,j--)
      total+=(long long int )(x[i]*y[j]);
    fprintf(p,"Case #%lld: %lld\n",++c,total);
   }
  fclose(p);
  fclose(in);
return 0;
}


 

Implementing Raid on PenDrive May 20, 2010

Filed under: LINUX — whoami @ 11:00
Tags: ,

Just like USB hard drive , we can implemnt RAID (level 0 or 1 )in LINUX in Pendrive
Here is the implementation details on my system
[root@localhost ~]# fdisk /dev/sdb
Device contains neither a valid DOS partition table, nor Sun, SGI or OSF disklabel
Building a new DOS disklabel with disk identifier 0xa515f612.
Changes will remain in memory only, until you decide to write them.
After that, of course, the previous content won’t be recoverable.

Warning: invalid flag 0x0000 of partition table 4 will be corrected by w(rite)

Command (m for help): d
No partition is defined yet!

Command (m for help): n
Command action
e extended
p primary partition (1-4)
p
Partition number (1-4): 1
First cylinder (1-1024, default 1):
Using default value 1
Last cylinder, +cylinders or +size{K,M,G} (1-1024, default 1024): 300

Command (m for help): n
Command action
e extended
p primary partition (1-4)
p
Partition number (1-4): 2
First cylinder (301-1024, default 301): 500
Last cylinder, +cylinders or +size{K,M,G} (500-1024, default 1024): ^C
[root@localhost ~]# fdisk /dev/sdb
Device contains neither a valid DOS partition table, nor Sun, SGI or OSF disklabel
Building a new DOS disklabel with disk identifier 0x96275f3e.
Changes will remain in memory only, until you decide to write them.
After that, of course, the previous content won’t be recoverable.

Warning: invalid flag 0x0000 of partition table 4 will be corrected by w(rite)

Command (m for help): p

Disk /dev/sdb: 4063 MB, 4063232000 bytes
125 heads, 62 sectors/track, 1024 cylinders
Units = cylinders of 7750 * 512 = 3968000 bytes
Disk identifier: 0x96275f3e

Device Boot Start End Blocks Id System

Command (m for help): n
Command action
e extended
p primary partition (1-4)
p
Partition number (1-4): 1
First cylinder (1-1024, default 1):
Using default value 1
Last cylinder, +cylinders or +size{K,M,G} (1-1024, default 1024): 300

Command (m for help): n
Command action
e extended
p primary partition (1-4)
p
Partition number (1-4): 2
First cylinder (301-1024, default 301):
Using default value 301
Last cylinder, +cylinders or +size{K,M,G} (301-1024, default 1024): 506

Command (m for help): n
Command action
e extended
p primary partition (1-4)
p
Partition number (1-4): 3
First cylinder (507-1024, default 507):
Using default value 507
Last cylinder, +cylinders or +size{K,M,G} (507-1024, default 1024): 800

Command (m for help): n
Command action
e extended
p primary partition (1-4)
p
Selected partition 4
First cylinder (801-1024, default 801):
Using default value 801
Last cylinder, +cylinders or +size{K,M,G} (801-1024, default 1024):
Using default value 1024

Command (m for help): p

Disk /dev/sdb: 4063 MB, 4063232000 bytes
125 heads, 62 sectors/track, 1024 cylinders
Units = cylinders of 7750 * 512 = 3968000 bytes
Disk identifier: 0x96275f3e

Device Boot Start End Blocks Id System
/dev/sdb1 1 300 1162469 83 Linux
/dev/sdb2 301 506 798250 83 Linux
/dev/sdb3 507 800 1139250 83 Linux
/dev/sdb4 801 1024 868000 83 Linux

Command (m for help): t
Partition number (1-4): 1
Hex code (type L to list codes): fd
Changed system type of partition 1 to fd (Linux raid autodetect)

Command (m for help): t
Partition number (1-4): fd
Partition number (1-4): 2
Hex code (type L to list codes): fd
Changed system type of partition 2 to fd (Linux raid autodetect)

Command (m for help): t
Partition number (1-4): 3
Hex code (type L to list codes): fd
Changed system type of partition 3 to fd (Linux raid autodetect)

Command (m for help): t
Partition number (1-4): 3
Hex code (type L to list codes): fd

Command (m for help): t
Partition number (1-4): 4
Hex code (type L to list codes): fd
Changed system type of partition 4 to fd (Linux raid autodetect)

Command (m for help): p

Disk /dev/sdb: 4063 MB, 4063232000 bytes
125 heads, 62 sectors/track, 1024 cylinders
Units = cylinders of 7750 * 512 = 3968000 bytes
Disk identifier: 0x96275f3e

Device Boot Start End Blocks Id System
/dev/sdb1 1 300 1162469 fd Linux raid autodetect
/dev/sdb2 301 506 798250 fd Linux raid autodetect
/dev/sdb3 507 800 1139250 fd Linux raid autodetect
/dev/sdb4 801 1024 868000 fd Linux raid autodetect

Command (m for help): w
The partition table has been altered!

Calling ioctl() to re-read partition table.
Syncing disks.
[root@localhost ~]# mdadm –create –verbose /dev/md0 –level=0 –raid-devices=2 /dev/sdb1 /dev/sdb2
mdadm: chunk size defaults to 64K
mdadm: array /dev/md0 started.
[root@localhost ~]# mdadm –create –verbose /dev/md0 –level=1 –raid-devices=2 /dev/sdb3 /dev/sdb4
mdadm: size set to 867904K
mdadm: largest drive (/dev/sdb3) exceeds size (867904K) by more than 1%
Continue creating array? y
mdadm: /dev/md0 is already in use.
[root@localhost ~]# mdadm –create –verbose /dev/md1 –level=1 –raid-devices=2 /dev/sdb3 /dev/sdb4
mdadm: size set to 867904K
mdadm: largest drive (/dev/sdb3) exceeds size (867904K) by more than 1%
Continue creating array? y
mdadm: array /dev/md1 started.
[root@localhost ~]# mkfs.ext3 /dev/md0
mke2fs 1.41.9 (22-Aug-2009)
Filesystem label=
OS type: Linux
Block size=4096 (log=2)
Fragment size=4096 (log=2)
122640 inodes, 490128 blocks
24506 blocks (5.00%) reserved for the super user
First data block=0
Maximum filesystem blocks=503316480
15 block groups
32768 blocks per group, 32768 fragments per group
8176 inodes per group
Superblock backups stored on blocks:
32768, 98304, 163840, 229376, 294912

Writing inode tables: done
Creating journal (8192 blocks): done
Writing superblocks and filesystem accounting information: done

This filesystem will be automatically checked every 25 mounts or
180 days, whichever comes first. Use tune2fs -c or -i to override.
[root@localhost ~]# mkfs.ext3 /dev/md1
mke2fs 1.41.9 (22-Aug-2009)
Filesystem label=
OS type: Linux
Block size=4096 (log=2)
Fragment size=4096 (log=2)
54320 inodes, 216976 blocks
10848 blocks (5.00%) reserved for the super user
First data block=0
Maximum filesystem blocks=222298112
7 block groups
32768 blocks per group, 32768 fragments per group
7760 inodes per group
Superblock backups stored on blocks:
32768, 98304, 163840

Writing inode tables: done
Creating journal (4096 blocks): done
Writing superblocks and filesystem accounting information: done

This filesystem will be automatically checked every 21 mounts or
180 days, whichever comes first. Use tune2fs -c or -i to override.

 

Compression (SCANPATH) -strings May 18, 2010

Filed under: C,C++ Programs,Compression — whoami @ 23:38
Tags: ,

Though Huffman coding is one of the best way to do data compression , this  scanpath compression is helpful in many circumstances

Read it here