Notes![what is notes.io? What is notes.io?](/theme/images/whatisnotesio.png)
![]() ![]() Notes - notes.io |
#include<stdlib.h>
struct Bag{
int weight;
int marble_1;
int marble_2;
};
int binarySearch(struct Bag arr[], int l, int r, int x)
{
if (r >= l) {
int mid = l + (r - l) / 2;
if (arr[mid].weight == x)
return mid;
if (arr[mid].weight > x)
return binarySearch(arr, l, mid - 1, x);
return binarySearch(arr, mid + 1, r, x);
}
return -1;
}
void swap(struct Bag *a, struct Bag *b)
{
struct Bag temp = *a;
*a = *b;
*b = temp;
}
void Sort(struct Bag arr[], int n)
{
int i, j, min_idx;
for (i = 0; i < n - 1; i++) {
min_idx = i;
for (j = i + 1; j < n; j++)
if (arr[j].weight < arr[min_idx].weight)
min_idx = j;
swap(&arr[min_idx], &arr[i]);
}
}
int main(){
int key,n;
printf("Enter the number of bags:- n");
scanf("%d",&n);
struct Bag bag[3];
for(int i=0;i<n;i++){
printf("Enter the weights of marble 1 and 2 for bag%d :- ",i+1);
scanf("%d",&bag[i].marble_1);
scanf("%d",&bag[i].marble_2);
bag[i].weight=bag[i].marble_1+bag[i].marble_2;
}
printf("Enter the weight of the bag :- n");
scanf("%d",&key);
Sort(bag,n);
int index=binarySearch(bag,0,n-1,key);
printf("The index of the bag is :- %dn",index);
printf("Weight of Marble 1 :- %d",bag[index].marble_1);
printf("Weight of Marble 2 :- %d",bag[index].marble_2);
}
![]() |
Notes.io is a web-based application for taking notes. You can take your notes and share with others people. If you like taking long notes, notes.io is designed for you. To date, over 8,000,000,000 notes created and continuing...
With notes.io;
- * You can take a note from anywhere and any device with internet connection.
- * You can share the notes in social platforms (YouTube, Facebook, Twitter, instagram etc.).
- * You can quickly share your contents without website, blog and e-mail.
- * You don't need to create any Account to share a note. As you wish you can use quick, easy and best shortened notes with sms, websites, e-mail, or messaging services (WhatsApp, iMessage, Telegram, Signal).
- * Notes.io has fabulous infrastructure design for a short link and allows you to share the note as an easy and understandable link.
Fast: Notes.io is built for speed and performance. You can take a notes quickly and browse your archive.
Easy: Notes.io doesn’t require installation. Just write and share note!
Short: Notes.io’s url just 8 character. You’ll get shorten link of your note when you want to share. (Ex: notes.io/q )
Free: Notes.io works for 12 years and has been free since the day it was started.
You immediately create your first note and start sharing with the ones you wish. If you want to contact us, you can use the following communication channels;
Email: [email protected]
Twitter: http://twitter.com/notesio
Instagram: http://instagram.com/notes.io
Facebook: http://facebook.com/notesio
Regards;
Notes.io Team