K-pairs with smallest sum in 2 arrays in C.

K-pairs with smallest sum in 2 arrays in C

 

In this page we will look into a coding question where we will learn how to find the k pairs with the smallest sums such that one element of a pair belongs to arr1[] and another element belongs to arr2[]
There might be different approach to solve this question, one you will find here. If your approach is bit different post it onto the comment section.

Majority Element in C

Problem Statement

Write a program in C programming language where you need to find the element of array which occurs most time in the array

Input Format
    -First line will contain the number of elements present in the first array.
    -Second line will contain the elements of first array.
    -Third line will contain the number of element present in second array.
    -Fourth line will contain the elements of second array.
    -Fifth line will contain the number of pair which we have to print.

Output Format
   
-Output Contains space separated pairs from array 1 and array 2

Sample Test Case : 1

Input
3
1 7 11
3
2 4 6
3

Output
[1,2] [1,4] [1,6]

Sample Test Case : 2

Input
3
2 3 4
3
1 6 5 8
4

Output
[1,2] [1, 3] [1,4] [2,6]

Program for finding the element that occurs most time in an array

#include <stdio.h> 
int main()
{
    int n;
    scanf("%d",&n);
    int array[n];
    int max = 0;
    int count;
  int maxelement;
  for(int i=0i<ni++)
    {
        scanf("%d",&array[i]);
    }
    for(int i=0i<ni++)
    {
        count++;
        for(int j=0j<nj++)
        {
            if(array[i]==array[j])
            {
                count++;
            }
        }
        if(count>max)
        {
            max = count;
            maxelement = array[i];
        }    
    }
    printf("%d",maxelement);
    return 0;
}

Learn About Arrays in C

Click on the below button to learn about in C Programming Language

Learn Arrays in C

Learn Data Structures

Click on the below button to study more Data Structure

Learn Data Structures