How to find largest and smallest No in unsorted array




How to find out the min & max form an unsorted array

Logic:

Suppose an Array value 11,1,23,13,7

Step 1:

Put array first value in min and max variable
Min = 11
Max = 11

Step 2:

Through loop compare min and min value with I variable

If( Min > array index value) then
Min = array index value
Else
Max = array index value





 public static string maxAndMin(int[] arr)

        {
            int min = arr[0];
            int max = arr[0];
            // 11 1 13 25 17
            for (int i = 1; i <= arr.Length - 2; i++)
            {
                   if (arr[i] > max)
                    {
                        
                         max = arr[i];
                      
                    }
                    else
                    {
                       
                        min = arr[i];
                      
                    }
            }
            return min +" " +max;
        }



static void Main(string[] args)
        {
            Console.WriteLine("**********************************************************************");
            Console.WriteLine("How to find largest and smallest No in unsorted array ");
            Console.WriteLine("**********************************************************************");

            Console.WriteLine("Enter the size of array");
            int ArraySize= Convert.ToInt32(Console.ReadLine());
            Console.WriteLine("Enter the array element");
            int[] arr = new int[ArraySize];
            for (int i = 0; i<= ArraySize - 1; i++)
            {
                arr[i]= Convert.ToInt32(Console.ReadLine());
            }
            Console.WriteLine(" Result  " + Program.maxAndMin(arr));
          
            Console.ReadLine();

        }


OutPut 

**********************************************************************

How to find largest and smallest No in unsorted array
**********************************************************************
Enter the size of array
5
Enter the array element
11
1
13
25
23

Result  1 25



Share:

Determine if any two integers in array equal to sum of given number




//------------------------------

1.     
  Determine if any two integers in array equal to sum of given number


        public static string CheckArraySum(int[] ar, int found)

        {
            for (int i = 0; i <= ar.Length - 1; i++)
            {
                for (int j = i+1; j<= ar.Length - 1; j++)
                {
                    if (ar[i] + ar[j] == found)
                    {
                        return "Sum Exist ! Values are " + ar[i] + " " + ar[j];
                    }
                }
            }

            return " Value "+ found +"Not in array ";
        }
      
//-----------------------------------------------------------------------------------

        static void Main(string[] args)
        {
 Console.WriteLine("Determine if any two intergers in array equal to sum of given number \n");

            Console.WriteLine("Enter array Size");
            int arraysize = Convert.ToInt32(Console.ReadLine());
            int[] arr = new int[arraysize];

            Console.WriteLine("Enter the array Value");
            for (int i = 0; i <= arraysize - 1; i++)
            {
                arr[i]= Convert.ToInt32(Console.ReadLine());
            }

            Console.WriteLine("Enter the value you want find");

            int val = Convert.ToInt32(Console.ReadLine());
            Console.WriteLine("Result"+ Program.CheckArraySum(arr, val));
            Console.ReadLine();
        }
    
Share:

How to count the occurrence of no of char in a string and asci value of char






How to count the occurrence of no of char in a string and asci value of char

For example:
Input : ShivKumarSingh
OutPut :
S =  Ascii Value = 83 and Count = 4
h =  Ascii Value = 104 and Count = 2
i =  Ascii Value = 105 and Count = 2
v =  Ascii Value = 118 and Count = 1
K =  Ascii Value = 75 and Count = 1
u =  Ascii Value = 117 and Count = 1
m =  Ascii Value = 109 and Count = 1
a =  Ascii Value = 97 and Count = 1
r =  Ascii Value = 114 and Count = 1
n =  Ascii Value = 110 and Count = 1
g =  Ascii Value = 103 and Count = 1





public static void CountAccurance(String str) { char[] ch = str.ToCharArray(); for (int i = 0; i <= str.Length - 1; i++) { if (ch[i] != '0') { int count = 1; for (int j = i + 1; j <= str.Length - 1; j++) { if (char.ToUpper(ch[i]) == char.ToUpper(ch[j])) { count++; ch[j] = '0'; } } Console.WriteLine(ch[i] + " = Ascii Value = "+ Convert.ToInt32(ch[i]) +" and Count = " + count); } } }
Share:

Grading Students - Hacker Rank Solution



At HackerLand University, a passing grade is any grade 40 points or higher on a 100 point scale. Sam is a professor at the university and likes to round each student’s grade according to the following rules:
·         If the difference between the grade and the next higher multiple of 5 is less than 3, round to the next higher multiple of 5
·         If the grade is less than 38, don’t bother as it’s still a failing grade
Automate the rounding process then round a list of grades and print the results.
Input Format
The first line contains a single integer denoting  (the number of students).
Each line  of the  subsequent lines contains a single integer, , denoting student 's grade.


 Grading Students - Hacker Rank Solution


Solution in C#

public static List<int> gradingStudents(List<int> grades) { List<int> Newgrades = new List<int>(); foreach (int item in grades) { int val = item; int next5mul = 0; int Valu1 = 0; int i = 1; while (item >= next5mul) { next5mul = i * 5; i++; } if (next5mul - item < 3) { Newgrades.Add(next5mul); } else { Newgrades.Add(item); } } return Newgrades; }







Share:

How to Find out special char ‘_’ in a string and convert next char to lower if in upper and vise versa of this problem Input : shiv_Kumar Output : shivkumar

Add caption



How to Find out special char ‘_’ in a string and convert next char to lower if in upper and vise versa of this problem

Input :           shiv_Kumar
Output :        shivkumar

Input :           thisIsShivKumarTutorials
OutPut :        this_is_shiv_kumar_tutorials

Input :           this_Is_Shiv_Kumar_Tutorials
OutPut :        thisisshivkumartutorials


static void FindCharinString(string arr)

        {

           char[] chr = arr.ToCharArray();
            //Array.Sort(chr); //SORTING ARRAY
            string newstr = "";
            if (arr.Contains('_') == true)
            {
                for (int i = 0; i < chr.Length; i++)
                {
                    if (chr[i] == '_')
                    {
                        chr[i + 1] = Char.ToLower(chr[i + 1]);
                    }
                    else
                    {
                        newstr += chr[i].ToString();
                    }
                }
            }
            else
            {
                for (int i = 0; i < chr.Length; i++)
                {
                    if (chr[i] == char.ToUpper(chr[i]))
                    {
                        newstr += "_" + char.ToLower(chr[i]);
                    }
                    else
                    {
                        newstr += chr[i].ToString();
                    }
                }
            }

            Console.WriteLine(newstr); }

Share:

Calculate the absolute difference between the sums of its diagonals.(Hacker Rank Problem )




Diagonal Difference





Solution 


public static int diagonalDifference(List<List<int>> arr) { int d1=0,d2=0; int count=arr.Count; for(int i=0;i<=arr.Count-1;i++) { for(int j=0;j<=arr[i].Count-1;j++) { if(i==j) { d1+=arr[i][j]; } if(i==count-j-1) { d2+=arr[i][j]; } } } return Math.Abs(d1-d2); }
Share:

How to Count no of repeated character in a String?



How to Count no of repeated character in a String?
Input   : ababcgah
OutPut : a3b2g1h1

Input : shivKvish
Output : s2h2i2v2k1

static void FindCharinString(string arr)
        {
{
           
          
            char[] chr = arr.ToCharArray();
            Array.Sort(chr); //SORTING ARRAY
            string newstr = "";
            for (int i = 0; i < chr.Length - 1; i++)
            {
                int count = 1;
                for (int j = i + 1; j <= arr.Length - 1; j++)
                {
                    if (chr[i] == chr[j])
                    {
                        count=count+1;

                    }
                    if (chr[i] != chr[j])
                    {
                        newstr += chr[i].ToString() + count;
                        i = j-1;
                        break;
                    }

                }
            }
            Console.WriteLine(newstr);
           


        }
  Output : K1h2i2s2




Share:

Popular

Tags

Mobile

Recent Posts